我有一个表单子类,其中包含MouseHover
和MouseLeave
的处理程序。当指针位于窗口背景上时,事件可以正常工作,但当指针移动到窗口内部的控件上时,就会引起MouseLeave
事件。
是否有任何方法可以覆盖整个窗口的事件。
(.NET 2.0,Visual Studio 2005,Windows XP。)
我有一个表单子类,其中包含MouseHover
和MouseLeave
的处理程序。当指针位于窗口背景上时,事件可以正常工作,但当指针移动到窗口内部的控件上时,就会引起MouseLeave
事件。
是否有任何方法可以覆盖整个窗口的事件。
(.NET 2.0,Visual Studio 2005,Windows XP。)
重写 MouseLeave 事件以使其在鼠标进入子控件时不触发
protected override void OnMouseLeave(EventArgs e)
{
if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
return;
else
{
base.OnMouseLeave(e);
}
}
没有好的方法使MouseLeave对容器控件可靠。 使用计时器解决此问题:
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
//...
}
}
}
}
private void picBoxThumb_MouseHover(object sender, EventArgs e)
{
// Call Parent OnMouseHover Event
OnMouseHover(EventArgs.Empty);
}
this.thumbImage1.MouseHover += new System.EventHandler(this.ThumbnailMouseHover);
在你的WinForm上调用这个方法
private void ThumbnailMouseHover(object sender, EventArgs e)
{
ThumbImage thumb = (ThumbImage) sender;
}
其中ThumbImage是用户控件的类型