写在前面
用WinForm做RPA项目时经常需要模拟鼠标操作,通过调用Windows Api 可以实现控制鼠标的移动、点击以及滚轮滚动,做到跟人工一样的操作。
代码实现
public static class MouseKeyController{[DllImport("user32")]private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]private static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]private static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);#region 屏幕分辨率和缩放百分比[DllImport("user32.dll")]private static extern IntPtr GetDC(IntPtr ptr);[DllImport("gdi32.dll")]private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);[DllImport("user32.dll", EntryPoint = "ReleaseDC")]private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);const int HORZRES = 8;const int VERTRES = 10;const int LOGPIXELSX = 88;const int LOGPIXELSY = 90;const int DESKTOPVERTRES = 117;const int DESKTOPHORZRES = 118;/// <summary>/// 获取真实屏幕分辨率大小/// </summary>public static Size DESKTOP{get {IntPtr hdc = GetDC(IntPtr.Zero);Size size = new Size();size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);ReleaseDC(IntPtr.Zero, hdc);return size;}}#endregion//移动鼠标 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;//模拟鼠标滚轮滚动操作,必须配合dwData参数const int MOUSEEVENTF_WHEEL = 0x0800;private static int screenWidth = 0;private static int screenHeight = 0;public static void Init(){screenWidth = Screen.PrimaryScreen.Bounds.Width;screenHeight = Screen.PrimaryScreen.Bounds.Height;}public static void MoveMouseWheel(int offset){mouse_event(MOUSEEVENTF_WHEEL, 0, 0, offset, 0);//鼠标滚动,使界面向下滚动offset的高度}public static void MoveMousePoint(int x, int y){ mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);//相对当前鼠标位置x轴和y轴分别移动50像素}public static void SetMousePoint(int x, int y){// 需要说明的是,如果没有使用MOUSEEVENTF_ABSOLUTE,函数默认的是相对于鼠标当前位置的点,如果dx,和dy,用0,0表示,这函数认为是当前鼠标所在的点。mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x * 65536 / screenWidth, y * 65536 / screenHeight, 0, 0);}public static void MouseLeftDown(){mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);}public static void MouseLeftUp(){mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);}public static void MouseLeftClick(){mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);}public static void MouseRightClick(){mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);}}
总结
已在项目中实际应用过了,真实可靠,可放心使用。