4

c# winforms here. I need to draw an invisible rectangle area over a panel and catch his mouse enter/leave events.

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

I have a media player (the panel), on mouse enter event I make visible a little navigation menu (it's located over the panel). I want to hide the nav menu on mouse leave from the panel. This works but unfortunately also entering the nav menu make it invisible. Many thanks.

WizardingStudios
  • 554
  • 2
  • 7
  • 22

1 Answers1

3

On mouse leave, simply see if the current Cursor.Position is contained by your rectangle. For example, using a panel and a 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;
        }
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40