469,636 Members | 1,951 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Transparent usercontrol background

HaLo2FrEeEk
404 256MB
I'm working on an application that contains a UserControl that I made. I need this UserControl to have asemi-transparent background, but the controls need to stay 100% visible. Right now I'm using the code that I found in this thread on the MSDN forums:

http://social.msdn.microsoft.com/for...-b8411c5bbffb/

And embedding the UserControl onto my main form like this:

Expand|Select|Wrap|Line Numbers
  1. commDisplay thisComm = new commDisplay(checkComm, player.Player.CommendationState[showCommID]);
  2. thisComm.Location = new Point(12, 186);
  3. thisComm.Name = "commDisplay";
  4. thisComm.Opacity = 127;
  5. this.Controls.Add(thisComm);
So with my little:

thisComm.Opacity = 127;

line, I should see a nicely rendered UserControl with a 50% opacity, black background. I don't get this result. Instead what I sometimes get is the UserControl with a completely transparent background and messed up controls, and sometimes I get a UserControl with the proper background opacity, and still screwed up controls. Picture:



The only reason I can think as to why this is happening is that I'm adding the UserControl to the form programmatically, not through the Designer. Still, there has to be a way I can fix this, I need the partially transparent background.

Here's the full implementation of the transparency methods:

Expand|Select|Wrap|Line Numbers
  1. protected override CreateParams CreateParams
  2. {
  3.     get
  4.     {
  5.         CreateParams cp = base.CreateParams;
  6.         cp.ExStyle |= 0x00000020;
  7.         return cp;
  8.     }
  9. }
  10.  
  11. public int Opacity
  12. {
  13.     get { return opacity; }
  14.     set
  15.     {
  16.         opacity = value;
  17.         this.InvalidateEx();
  18.     }
  19. }
  20.  
  21. protected override void OnPaintBackground(PaintEventArgs e)
  22. {
  23.     Color bk = Color.FromArgb(Opacity, this.BackColor);
  24.     e.Graphics.FillRectangle(new SolidBrush(bk), e.ClipRectangle);
  25. }
  26.  
  27. protected void InvalidateEx()
  28. {
  29.     if (Parent == null)
  30.         return;
  31.     Rectangle rc = new Rectangle(this.Location, this.Size);
  32.     Parent.Invalidate(rc, true);
  33. }
Also of note, I'm using this control to display a single piece of information from an array. The user is given buttons to flip through the array, each time the button is pressed the current control is removed from the form and a new one is generated with the new information, then added to the form. Something odd sometimes happens with this. The first time the UserControl is added to the form (which isn't at runtime, it happens when the user clicks a button) it will almost always have a completely transparent background (even though the BackColor property is set to black). When I click the "next" button to flip through the array, the control is rendered with the proper opacity, but the controls are still messed up.
Dec 29 '10 #1
8 11079
GaryTexmo
1,501 Expert 1GB
I'm having trouble understanding the screen shot... blah, maybe my brain just isn't functioning this first day back but my eyes refuse to cooperate.

So I understand, the problem is that your user control is coming out completely transparent and the controls on it are fully opaque, right?

What's the background by the way, is it the desktop (and/or other programs running), or is it parts of your form?
Dec 29 '10 #2
HaLo2FrEeEk
404 256MB
The background is just a BackgroundImage I set on the form to test the transparency.

I've fixed some of the issues, like the background sometmes being completely transparent and sometimes rendering correctly, but the child controls (mainly the labels, but also the panels that should be immediately above the "(Silver) 1086" text in the screenshot. Hre is a screenshot of a properly working, but not semi-transparent, implementation of my UserControl:



Everything's good there except the black background of the text in the progress panels.
Dec 29 '10 #3
GaryTexmo
1,501 Expert 1GB
Ok, so I played around with this and I think it's because there's no opacity property on a control itself, so while your user control paints correctly, the controls on it have no way to do this too.

So what I tried doing was creating a new control (in my case, a button) that also had those opacity properties and methods on it. This kind of worked in that when I changed the opacity with a slider, I saw my button go transparent too... but then the paint method just comes right over top and does a full colour paint. I can't figure out how to prevent this... short of doing a custom paint that is.

I did some googling around and don't see any easy way to do this, it seems controls just aren't built for transparency, so you'll likely have to go with a custom paint to get the effect you want. This is really easy for a button or for a label but will probably be difficult for other controls. Here's a codeproject link to get you started...

http://www.codeproject.com/KB/buttons/VistaButton.aspx

If you want to discuss this further, or generate ideas on how to make controls transparent, I'm definitely willing.
Dec 29 '10 #4
HaLo2FrEeEk
404 256MB
I'm at work now but when I get home I'll post my current source for the UserControl. Post here again to remind me (I'll see the email when I get home).
Dec 30 '10 #5
HaLo2FrEeEk
404 256MB
You didn't post to remind me! It's ok though, because I got off work at midnight and crashed immediately after getting home. I slept for 14 hours!

Here's the source for the current source for the UserControl:

http://infectionist.com/extras/cshar...mDisplay_2.zip

Hopefully we can find what's going wrong. Right now I've got all of the labels set to black background color, but to see the weird effects they should be set to transparent. I tried inheriting the label too to see if I could paint it manually, but it didn't work. It just looks like it's taking it's e.ClipRectangle from the top-left of the parent form instead of the UserControl.
Dec 31 '10 #6
GaryTexmo
1,501 Expert 1GB
I'm not ignoring you! Haha I'm on vacation again and with new years happening I probably won't get to this for a bit. I just wanted to let you know, but I will come back to it :)
Dec 31 '10 #7
HaLo2FrEeEk
404 256MB
It's cool, I figured it out. I had to make my own label class that inherited from a label, use SetStyle() to set Opaque, DoubleBuffer, and OptimizedDoubleBuffer to false, override CreateParams and give it the WS_EX_TRANSPARENT style, then override the OnPaintBackground event so that it didn't draw the background, and finally override the OnPaint event so I could draw it with my own specified opacity. I didn't really need to override the OnPaint event, because I didn't want to set my own transparency on the label, but it doesn't hurt to have it there just in case. Now my labels render properly:



I also had to do all that for the panels (progess bar-style control), because I did want them to have transparency.
Jan 2 '11 #8
GaryTexmo
1,501 Expert 1GB
Glad you got it figured out. Yea I think that's the way to go, you need to make your own controls that can handle transparency because Microsoft, in their infinite wisdom, didn't think it was a good idea to do it on anything other than the form to start with.

Ah well, glad you got it sorted out!
Jan 6 '11 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Tom | last post: by
9 posts views Thread by Rhino | last post: by
4 posts views Thread by tommaso.gastaldi | last post: by
2 posts views Thread by serdar.c | last post: by
8 posts views Thread by Brian Ward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.