Transparent label on picturebox

T

Tim Johnson

Can someone point me in the right direction for this one? I have a
"background" watermark image in a picturebox and I'd like to put labels (at
least) on it such that the image shows thru. I know labels in CF don't have
the transparent property, but I also know you can't make a picturebox the
parent of other controls either. I'm not clear on what to do in an OnPaint
override (and whose) to accomplish this. Thanks!

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
T

Tim Wilson

If you hook into the Paint event for the PictureBox you will be able to draw
your text directly on top of the image using the DrawString method.
 
T

Tim Johnson

I tried instead creating a custom control for a "TransparentLabel", which
inherits from Control (not Label). I hook its OnPaint to draw just the text
(not exactly what you suggested, but I thought it would be more useful in
the long run).

The code is:
------------------
m_brush = new SolidBrush(Color.Black);
m_font = new Font("Arial", 10, FontStyle.Bold);

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.Transparent);

Size textSize = e.Graphics.MeasureString(this.Text, m_font).ToSize();
e.Graphics.DrawString(Text, m_font, brush, 16, (this.Height -
textSize.Height)/2);
}
-------------------

What I find is that:

- doing a Clear(Color.Transparent) is the same as not doing a clear at all -
I get the background of the parent form, even though I've done a
label.BringToFront to make sure it's on top of the picturebox.

- if I bypass the OnPaintBackground and don't do a Clear, I get the
background of the previous foreground window.

- if I do a Clear(Color.Blue) I obviously get a blue background in the label
box

Is this TransparentLabel approach possible to accomplish letting the
picturebox gradient image peek thru like I want? I can only get form1's
color or form2's color to peek thru.

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
T

Tim Wilson

"Transparency" is really just the back color of the container. This is
referred to as "ambience" - the ability for a control to take on the
appearance of its surroundings (aka container or parent). So everything that
you described is what is expected. You will need to draw directly on top of
your image using the Paint event of the PictureBox. Or, if you want
something that is reusable, then inherit from PictureBox and do all the work
in the OnPaint override.

See the "Remarks" section at the link below for some information on this
matter.
http://msdn.microsoft.com/library/d...temWindowsFormsControlClassBackColorTopic.asp
 
T

Tim Johnson

I ended up doing exactly that, creating a new TranspPictureBox control that
can have any number of transparent labels. What a pain though for making
forms with a watermark background. It makes such a better looking screen
(look at PPC Notes or Contacts for example), you'd think MS would have put
in a simple label.transparent=true property and use the Z-order to determine
what should peek through. I'll have to add checkboxes and radio buttons if
I ever need them since they have a label component too.

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
 
M

Mark Davison

Hi,

Does anyone have any VB.NET examples of how to do this? I have been trying
to figure it out withough success!

Mark
 
R

Ruud van Eeghem

The problem with adding labels to a picturebox is more a designer
problem. The designer keeps adding the label to the form even though the
label is actually placed on the picturebox.

Once I noticed that the picturebox is a container control, I found that
you can simply create the label using the code and then add them to the
picturebox. You can make them transparent by simply setting the Color
property to Color.Transparent. No more, no less.
 
M

Mark Davison

Hi,

I have tried adding the label control to the pictureboxes controls
collection without success.

Dim lbl As New System.Windows.Forms.Label

lbl.BackColor = System.Drawing.Color.Transparent

lbl.Text = "Hello Mark!"

lbl.Left = 20

lbl.Top = 20

lbl.Visible = True

Me.PictureBox5.Controls.Add(lbl)


Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top