如何检查鼠标是否位于控件上
如何检查鼠标是否位于某一 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要复制此功能,只需调用
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 callReleaseCapture()
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.这是 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.