//**********以下代码用SENDKEY打开记事本写信息,保存,关闭的例子*******开始********* private void button8_Click(object sender, EventArgs e) { Process txt = Process.Start(@"notepad.exe", @"d:/12.txt"); txt.StartInfo.WindowStyle = ProcessWindowStyle.Normal; txt.WaitForInputIdle(1000); if (txt.Responding) { SendKeys.SendWait("auto write!"); SendKeys.SendWait(textBox1.Text); SendKeys.SendWait("{Enter}{Enter}"); SendKeys.SendWait("{F5}"); SendKeys.SendWait("{Enter}{Enter}"); SendKeys.SendWait("^s"); SendKeys.SendWait("%{F4}"); } } //**********以下代码用SENDKEY打开记事本写信息,保存,关闭的例子*******开始********* //**********以下代码是操作计算器算“3+2=5”的例子*******开始********* [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)] private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam); [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)] private static extern void SetForegroundWindow(IntPtr hwnd); private void button9_Click(object sender, EventArgs e) { const uint BM_CLICK = 0xF5; //鼠标点击的消息,对于各种消息的数值,大家还是得去API手册 IntPtr hwndCalc = FindWindow(null, "電卓"); //查找计算器的句柄 if (hwndCalc != IntPtr.Zero) { IntPtr hwndThree = FindWindowEx(hwndCalc, 0, null, "3"); //获取按钮3 的句柄 IntPtr hwndPlus = FindWindowEx(hwndCalc, 0, null, "+"); //获取按钮 + 的句柄 IntPtr hwndTwo = FindWindowEx(hwndCalc, 0, null, "2"); //获取按钮2 的句柄 IntPtr hwndEqual = FindWindowEx(hwndCalc, 0, null, "="); //获取按钮= 的句柄 SetForegroundWindow(hwndCalc); //将计算器设为当前活动窗口 System.Threading.Thread.Sleep(2000); //暂停2秒让你看到效果 SendMessage(hwndThree, BM_CLICK, 0, 0); System.Threading.Thread.Sleep(2000); //暂停2秒让你看到效果 SendMessage(hwndPlus, BM_CLICK, 0, 0); System.Threading.Thread.Sleep(2000); //暂停2秒让你看到效果 SendMessage(hwndTwo, BM_CLICK, 0, 0); System.Threading.Thread.Sleep(2000); //暂停2秒让你看到效果 SendMessage(hwndEqual, BM_CLICK, 0, 0); System.Threading.Thread.Sleep(2000); MessageBox.Show("你看到结果了吗?"); } else { MessageBox.Show("没有启动 [计算器]"); } } //**********以下代码是操作计算器算“3+2=5”的例子*******结束********* //**********以下代码是显示/隐藏,系统任务栏的功能*******开始********* [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); private void button10_Click(object sender, EventArgs e) { //IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null); IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null); ShowWindow(trayHwnd, 1); } private void button11_Click(object sender, EventArgs e) { IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null); if (trayHwnd != IntPtr.Zero) { ShowWindow(trayHwnd, 0); } } //**********以下代码是显示/隐藏,系统任务栏的功能*******结束*********
以上就是敏感世界最近收集整理的关于c# 常用API的例子【不断更新】的全部内容,更多相关c#内容请搜索靠谱客的其他文章。
微信里点“发现”,扫一下
二维码便可将本文分享至朋友圈。
发表评论 取消回复