Reputation: 6321
In C++ (WIN32), how can I get the (X,y) coordinate of a mouse click on the screen?
Upvotes: 5
Views: 16017
Reputation: 330
POINT p; //You can use this to store the values of x and y coordinates
Now assuming you will handle this on clicking the left mouse button
case WM_LBUTTONDOWN:
p.x = LOWORD(lParam); //X coordinate
p.y = HIWORD(lParam); //Y coordinate
/* the rest of your code */
break;
Upvotes: -1
Reputation: 11
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
bool find(xPos,yPos);
Now you will get the x and y position of the mouse pointer in the coordinate. xPos and yPos should be long:
bool find(long x,long y);
Inside, check if xPos and yPos come under any object in the screen coordinate.
Upvotes: 1
Reputation: 98964
Assuming the plain Win32 API, use this in your handler for WM_LBUTTONDOWN
:
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
Upvotes: 8
Reputation: 2592
Visual C++:
System::Windows::Forms::Control::MousePosition
System::Windows::Forms::Cursor:: Position
C++:
Upvotes: 1
Reputation: 7563
You can call GetMouseMovePointsEx to get the mouse position and history. Alternatively, if you have access to your wndproc, you can just check the lparam of WM_MOUSEMOVE, WM_LBUTTONDOWN or similar message for the x,y coordinates.
Upvotes: 1