Advertisement
ivandrofly

Sending Mouse Click in C#

Nov 3rd, 2013
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. // You can't do that by sending messages, instead use mouse_event Windows API, here is the code :
  2.  
  3. // Call method ClickOnPoint, this is an example from form click event, so this.handle is form handle, note that these are client coordinates on window witch handle is send, you can easily change this and send screen coordinates, and in that case you don't need handle or ClientToScreen call below.
  4.  
  5. ClickOnPoint(this.Handle, new Point(375, 340));
  6.  
  7. // Code:
  8. private void ClickOnPoint(IntPtr wndHandle, Point clientPoint)
  9. {
  10.   var oldPos = Cursor.Position;
  11.  
  12.   /// get screen coordinates
  13.   ClientToScreen(wndHandle, ref clientPoint);
  14.  
  15.   /// set cursor on coords, and press mouse
  16.   Cursor.Position = new Point(clientPoint.X, clientPoint.Y);
  17.   mouse_event(0x00000002, 0, 0, 0, UIntPtr.Zero); /// left mouse button down
  18.   mouse_event(0x00000004, 0, 0, 0, UIntPtr.Zero); /// left mouse button up
  19.  
  20.   /// return mouse
  21.   Cursor.Position = oldPos;
  22. }
  23. // Declarations :
  24.  
  25.     [DllImport("user32.dll")]
  26.     static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);  
  27.  
  28.     [DllImport("user32.dll")]
  29.     static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement