如何检查鼠标是否在控件上 [英] How to check if a mouse is over a control

查看:189
本文介绍了如何检查鼠标是否在控件上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查鼠标是否超过某个HWND?我已经尝试使用WM_MOUSELEAVE和WM_MOUSEMOVE消息来跟踪,但如果你单击一个按钮并将鼠标拖出按钮,它不会收到WM_MOUSELEAVE直到鼠标释放,这是太晚了,因为:

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:

单击按钮时,只有在以下情况下才发送WM_COMMAND消息:

1.鼠标最初按在按钮上

2.鼠标在按钮上方

3.鼠标通过按钮释放

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

我需要复制此功能。

推荐答案

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

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:。您可以使用Windows API函数 WindowFromPoint()来确定鼠标在哪个窗口。

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

这篇关于如何检查鼠标是否在控件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆