C# 操作鼠标
1 [DllImport("user32.dll")] 2 private static extern int SetCursorPos(int x, int y); 3 4 public enum MouseEventFlags 5 { 6 Move = 0x0001, 7 LeftDown = 0x0002, 8 LeftUp = 0x0004, 9 RightDown = 0x0008, 10 RightUp = 0x0010, 11 MiddleDown = 0x0020, 12 MiddleUp = 0x0040, 13 Wheel = 0x0800, 14 Absolute = 0x8000 15 } 16 [DllImport("User32")] 17 public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo); 18 19 /// <summary> 20 /// 移动鼠标到指定的坐标点 21 /// </summary> 22 public void MoveMouseToPoint() 23 { 24 string settingValue = ConfigurationManager.AppSettings.Get("Point"); 25 string[] pList = settingValue.Split(','); 26 Point centerP = new Point(int.Parse(pList[0]), int.Parse(pList[1])); 27 if (checkBox1.Checked) 28 { 29 SetCursorPos(centerP.X, centerP.Y); 30 mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); 31 mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); 32 } 33 } 34 /// <summary> 35 /// /// 设置鼠标的移动范围 36 /// </summary> 37 public void SetMouseRectangle(Rectangle rectangle) 38 { 39 System.Windows.Forms.Cursor.Clip = rectangle; 40 }