0

I currently have a form that has a completely transparent background. At the moment I have to picture boxes that appear at the top of the forum when a user hovers over a control on the form.

Screenshot!

Hovering over the PictureBox triggers the MouseEnter event properly and sets the buttons Visible state to true and the MouseLeave event sets it to false. The buttons themselves have the same MouseEnter and MouseLeave events but as Winforms passes mouse events to the form under any space on the form that is transparent (my images used in the buttons are transparent around them as well) whenever I go to click the buttons, they disappear as the form thinks that the mouse has "left" both the buttons or the form. Does anyone know ANY way of stopping the pass-through of the events?

Some code you ask? Some code you get :)

// Form Constructor!
// map = picturebox, this = form, move = first button, attach = second button
public Detached(PictureBox map)
{
    InitializeComponent();
    doEvents(map, this, this.attach, this.move);
}

// doEvents method! I use this to add the event to all controls
// on the form!
void doEvents(params Control[] itm)
{
    Control[] ctls = this.Controls.Cast<Control>().Union(itm).ToArray();
    foreach (Control ctl in ctls)
    {
        ctl.MouseEnter += (s, o) =>
        {
            this.attach.Visible = true;
            this.move.Visible = true;
        };
        ctl.MouseLeave += (s, o) =>
        {
            this.attach.Visible = false;
            this.move.Visible = false;
        };
    }
}
jduncanator
  • 2,154
  • 1
  • 22
  • 39

1 Answers1

0

Thanks to Hans Passant for pointing me in the correct direction. I ended up creating a thread that checks if the mouse is in the bounds every 50ms.

public Detached(PictureBox map)
{
    Thread HoverCheck = new Thread(() =>
    {
        while (true)
        {
            if (this.Bounds.Contains(Cursor.Position))
            {
                ToggleButtons(true);
            }
            else
            {
                ToggleButtons(false);
            }
            Thread.Sleep(50);
        }
    });
    HoverCheck.Start();
}

void ToggleButtons(bool enable)
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(() => ToggleButtons(enable)));
        return;
    }

    this.attach.Visible = enable;
    this.move.Visible = enable;
    this.pictureBox1.Visible = enable;
}

Thanks :)

jduncanator
  • 2,154
  • 1
  • 22
  • 39