2

I have a panel control, that has a background picture on it. I want it to change opacity one I move mouse over it. How can I do that? I tried:

  btnExit.BackColor = Color.FromArgb(20,63,63,63);
  btnExit.BackColor = Color.FromArgb(20);

but nothing changes.. Any ideas why this is not working? this panel is sitting on another panel, which also has background picture. Thanks!

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Kristian
  • 1,348
  • 4
  • 16
  • 39

1 Answers1

1

It can be done, as far as I know, with your method, but I guess that you have to refresh the control.

btnExit.Refresh();

EDIT:

First set your button FlatStyle to Flat.

this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

Then make two functions called btnExit_MouseHover and btnExit_MouseLeave:

void btnExit_MouseHover(object sender, EventArgs e)
{
  btnExit.BackColor = Color.FromArgb(20, 63, 63, 63);
  btnExit.Refresh();
}

void btnExit_MouseLeave(object sender, EventArgs e)
{
  btnExit.BackColor = Color.FromArgb(100, 63, 63, 63);
  btnExit.Refresh();
}

To activate these functions add two EventHandlers:

btnExit.MouseHover += new EventHandler(btnExit_MouseHover);
btnExit.MouseLeave += new EventHandler(btnExit_MouseLeave);

This will do the trick, now you only have to change the backcolor to the one you like ;).

Quispie
  • 948
  • 16
  • 30
  • Thanks! The panel control doesn't have flat appearance, so I tried this with both regular button with an image and panel with an image. regular button just changed background color, and panel flickers for a second like it's about to work, but the resets back. I set opacity to 100 for both mousehover and leave, but the same thing happens. Any tips? Thanks! – Kristian Jan 11 '13 at 07:20
  • I guess when you deactivate the MouseLeave eventHandler it will sort the problem. Or set a if statement/switch in mouseHover and reset the var in MouseLeave – Quispie Jan 11 '13 at 11:37