473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 12045
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

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

Similar topics

2
by: Tom | last post by:
Hi friends I'm having trouble setting the background color of a user control to transparent ! why is that ? any help appreciated Thanks Tom
2
by: Jaikumar | last post by:
Hi, 1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image. 2) In the UserControl1 i am...
2
by: FredC | last post by:
I went thru the walkthough:"Walktrough: Authoring a User Control with Visual c#". All is fine except that when I add this control to my form that has a map as a background, I can't see through the...
3
by: Saladin | last post by:
Hello I've set datagrid BackColor = Transparent and this works well in IE. But in Firefox, I can't get rid of a bright blue background if set to Transparent. Any ideas please? Thanks Graeme
9
by: Rhino | last post by:
I've been updating some CSS today and got one odd error from the validator at http://jigsaw.w3.org/css-validator/. Every time I had 'background: transparent;' (or background-color: transparent;)...
4
by: tommaso.gastaldi | last post by:
I found useful to use transparent controls like: '---------------------------------------------------------------------------------- Public Class UserControlTransp Inherits...
2
by: serdar.c | last post by:
i have written a costum user control for my program works like an empty windows form, for styling and using reasons. and then i create inherited controls from that my main form design.....
8
by: Brian Ward | last post by:
I am looking for a simple way to set the image transparency in a PictureBox. I have a moving PictureBox containing a graphic image .. moving by incrementing its Left property. The background...
4
by: Rishabh Indianic | last post by:
i am developing smart phone application with VS 2005 using C#. i am trying to transparent the background of link label control which dynamically added in panel. i try with following code to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.