Vin*_*cio 4 c# events winforms
我有一个用于Panel控件的mouseenter和mouseleave事件,当鼠标进入时会改变背景颜色,当它离开时会返回白色.
我也在此面板中有Label控件,但当鼠标进入Label控件时,面板的mouseleave事件将触发.
这是有道理的,但是当鼠标位于其区域而其他控件不会影响它时,如何保持Panel的背景颜色相同?
您可以使用GetChildAtPoint()来确定鼠标是否在子控件上.
private void panel1_MouseLeave(object sender, EventArgs e)
{
if (panel1.GetChildAtPoint(panel1.PointToClient(MousePosition)) == null)
{
panel1.BackColor = Color.Gray;
}
}
Run Code Online (Sandbox Code Playgroud)
如果控件实际上不是子控件,您仍然可以使用MousePosition和PointToScreen来确定鼠标是否仍在控件的范围内.
private void panel1_MouseLeave(object sender, EventArgs e)
{
Rectangle screenBounds = new Rectangle(this.PointToScreen(panel1.Location), panel1.Size);
if (!screenBounds.Contains(MousePosition))
{
panel1.BackColor = Color.Gray;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9071 次 |
| 最近记录: |