Transparent Label on Gradient Panel

  • Thread starter Thread starter Mark Roebke
  • Start date Start date
M

Mark Roebke

Hello, I think I have a pretty simple problem, but have
spent a lot of time researching, only to find no 'real'
solution.

I have a Panel control that I'm filling with a gradient
using a LinearGradientBrush in the Paint event of the
Panel:

Control control = (Control)sender;
Graphics g = control.CreateGraphics();
g.Clear(Color.White);
Color startColor = Color.FromArgb(106, 127, 156);
Color endColor = Color.FromArgb(177, 197, 218);
LinearGradientBrush darkBrush = new LinearGradientBrush
(control.ClientRectangle, startColor, endColor,
LinearGradientMode.Vertical);
g.FillRectangle(darkBrush, control.ClientRectangle);
g.Dispose();

I have a Label control (on the Panel) that I want to
display transparent and thus show the Panel's gradient. I
have tried all the suggestions made in other posts
including:
Setting the BackColor of the Label to Transparent
Setting the Visible property of the Label to False and
then to True in code following the Paint event
Set the Label's Parent to the Panel in code

It doesn't seem to pick up what is drawn in the Paint
event, instead relying on the BackColor of the Panel set
at design time.

Any ideas?

Thanks,

Mark Roebke
RTP
 
Maybe you could use DrawString to draw the text rather than use a label.
Check it out in the help.

Matt
 
Controls aren't really transparent. They just find what is on the parent
background color and fill themselves with that. If the background is a
picture, it works out the particular bit of picture and uses that.

To make a control truly transparent you must override the CreateParams
property and modify the window style like so:

protected override CreateParams CreateParams

{

get

{

CreateParams cp = base.CreateParams;

cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

return cp;

}

}



Then you need to set the control's BackColor to Color.Transparent in the
constructor.



Note that using the automatic double buffering on the control destroys the
effect so animated controls can flicker a bit using this technique.



--
Bob Powell [MVP]
C#, System.Drawing

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm

Get the NEW must-have UI component. The .NET RectTracker
Enables CRectTracker like functionality for WindowsForms.
User defined layouts are a breeze.
http://www.bobpowell.net/recttracker.htm
 
Thanks Bob, that worked perfectly!
-----Original Message-----
Controls aren't really transparent. They just find what is on the parent
background color and fill themselves with that. If the background is a
picture, it works out the particular bit of picture and uses that.

To make a control truly transparent you must override the CreateParams
property and modify the window style like so:

protected override CreateParams CreateParams

{

get

{

CreateParams cp = base.CreateParams;

cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

return cp;

}

}



Then you need to set the control's BackColor to Color.Transparent in the
constructor.



Note that using the automatic double buffering on the control destroys the
effect so animated controls can flicker a bit using this technique.



--
Bob Powell [MVP]
C#, System.Drawing

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm

Get the NEW must-have UI component. The .NET RectTracker
Enables CRectTracker like functionality for WindowsForms.
User defined layouts are a breeze.
http://www.bobpowell.net/recttracker.htm




.
 
Hi Bob,

I have a panel which has to be downloaded on the web browser. I've
created a user control and added this panel into it. When I create
override the CreateParams in my panel, nothing gets downloaded on the
browser. Do I need to do something different for the web.

Thanks,
Nitin
 
Back
Top