1 /// <summary>
2 /// 重写OnControlAdded方法,为每个子控件添加MouseLeave事件
3 /// </summary>
4 /// <param name="e"></param>
5 protected override void OnControlAdded(ControlEventArgs e)
6 {
7 Control control = e.Control;
// 获取添加的子控件
8 control.MouseLeave +=
this.SubControlLeave;
// 当鼠标离开该子控件时判断是否是离开SelfDefinePanel
9 base.OnControlAdded(e);
10 }
11
12 /// <summary>
13 /// 重写OnMouseLeave事件,如果是离开本身的矩形区域则发生 base.OnMouseLeave(e);
14 /// </summary>
15 /// <param name="e"></param>
16 protected override void OnMouseLeave(EventArgs e)
17 {
18 //判断鼠标是否还在本控件的矩形区域内
19 if (!
this.RectangleToScreen(
this.ClientRectangle).Contains(Control.MousePosition))
// this.RectangleToScreen(this.ClientRectangle) 映射为屏幕的矩形
20 {
21 base.OnMouseLeave(e);
22 }
23 }
复制以上代码在需要的窗体中即可。这样,如果鼠标进入到窗体中的子控件,导致也触发了窗体的Leavel移开事件的问题就解决了。
转载于:https://www.cnblogs.com/qinyun118/p/4800044.html