Reputation: 21
I create a program in this can add label and picture box.
All control must is children of a panel.
I use code like this:
panel2.Controls.Add(picturebox1);
panel2.Controls.Add(label1);
Yup, the problem is I want label over picture box.
I was set with code:
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
Update:
Because control only creates when I want to create by button_event. Like, create picture box, create label text. This is not was created before I want to use them.
My code to create this control:
public PictureBox ctrl = new PictureBox();
public void btnAddLogo_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int randNumber = rnd.Next(1, 1000);
String picName = "Pic_" + randNumber;
ctrl.Location = new Point(200, 170);
ctrl.Size = new System.Drawing.Size(100, 60);
ctrl.Name = picName;
ctrl.BackgroundImageLayout = ImageLayout.Zoom;
ctrl.Font = new System.Drawing.Font("NativePrinterFontA", 10F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte) (0)));
ctrl.BackColor = Color.Chocolate;
panel2.Controls.Add(ctrl);
}
private Label ctrLabel = new Label();
public void btnAddCharacter_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int randNumber = rnd.Next(1, 1000);
String LableName = "Lbl_" + randNumber;
ctrLabel.Name = LableName;
ctrLabel.AutoSize = true;
ctrLabel.Text = txtIDImg.Text;
ctrLabel.BackColor = Color.Transparent;
panel2.Controls.Add(ctrLabel);
}
But result shows like:
Upvotes: 2
Views: 2369
Reputation: 54433
Transparency does work for nested controls; but it is not supported for overlapping controls under winforms. Period.
You could try to workaround by using two labels, one nested in the panel under the pb, the other in the pb.
Here is an example:
Label l1 = new Label() { Text = "Hello World", BackColor = Color.Transparent };
Label l2 = new Label() { Text = "Hello World", BackColor = Color.Transparent };
l1.Parent = scrollPanel;
l2.Parent = picBox;
Point pt = new Point(picBox.Right - 30, 30);
l1.Location = pt;
pt.Offset(-picBox.Left, -picBox.Top);
l2.Location = pt;
The above code could also be put into a reusable function:
Label overlayLabel(Label source, Control target)
{
Label old = source.Tag as Label;
if (old != null && old.Parent == target) target.Controls.Remove(old);
Label lbl = new Label();
// copy all necessary properties here:
lbl.Text = source.Text;
lbl.Font = source.Font;
lbl.AutoSize = source.AutoSize;
lbl.Size = source.Size;
lbl.Anchor = source.Anchor; // may work or not!
lbl.BackColor= source.BackColor;
lbl.ForeColor = source.ForeColor;
// etc..
Point pt = source.Location;
pt.Offset(-target.Left , -target.Top);
lbl.Location = pt;
lbl.Parent = target;
source.Tag = lbl;
return lbl;
}
In your code you would call it maybe like this; you can store the returned reference:
panel2.Controls.Add(ctrLabel);
Label ctrLabelOverlay = overlayLabel(ctrLabel, ctrl );
..or discard it, as it takes care of cleaning up the previous overlay, which is stored in the Tag
of the Label
..:
panel2.Controls.Add(ctrLabel);
overlayLabel(ctrLabel, ctrl );
But the most direct way is to draw those things, ie the text and the image yourself. Two or so lines in the panel's Paint
event..:
if (img != null)
{
Rectangle rect = new Rectangle(pt1, img.Size);
e.Graphics.DrawImage(img, rect);
e.Graphics.DrawString("Hello World", Font, Brushes.Black, pt2);
}
All you need is to calculate the two locations pt1
and pt2
. If your Picturbox is stretching or zooming you will also need to write the source rectangle into the DrawImage
overload that can zoom/stretch the image.
To enforce the display call panel2.Invalidate
whenever something changes..
Simpler and much more powerful, unless you need special abilities of Label
or PictureBox
..
Note all theres things are happening in code, so don't expect things to show up in the designer. Writing code so that it does show in the VS designer is not all that easy..
Upvotes: 3
Reputation: 364
Windows forms does not support true transparency, Go through this. http://www.broculos.net/2008/03/how-to-use-transparent-images-and.html#.Vq0q2jZuncc
Upvotes: 0