如何检查鼠标是否位于控件上

发布于 2024-10-08 23:37:46 字数 233 浏览 0 评论 0原文

如何检查鼠标是否位于某一 HWND 上方?我尝试过使用 WM_MOUSELEAVE 和 WM_MOUSEMOVE 消息来跟踪,但是如果您单击按钮并将鼠标拖出按钮,则在释放鼠标之前它不会收到 WM_MOUSELEAVE,这为时已晚,因为

:单击按钮,仅在以下情况下发送 WM_COMMAND 消息:
1. 鼠标最初是在按钮上按下的
2. 鼠标位于按钮上
3. 鼠标在

我需要复制此功能的按钮上释放。

How does one check if the mouse is over a certain HWND? I have tried using the WM_MOUSELEAVE and WM_MOUSEMOVE messages to keep track, but if you click a button and drag the mouse out of the button, it doesn't receive the WM_MOUSELEAVE until the mouse is released, which is too late, because:

When you click a button, the WM_COMMAND message is only sent if:
1. The mouse was originally depressed over the button
2. The mouse is over the button
3. The mouse is released over the button

I need to replicate this functionality.

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

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

发布评论

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

评论(2

半世晨晓 2024-10-15 23:37:46

要复制此功能,只需调用 SetCapture() 即可将鼠标消息发送到您的窗口,即使鼠标离开它也是如此。您可以在鼠标移动时读取当前鼠标位置,并确定它是否仍在您的窗口/按钮上(我仍然不能 100% 确定您在做什么)。并且,当释放鼠标按钮时,您可以调用 ReleaseCapture() 来恢复发送鼠标消息的位置。

编辑:哦,您可以使用 Windows API 函数 WindowFromPoint() 来确定鼠标位于哪个窗口上。

To duplicate this functionality, just call SetCapture() so that mouse messages are sent to your window even if the mouse leaves it. You can read the current mouse position as it moves and determine if it is still over your window/button (I'm still not 100% sure what you are doing). And, when the mouse button is released, you can call ReleaseCapture() to restore where mouse messages are sent.

EDIT: Oh, and you can use the Windows API function WindowFromPoint() to determine which window the mouse is over.

瑶笙 2024-10-15 23:37:46

这是 Windows 内置的,称为“鼠标捕获”,SetCapture(hWnd)。即使鼠标移出窗口,它也能确保您收到鼠标消息。您可以在 WM_LBUTTONDOWN 消息通知上调用它。

This is built-in to Windows, it is called 'mouse capture', SetCapture(hWnd). It ensures you get mouse messages even though the mouse has moved outside of the window. You call it on the WM_LBUTTONDOWN message notification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文