您的位置:首页 > 其它

WinForm 鼠标进入移开窗体事件,因子控件导致的误触发

2015-09-11 09:53 525 查看
/// <summary>
/// 重写OnControlAdded方法,为每个子控件添加MouseLeave事件
/// </summary>
/// <param name="e"></param>
protected override void OnControlAdded(ControlEventArgs e)
{
Control control = e.Control; // 获取添加的子控件
control.MouseLeave += this.SubControlLeave; // 当鼠标离开该子控件时判断是否是离开SelfDefinePanel
base.OnControlAdded(e);
}

/// <summary>
/// 重写OnMouseLeave事件,如果是离开本身的矩形区域则发生 base.OnMouseLeave(e);
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
//判断鼠标是否还在本控件的矩形区域内
if (!this.RectangleToScreen(this.ClientRectangle).Contains(Control.MousePosition)) // this.RectangleToScreen(this.ClientRectangle) 映射为屏幕的矩形
{
base.OnMouseLeave(e);
}
}


复制以上代码在需要的窗体中即可。这样,如果鼠标进入到窗体中的子控件,导致也触发了窗体的Leavel移开事件的问题就解决了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: