I need to detect when the user moves the mouse over the Form and all its child controls and also when it leaves the Form. I tried the MouseEnter
and MouseLeave
events of the Form, I tried the WM_MOUSEMOVE
& WM_MOUSELEAVE
and WM_NCMOUSEMOVE
& WM_NCMOUSELEAVE
pairs of windows messages but none seem to work as I want...
Most of my Form is occupied by child controls of many sorts, there's not much client area visible. This means that if I move the mouse very quickly the mouse movement will not be detected, although the mouse is inside the Form.
For instance, I have a TextBox that is docked at the bottom and between the desktop and the TextBox, there's only a very small border. If I quickly move the mouse from the bottom into the TextBox, the mouse movement won't be detected, but the mouse is inside the TextBox, therefore, inside the Form.
How can I achieve what I need?
On your user control create a mousehover Event for your control like this, (or other event type) like this
On your WinFrom which hosts the UserControl have this for the UserControl to Handle the MouseOver in your Designer.cs
Which calls this method on your WinForm
Where ThumbImage is the type of usercontrol
How about this: In your form's OnLoad, recursively go through all of the child controls (and their children) and hook up the MouseEnter event.
Then whenever the mouse enters any descendant, the event handler will be called. Similarly, you could hook up MouseMove and/or MouseLeave events.
Quick and dirty solution:
You can hook the main message loop and preprocess/postprocess any (WM_MOUSEMOVE) message what you want.