You can use GetChildAtPoint() to determine if the mouse is over a child control.
private void panel1_MouseLeave(object sender, EventArgs e)
{
if (panel1.GetChildAtPoint(panel1.PointToClient(MousePosition)) == null)
{
panel1.BackColor = Color.Gray;
}
}
If the control isn't actually a child control, you can still use MousePosition and PointToScreen to determine if the mouse is still within the bounds of the control.
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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…