I have a form with child controls.
I want when user move mouse over the form, the form will be closed.
So I catch mouse enter and move leave on the form. But if I move mouse on any controls in form, mouse leave event will be also caught.
Please help me to solve this problem. Thanks.
UPDATE:
When the cursor’s position is on the form’s caption region (this region is called non-client region). I move mouse out of this region, I can’t receive the WM_MOUSELEAVE message as well as WM_NCMOUSELEAVE. Please help me in this problem. I want to receive a message when move mouse out of this region. Thanks.
Essentially you want to check if the cursor is in the scope of the control. Here is the solution:
(1) Add a
Panelin the form which is the same size as yourForm, and move all controls in the form to the panel. It’s easy to change: openMyForm.designer.cs, add the panel, and change all statements likethis.Controls.Add(myLabel);tothis.myPanel.Controls.Add(myLabel);.(2) Deal with
MouseEnterandMouseLeaveevents of the panel you added.(3) Why not use
Formin step 2? Why do we need aPanelwith the same size? Try it yourself. The narrow border of the form will make you crazy.(4) You can make the
ifstatements in step 2 to an extension method, which is helpful to furthur usage.