ahmed0saber

Perform mouse click in C++

May 4th, 2021
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. // Forward declaration of the LeftClick function
  4. void LeftClick ( );
  5.  
  6. // Forward declaration of the MouseMove function
  7. void MouseMove ( int x, int y );
  8.  
  9. int main()
  10. {
  11.     MouseMove(530, 1);
  12.     LeftClick();
  13.     MouseMove(530, 350);
  14.     LeftClick();
  15.     return 0;
  16. }
  17.  
  18. // LeftClick function
  19. void LeftClick ( )
  20. {  
  21.   INPUT    Input={0};
  22.   // left down
  23.   Input.type      = INPUT_MOUSE;
  24.   Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  25.   ::SendInput(1,&Input,sizeof(INPUT));
  26.  
  27.   // left up
  28.   ::ZeroMemory(&Input,sizeof(INPUT));
  29.   Input.type      = INPUT_MOUSE;
  30.   Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  31.   ::SendInput(1,&Input,sizeof(INPUT));
  32. }
  33.  
  34. // MouseMove function
  35. void MouseMove (int x, int y )
  36. {  
  37.     double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1;
  38.     double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1;
  39.     double fx = x*(65535.0f/fScreenWidth);
  40.     double fy = y*(65535.0f/fScreenHeight);
  41.     INPUT  Input={0};
  42.     Input.type      = INPUT_MOUSE;
  43.     Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
  44.     Input.mi.dx = fx;
  45.     Input.mi.dy = fy;
  46.     ::SendInput(1,&Input,sizeof(INPUT));
  47. }
Advertisement
Add Comment
Please, Sign In to add comment