MouseLeave not fired C# WinForms

房东的猫 提交于 2019-12-24 02:25:09

问题


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();
}

And hiding like this:

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();
}

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 ?


回答1:


As this link states:

Mouse move messages are not accurate enough, they don't guarantee that every traversed pixel is reported. When you have a child window close to the edge of its parent, it is quite easy to not get the MouseEnter for the parent when you move the mouse fast enough.

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.



来源:https://stackoverflow.com/questions/15230257/mouseleave-not-fired-c-sharp-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!