3

So I have this form with a background. The problem was that I had a huge drop, performance wise. So someone told me to just use a picturebox and use "Set to back" to get the same effect.

Now the problem is that the background of my controls are not transparent anymore, but the same as the form background.

So someome told me to use this code:

control.Parent = pictureboxBackground;
control.BackColor = Color.Transparent;

But now I have to write those two lines of code for all of my 20 controls.

So I tried to use the following foreach statement:

foreach (Control but in tabPage2.Controls)
{
    but.Parent = pictureBox1;
    but.BackColor = Color.Transparent;
}   

But now only half of my controls' backcolors are made transparent.

for example:

Label1 is transparent

label2 isn't

button1 isn't

button2 is transparent

What am I doing wrong?

Domysee
  • 12,718
  • 10
  • 53
  • 84
Kev_T
  • 722
  • 2
  • 9
  • 40
  • I've deleted my answer since it didn't really solve the problem, the tab control may also be part of the issue. Transparency in winforms is a hack, it works by getting the background colour of the control behind it – Sayse Oct 16 '15 at 07:59
  • Yeah that's why I try to change the parent, but don't want those 40 lines of code I have now though. Thanks for your help anyways – Kev_T Oct 16 '15 at 08:01
  • Transparency effects become pretty expensive when the background image is large or has a pixel format that doesn't match the video adapter's pixel format or has to be resized to fit the window. Usually all three are a problem. Winforms makes it *very* easy to ignore those details, it is too helpful. You can't ignore them when you care about a responsive UI. Write the code to generate the bitmap up front so painting becomes cheap. It needs to match the form's ClientSize so it doesn't have to be resized and use the 32bppPArgb pixel format, it is ten times faster than the other ones. – Hans Passant Oct 16 '15 at 08:07
  • Dankje Hans ;) I really didn't know it would be this easy. I just changed the size of my picture to the size of the form and it's just as smooth as it was with a picturebox – Kev_T Oct 16 '15 at 08:17

1 Answers1

2

Try this:

foreach (Control but in tabPage2.Controls)
{
  but.Parent = pictureBox1;
  but.BackColor = Color.Transparent;
}

Application.DoEvents();

or

foreach (Control but in tabPage2.Controls)
{
  but.Parent = pictureBox1;
  but.BackColor = Color.Transparent;
  but.Invalidate();
}
Izzy
  • 6,740
  • 7
  • 40
  • 84
gogosweb
  • 101
  • 1
  • 3