如何获取鼠标单击的位置(以 xy 坐标像素为单位)?

发布于 2024-08-13 21:51:44 字数 41 浏览 2 评论 0原文

在C++(WIN32)中,如何获取鼠标在屏幕上单击的(X,y)坐标?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦初启 2024-08-20 21:51:44

假设使用普通 Win32 API,请在 < 的处理程序中使用此 API代码>WM_LBUTTONDOWN

xPos = GET_X_LPARAM(lParam); 
yPos = GET_Y_LPARAM(lParam);

Assuming the plain Win32 API, use this in your handler for WM_LBUTTONDOWN:

xPos = GET_X_LPARAM(lParam); 
yPos = GET_Y_LPARAM(lParam);
情仇皆在手 2024-08-20 21:51:44

您可以调用 GetMouseMovePointsEx 来获取鼠标位置和历史。或者,如果您有权访问 wndproc,则只需检查 WM_MOUSEMOVEWM_LBUTTONDOWN 或 x,y 坐标的类似消息。

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.

[浮城] 2024-08-20 21:51:44
xPos = GET_X_LPARAM(lParam); 
yPos = GET_Y_LPARAM(lParam);
bool find(xPos,yPos);

现在您将获得鼠标指针在坐标中的 x 和 y 位置。 xPos 和 yPos 应该很长:

bool find(long x,long y);

在内部,检查 xPos 和 yPos 是否位于屏幕坐标中的任何对象下方。

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.

掩耳倾听 2024-08-20 21:51:44
POINT p; //You can use this to store the values of x and y coordinates

现在假设您将通过单击鼠标左键来处理此问题

    case WM_LBUTTONDOWN:
        p.x = LOWORD(lParam); //X coordinate
        p.y = HIWORD(lParam); //Y coordinate
        /* the rest of your code */
        break;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文