Mark
Mark

Reputation: 6321

How do you get the location, in x-y coordinate pixels, of a mouse click?

In C++ (WIN32), how can I get the (X,y) coordinate of a mouse click on the screen?

Upvotes: 5

Views: 16017

Answers (5)

Mr.Cat
Mr.Cat

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

ram
ram

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

Georg Fritzsche
Georg Fritzsche

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

Andrew Keith
Andrew Keith

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

Related Questions