The rectangular area above the panel to catch mouse inputs

C # winforms here. I need to draw an invisible area of ​​the rectangle above the panel and catch his mouse to enter / leave events.

My situation (as with some other suggestions you may have):

I have a media player (panel), when I enter the mouse I make a small navigation menu visible (it is located above the panel). I want to hide the navigation menu with the mouse out of the panel. This works, but, unfortunately, entering the navigation menu makes it invisible. Many thanks.

+4
source share
1 answer

When moving away from the mouse, just see if Cursor.Positionyour current rectangle contains . For example, using the panel and label:

    public Form1()
    {
        InitializeComponent();
        panel1.MouseEnter += panel1_MouseEnter;
        panel1.MouseLeave += common_MouseLeave;
        label1.MouseLeave += common_MouseLeave;
    }

    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        label1.Visible = true;
    }

    private void common_MouseLeave(object sender, EventArgs e)
    {
        Rectangle rc = panel1.RectangleToScreen(panel1.ClientRectangle);
        if (!rc.Contains(Cursor.Position))
        {
            label1.Visible = false;
        }
    }
+2

Source: https://habr.com/ru/post/1598788/


All Articles