MouseLeave未触发C#WinForms [英] MouseLeave not fired C# WinForms

查看:117
本文介绍了MouseLeave未触发C#WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个按钮的用户控件,只有当鼠标位于控件区域内时,该控件才可见.

I have a user control with 2 buttons, that should only be visible when the mouse is inside the area of the control.

我正在显示这样的按钮:

I'm showing the buttons like this:

private void Node_MouseEnter(object sender, EventArgs e)
{           
    btn1.Show();
    btn2.Show();
}

并像这样隐藏:

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

private void Node_MouseLeave(object sender, EventArgs e)
{  
    btn1.Hide();
    btn2.Hide();
}

问题在于,有时(随机情况下)不会触发MouseLeave事件,并且即使鼠标位于控件之外,按钮仍保持可见.

The problem is that sometimes (random situations) the MouseLeave event is not fired, and the buttons stay visible, even with the mouse outside the control.

是否有可能多个事件发生冲突?

Is it possible that multiple events get in conflict ?

推荐答案

作为

鼠标移动消息不够准确,它们不能保证报告每个经过的像素.当子窗口靠近其父窗口的边缘时,如果您将鼠标移动得足够快,则很容易不获得父窗口的MouseEnter.

因此,解决方案是仅侦听MouseEnter事件,并且在触发该事件时,我向其他控件发送通知,以隐藏其按钮.

So, the solution was to listen only for the MouseEnterevent, and when this event is fired, i send a notification to the other controls, to hide its buttons.

这不是最优雅的解决方案,但它可以按预期工作.

Is not the most elegant solution, but it works as expected.

这篇关于MouseLeave未触发C#WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆