命名空间,引用:
鼠标:
Using System.IO;
Using System.Security.Cryptography;
[DllImport("user32.dll", EntryPoint = "mouse_event", SetLastError = true)]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private static extern int mouse_event(int 事件对应值, int X坐标, int Y坐标, int 轮子,int 信息);
鼠标键位:
const int MOUSEEVENTF_MOVE = 0x0001; // 移动鼠标
const int MOUSEEVENTF_LEFTDOWN = 0x0002;// 模拟鼠标左键按下
const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTUP = 0x0010;// 模拟鼠标右键抬起
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
简单调用:
//首先让鼠标移动到固定位置 x=2000,y=2000 左上方开始,向下移动
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 2000, 2000, 0, 0);
//这里执行双击事件(其实双击就是两个单击(鼠标左键))
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 2000, 2000, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 2000, 2000, 0, 0);
键盘操作:
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
//基于当前位置操作tab健、Enter健(注意大括号)
SendKeys.SendWait("{Tab}");
SendKeys.SendWait("{Enter}");