[DllImport("user32.dll")] public static extern void mouse_event(MouseEventFlags dwFlags, uint dx, uint dy, uint dwData, MouseEventDataXButtons dwExtraInfo); [DllImport("User32.Dll")] public static extern long SetCursorPos(int x, int y);
[Flags] public enum MouseEventFlags : uint { LEFTDOWN = 0x00000002, LEFTUP = 0x00000004, MIDDLEDOWN = 0x00000020, MIDDLEUP = 0x00000040, MOVE = 0x00000001, ABSOLUTE = 0x00008000, RIGHTDOWN = 0x00000008, RIGHTUP = 0x00000010, WHEEL = 0x00000800, XDOWN = 0x00000080, XUP = 0x00000100 } //Use the values of this enum for the 'dwData' parameter //to specify an X button when using MouseEventFlags.XDOWN or //MouseEventFlags.XUP for the dwFlags parameter. public enum MouseEventDataXButtons : uint { XBUTTON1 = 0x00000001, XBUTTON2 = 0x00000002 }
public static void MoveMouse(int x, int y) { PInvoke.SetCursorPos(x, y); } public static void ClickMouse(int x, int y) { PInvoke.mouse_event(MouseEventFlags.LEFTDOWN | MouseEventFlags.ABSOLUTE, (uint)x, (uint)y, 0, 0); Thread.Sleep(10); PInvoke.mouse_event(MouseEventFlags.LEFTUP | MouseEventFlags.ABSOLUTE, (uint)x, (uint)y, 0, 0); }
References
https://stackoverflow.com/questions/13520705/move-mouse-to-position-and-left-click
http://www.pinvoke.net/default.aspx/user32.mouse_event
https://stackoverflow.com/questions/8050825/how-to-move-mouse-cursor-using-c
https://www.pinvoke.net/default.aspx/user32.setcursorpos