Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent Control Mouse Enter/Leave Events With Child Controls

Tags:

I have a C# .NET 2.0 WinForms app. My app has a control that is a container for two child controls: a label, and some kind of edit control. You can think of it like this, where the outer box is the parent control:

 +---------------------------------+  | [Label Control]  [Edit Control] | +---------------------------------+

I am trying to do something when the mouse enters or leaves the parent control, but I don't care if the mouse moves into one of its children. I want a single flag to represent "the mouse is somewhere inside the parent or children" and "the mouse has moved outside of the parent control bounds".

I've tried handling MouseEnter and MouseLeave on the parent and both child controls, but this means the action begins and ends multiple times as the mouse moves across the control. In other words, I get this:

 Parent.OnMouseEnter      (start doing something) Parent.OnMouseLeave      (stop) Child.OnMouseEnter       (start doing something) Child.OnMouseLeave       (stop) Parent.OnMouseEnter      (start doing something) Parent.OnMouseLeave      (stop)

The intermediate OnMouseLeave events cause some undesired effects as whatever I'm doing gets started and then stopped. I want to avoid that.

I don't want to capture the mouse as the parent gets the mouse over, because the child controls need their mouse events, and I want menu and other shortcut keys to work.

Is there a way to do this inside the .NET framework? Or do I need to use a Windows mouse hook?

like image 601
Paul Williams Avatar asked Jul 21 '09 19:07

Paul Williams


1 Answers

After more research, I discovered the Application.AddMessageFilter method. Using this, I created a .NET version of a mouse hook:

class MouseMessageFilter : IMessageFilter, IDisposable {     public MouseMessageFilter()     {     }      public void Dispose()     {         StopFiltering();     }      #region IMessageFilter Members      public bool PreFilterMessage(ref Message m)     {          // Call the appropriate event          return false;     }      #endregion      #region Events      public class CancelMouseEventArgs : MouseEventArgs     {...}      public delegate void CancelMouseEventHandler(object source, CancelMouseEventArgs e);     public event CancelMouseEventHandler MouseMove;     public event CancelMouseEventHandler MouseDown;     public event CancelMouseEventHandler MouseUp;      public void StartFiltering()     {         StopFiltering();         Application.AddMessageFilter(this);     }      public void StopFiltering()     {         Application.RemoveMessageFilter(this);     } } 

Then, I can handle the MouseMove event in my container control, check to see if the mouse is inside my parent control, and start the work. (I also had to track the last moused over parent control so I could stop the previously started parent.)

---- Edit ----

In my form class, I create and hookup the filter:

public class MyForm : Form {    MouseMessageFilter msgFilter;     public MyForm()    {...        msgFilter = new MouseMessageFilter();        msgFilter.MouseDown += new MouseMessageFilter.CancelMouseEventHandler(msgFilter_MouseDown);        msgFilter.MouseMove += new MouseMessageFilter.CancelMouseEventHandler(msgFilter_MouseMove);     }      private void msgFilter_MouseMove(object source, MouseMessageFilter.CancelMouseEventArgs e)     {         if (CheckSomething(e.Control)             e.Cancel = true;     }    } 
like image 186
Paul Williams Avatar answered Sep 18 '22 14:09

Paul Williams