如何添加在图片框C#标签的透明度? [英] How to add label transparency in picturebox C#?

查看:150
本文介绍了如何添加在图片框C#标签的透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建这个程序可以添加标签和图片框。

I create a program in this can add label and picture box.

所有的控制必须是面板的孩子。

All control must is children of a panel.

我用这样的代码:

panel2.Controls.Add(picturebox1);
panel2.Controls.Add(label1);



没错,问题是我要标注在图片框。

Yup, the problem is I want label over picture box.

我是被陷害的代码:

label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;



更新:

Update:

由于控制只会造成当我想通过button_event创建。就像,创建图片框,创建标签文本。这是不是被创造之前,我想使用它们。

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:

推荐答案

透明度确实为嵌套控制工作;但它的不支持下的WinForms控件重叠即可。期。

Transparency does work for nested controls; but it is not supported for overlapping controls under winforms. Period.

您可以尝试使用要解决的两个标签,嵌套在面板的之一的的PB,在其他在PB

You could try to workaround by using two labels, one nested in the panel under the pb, the other in the pb.

下面是一个例子:

在这里输入的形象描述

    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 );



..或放弃它,因为它负责清理先前覆盖,其被存储在在标签标签的..:

 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);
}

所有你需要的是计算两个位置 PT1 PT2 。如果您是Picturbox拉伸或缩放您还需要编写源矩形到的DrawImage 重载,可以放大/拉伸图像。

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.

要强制显示来电 panel2.Invalidate 每当有新的变化。

To enforce the display call panel2.Invalidatewhenever something changes..

更简单和更强大的,除非你需要的特殊能力标签图片框 ..

Simpler and much more powerful, unless you need special abilities of Label or PictureBox..

请注意所有的东西孤单在代码中发生的事情,所以不要指望事情在设计中出现。编写代码,以便它在VS设计师展示的是不那么容易。

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

这篇关于如何添加在图片框C#标签的透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆