C# 透明控制图片框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9387267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent control over PictureBox
提问by Derezzed
In my C# Form I have a Label that displays a download percentage in the download event:
在我的 C# 表单中,我有一个在下载事件中显示下载百分比的标签:
this.lblprg.Text = overallpercent.ToString("#0") + "%";
The Label control's BackColor property is set to be transparent and I want it to be displayed over a PictureBox. But that doesn't appear to work correctly, I see a gray background, it doesn't look transparent on top of the picture box. How can I fix this?
Label 控件的 BackColor 属性设置为透明,我希望它显示在 PictureBox 上。但这似乎无法正常工作,我看到灰色背景,它在图片框顶部看起来不透明。我怎样才能解决这个问题?
采纳答案by Hans Passant
The Label control supports transparency well. It is just that the designer won't let you place the label correctly. The PictureBox control is not a container control so the Form becomes the parent of the label. So you see the form's background.
Label 控件很好地支持透明度。只是设计师不会让你正确放置标签。PictureBox 控件不是容器控件,因此 Form 成为标签的父级。所以你看到了表单的背景。
It is easy to fix by adding a bit of code to the form constructor. You'll need to change the label's Parent property and recalculate it's Location since it is now relative to the picture box instead of the form. Like this:
通过向表单构造函数添加一些代码很容易修复。您需要更改标签的 Parent 属性并重新计算它的位置,因为它现在相对于图片框而不是表单。像这样:
public Form1() {
InitializeComponent();
var pos = this.PointToScreen(label1.Location);
pos = pictureBox1.PointToClient(pos);
label1.Parent = pictureBox1;
label1.Location = pos;
label1.BackColor = Color.Transparent;
}
Looks like this at runtime:
在运行时看起来像这样:


Another approach is to solve the design-time problem. That just takes an attribute. Add a reference to System.Design and add a class to your project, paste this code:
另一种方法是解决设计时问题。那只需要一个属性。添加对 System.Design 的引用并向您的项目添加一个类,粘贴以下代码:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design; // Add reference to System.Design
[Designer(typeof(ParentControlDesigner))]
class PictureContainer : PictureBox {}
回答by Maciej
You can draw text using TextRenderer which will draw it without background:
您可以使用 TextRenderer 绘制文本,它将在没有背景的情况下绘制文本:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics,
overallpercent.ToString("#0") + "%",
this.Font,
new Point(10, 10),
Color.Red);
}
When overallpercent value changes, refresh pictureBox:
当overallpercent值改变时,刷新pictureBox:
pictureBox1.Refresh();
You can also use Graphics.DrawString but TextRenderer.DrawText (using GDI) is faster than DrawString (GDI+)
您也可以使用 Graphics.DrawString 但 TextRenderer.DrawText(使用 GDI)比 DrawString (GDI+) 快
回答by crdy
You can just use
你可以使用
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239
回答by Grey Wolf
For easy for your design. You can place your label inside a panel. and set background image of panel is what every image you want. set label background is transparent
便于您的设计。您可以将标签放置在面板内。并设置面板的背景图像是您想要的每个图像。设置标签背景透明
回答by Andrei Magic
Using Visual Studio with Windows Form you may apply transparency to labels or other elements by adding using System.Drawing;into Form1.Designer.csThis way you will have Transparency available from the Properties panel ( in Appearance at BackColor ). Or just edit code in Designer.cs this.label1.BackColor = System.Drawing.Color.Transparent;
使用带有 Windows 窗体的 Visual Studio,您可以通过使用 System.Drawing添加来将透明度应用于标签或其他元素;进入Form1.Designer.cs这样您就可以从“属性”面板(在 BackColor 的外观中)获得透明度。或者只是在 Designer.cs 中编辑代码this.label1.BackColor = System.Drawing.Color.Transparent;
回答by Stefan Pintilie
One way which works for everything, but you need to handle the position, on resize, on move etc.. is using a transparent form:
一种适用于所有情况但您需要处理位置、调整大小、移动等的方法是使用透明表单:
Form form = new Form();
form.FormBorderStyle = FormBorderStyle.None;
form.BackColor = Color.Black;
form.TransparencyKey = Color.Black;
form.Owner = this;
form.Controls.Add(new Label() { Text = "Hello", Left = 0, Top = 0, Font = new Font(FontFamily.GenericSerif, 20), ForeColor = Color.White });
form.Show();
回答by Falcon
After trying most of the provided solutions without success, the following worked for me:
在尝试了大多数提供的解决方案但没有成功后,以下对我有用:
label1.FlatStyle = FlatStyle.Standard
label1.Parent = pictureBox1
label1.BackColor = Color.Transparent

