10

MouseHover我有 Form 子类和处理程序MouseLeave。当指针在窗口的背景上时,事件工作正常,但是当指针移动到窗口的控件上时,它会引发一个MouseLeave事件。

反正有一个事件覆盖整个窗口。

(.NET 2.0、Visual Studio 2005、Windows XP。)

4

4 回答 4

10

只要鼠标进入子控件,就不会触发 MouseLeave 事件

    protected override void OnMouseLeave(EventArgs e)
    {
        if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
            return;
        else
        {
            base.OnMouseLeave(e);
        }
    }
于 2012-08-30T14:36:00.777 回答
6

没有使 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
                //...
            }
        }
    }
}
于 2010-01-06T18:17:34.910 回答
6

当触发鼠标离开事件时,一种选择是检查指针的当前位置并查看它是否在表单区域内。我不确定是否有更好的选择。

编辑:我们有一个您可能感兴趣的类似问题。如何检测鼠标是否在 C# 中的整个表单和子控件内?

于 2010-01-06T18:18:49.550 回答
0

在您的用户控件上为您的控件创建一个鼠标悬停事件,像这样(或其他事件类型)像这样

private void picBoxThumb_MouseHover(object sender, EventArgs e)
{
    // Call Parent OnMouseHover Event
    OnMouseHover(EventArgs.Empty);
}

在承载 UserControl 的 WinForm 上有这个供 UserControl 处理 MouseOver 所以把它放在你的 Designer.cs

this.thumbImage1.MouseHover += new System.EventHandler(this.ThumbnailMouseHover);

在您的 WinForm 上调用此方法

private void ThumbnailMouseHover(object sender, EventArgs e)
{

    ThumbImage thumb = (ThumbImage) sender;

}

其中 ThumbImage 是用户控件的类型

于 2011-04-08T19:19:37.800 回答