家长控制鼠标进入/离开事件有了子控件 [英] Parent Control Mouse Enter/Leave Events With Child Controls

查看:202
本文介绍了家长控制鼠标进入/离开事件有了子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#.net 2.0 WinForms应用程序。我的应用程序有一个控件是一个容器两个子控件:一个标签,和一些编辑控制。你可以认为它像这样,这里的外包装盒是父控件:

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] |
+---------------------------------+

我试图做一些事情,当鼠标进入或离开父母的控制,但我不在乎,如果鼠标移动到它的一个孩子。我希望有一个单一的标志重新present鼠标某处父母或子女内部和鼠标已经移动母公司控制的范围之外。

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".

我已经试过处理的MouseEnter和鼠标离开父和两个子控件,但这意味着行动开始和结束时多次通过控制鼠标的移动。换句话说,我得到这样的:

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)

中间事件OnMouseLeave在造成一些不良影响的一切我做得到启动,然后停止。我想避免的。

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.

有没有一种方法来在.NET框架内做到这一点?或者,我需要使用Windows鼠标钩子?

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

推荐答案

在更多的研究,我发现了<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx">Application.AddMessageFilter法。用这种方法,我创建了一个鼠标钩子一个.NET版本:

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);
	}
}

然后,我可以处理MouseMove事件在我的容器控件,请检查如果鼠标是我的父母控件内,并开始工作。 (我也有跟踪的最后滑鼠移到父控件这样我就可以停止previously开始父。)

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;
    }   
}

这篇关于家长控制鼠标进入/离开事件有了子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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