Windows 窗体上的鼠标离开事件

发布于 2024-12-10 18:01:36 字数 164 浏览 5 评论 0原文

我已经在 Windows 窗体上设置了“鼠标离开”事件,并且想在鼠标离开可见区域时隐藏窗体。

但这是我面临问题的地方。即使当我将鼠标移动到同一窗体上的按钮时,它也会调用“鼠标离开”事件,这使得该窗体不可见。

这意味着我必须防止将鼠标移动到按钮时触发事件。但如何呢? 还有其他方法吗?

I've set the 'mouse leave' event on a windows form and I want to hide the form when the mouse leaves the visible area.

But here is where I am facing a problem. Even when I move the mouse to a button on the same form, it calls the 'mouse leave' event, which makes this form invisible.

It means I've got to prevent the event triggering when moving the mouse to the button. But how?
Any other approach?

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

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

发布评论

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

评论(3

○愚か者の日 2024-12-17 18:01:36

没有简单的方法可以做到这一点。一种方法可能是检查窗体内的所有控件,如果鼠标不在其中任何一个上,则意味着鼠标位于窗体之外

另一种方法可能是检查鼠标离开事件内部鼠标是否位于窗口边界内

There is no simple way to do it. One way could be to check all the controls inside the Form and if mouse is not over any of them this means mouse is outside the Form

Another way could be to check inside mouse leave event whether mouse is inside the window boundary or not

桃扇骨 2024-12-17 18:01:36

非常简单...添加以下内容:

protected override void OnMouseLeave(EventArgs e)
{

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

it's very simple...add this:

protected override void OnMouseLeave(EventArgs e)
{

    if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
         return;
    else
    {
        base.OnMouseLeave(e);
    }
}
无人接听 2024-12-17 18:01:36
// On the form's MouseLeave event, please checking for mouse position is not in each control's client area.
private void TheForm_MouseLeave(object sender, EventArgs e)
{
    bool mouse_on_control = false;
    foreach (var c in Controls)
        mouse_on_control |= c.RectangleToScreen(c.ClientRectangle).Contains(Cursor.Position);
    if (!mouse_on_control)
        Close();
}

// And in addition, if any control inside has its client area overlapping the form's client area at any border, 
// please also checking if mouse position is outside the form's client area on the control's MouseLeave event.
private void ControlOverlappedTheFormsBorder_MouseLeave(object sender, EventArgs e)
{
    if (!RectangleToScreen(ClientRectangle).Contains(Cursor.Position))
        Close();
}
// On the form's MouseLeave event, please checking for mouse position is not in each control's client area.
private void TheForm_MouseLeave(object sender, EventArgs e)
{
    bool mouse_on_control = false;
    foreach (var c in Controls)
        mouse_on_control |= c.RectangleToScreen(c.ClientRectangle).Contains(Cursor.Position);
    if (!mouse_on_control)
        Close();
}

// And in addition, if any control inside has its client area overlapping the form's client area at any border, 
// please also checking if mouse position is outside the form's client area on the control's MouseLeave event.
private void ControlOverlappedTheFormsBorder_MouseLeave(object sender, EventArgs e)
{
    if (!RectangleToScreen(ClientRectangle).Contains(Cursor.Position))
        Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文