我有处理程序的表单子类MouseHover
和MouseLeave
。当指针在窗口的背景上时,事件工作正常,但当指针移动到窗口内的控件时,它会导致MouseLeave
事件。整个窗口上的MouseHover/MouseLeave事件
无论如何有一个事件覆盖整个窗口。
(.NET 2.0,Visual Studio 2005中,Windows XP中。)
我有处理程序的表单子类MouseHover
和MouseLeave
。当指针在窗口的背景上时,事件工作正常,但当指针移动到窗口内的控件时,它会导致MouseLeave
事件。整个窗口上的MouseHover/MouseLeave事件
无论如何有一个事件覆盖整个窗口。
(.NET 2.0,Visual Studio 2005中,Windows XP中。)
当鼠标离开事件被触发一个选项是检查指针的当前位置,看看它的形态区域内。我不确定是否有更好的选择。
编辑:我们有一个类似的问题,你可能会感兴趣。 How to detect if the mouse is inside the whole form and child controls in C#?
有使鼠标离开可靠的容器控件没有什么好办法。具有定时平底船这个问题:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 200;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
}
private bool mEntered;
void timer1_Tick(object sender, EventArgs e) {
Point pos = this.PointToClient(Cursor.Position);
bool entered = this.ClientRectangle.Contains(pos);
if (entered != mEntered) {
mEntered = entered;
if (!entered) {
// Do your leave stuff
//...
}
}
}
}
在用户控件创建一个MouseHover事件为控制这个样子,(或其他事件类型)这样
private void picBoxThumb_MouseHover(object sender, EventArgs e)
{
// Call Parent OnMouseHover Event
OnMouseHover(EventArgs.Empty);
}
在你的WinForm它承载的用户控件有这样的用户控件处理mouseOver所以把这个在您的Designer.cs
this.thumbImage1.MouseHover += new System.EventHandler(this.ThumbnailMouseHover);
您的WinForm调用该方法
private void ThumbnailMouseHover(object sender, EventArgs e)
{
ThumbImage thumb = (ThumbImage) sender;
}
哪里ThumbImage是用户控件
Ovveride MouseLeave事件的类型不会触发,只要鼠标进入一个子控件
protected override void OnMouseLeave(EventArgs e)
{
if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return;
else
{
base.OnMouseLeave(e);
}
}
所以,如果我注册的MouseEnter和鼠标离开了相同的处理*所有*控制在窗口内(包括内部面板),然后让该处理程序检查鼠标位置以检测内部/外部的变化并调用“真正”进入/离开处理程序。好主意,谢谢。 – billpg 2010-01-06 18:33:28
我添加了一个如何为整个表单使用一个MouseEnter和MouseLeave事件的例子。让我知道,如果这对你有用 – SwDevMan81 2010-01-06 19:49:16
好主意。但是如果包含的控件位于边界上,它将不起作用。 (向此用户发送信用[here](http://stackoverflow.com/a/19361582/939213))。 – ispiro 2013-12-28 22:12:27