Parent Control Mouse Enter/Leave Events With Child Controls

前端 未结 5 1065
天涯浪人
天涯浪人 2020-11-27 18:49

I have a C# .NET 2.0 WinForms app. My app has a control that is a container for two child controls: a label, and some kind of edit control. You can think of it like this,

5条回答
  •  长情又很酷
    2020-11-27 19:01

    You can find out whether the mouse is within the bounds of your control like this (assuming this code resides in your container control; if not, replace this with a reference to the container control):

    private void MyControl_MouseLeave(object sender, EventArgs e)
    {
        if (this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
        {
            // the mouse is inside the control bounds
        }
        else
        {
            // the mouse is outside the control bounds
        }
    }
    

提交回复
热议问题