Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a mouse is over a control

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.

like image 997
John Zane Avatar asked Dec 21 '10 00:12

John Zane


1 Answers

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.

like image 166
Jonathan Wood Avatar answered Sep 18 '22 16:09

Jonathan Wood