整个窗口的 MouseHover/MouseLeave 事件

发布于 2024-08-17 04:15:45 字数 224 浏览 4 评论 0原文

我有 Form 子类,其中包含 MouseHoverMouseLeave 的处理程序。当指针位于窗口背景上时,事件正常工作,但当指针移动到窗口内的控件上时,会引发 MouseLeave 事件。

无论如何,是否有一个事件覆盖整个窗口。

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

I have Form subclass with handlers for MouseHover and MouseLeave. When the pointer is on the background of the window, the events work fine, but when the pointer moves onto a control inside the window, it causes a MouseLeave event.

Is there anyway to have an event covering the whole window.

(.NET 2.0, Visual Studio 2005, Windows XP.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜妞爱困 2024-08-24 04:15:45

重写 MouseLeave 事件,使其在鼠标进入子控件时不会触发

    protected override void OnMouseLeave(EventArgs e)
    {
        if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
            return;
        else
        {
            base.OnMouseLeave(e);
        }
    }

Ovveride the MouseLeave event to not trigger so long as the mouse enters a child control

    protected override void OnMouseLeave(EventArgs e)
    {
        if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
            return;
        else
        {
            base.OnMouseLeave(e);
        }
    }
椵侞 2024-08-24 04:15:45

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

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

When the mouse leave event is fired one option is to check for the current position of the pointer and see if it within the form area. I am not sure whether a better option is available.

Edit: We have a similar question which might be of interest to you. How to detect if the mouse is inside the whole form and child controls in C#?

荭秂 2024-08-24 04:15:45

没有好的方法可以使 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
                //...
            }
        }
    }
}

There is no good way to make MouseLeave reliable for a container control. Punt this problem with a timer:

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
                //...
            }
        }
    }
}
孤独患者 2024-08-24 04:15:45

在您的用户控件上,为您的控件创建一个 mousehover 事件,如下所示(或其他事件类型),如下所示

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 是用户控件的类型

On your user control create a mousehover Event for your control like this, (or other event type) like this

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

On your WinForm which hosts the UserControl have this for the UserControl to Handle the MouseOver so place this in your Designer.cs

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

Which calls this method on your WinForm

private void ThumbnailMouseHover(object sender, EventArgs e)
{

    ThumbImage thumb = (ThumbImage) sender;

}

Where ThumbImage is the type of usercontrol

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文