How do you get the location, in x-y coordinate pixels, of a mouse click?
In C++ (WIN开发者_JAVA技巧32), how can I get the (X,y) coordinate of a mouse click on the screen?
Assuming the plain Win32 API, use this in your handler for WM_LBUTTONDOWN
:
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
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.
Visual C++:
GetCursorPos
System::Windows::Forms::Control::MousePosition
System::Windows::Forms::Cursor:: Position
C++:
Mouse Position
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.
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;
精彩评论