在我的C#表单中,我有一个标签,在下载事件中显示下载百分比:
this.lblprg.Text = overallpercent.ToString("#0") + "%";
Label控件的BackColor属性设置为透明,我希望它显示在PictureBox上。但是这看起来没有正常工作,我看到灰色的背景,它在图片框的顶部看起来并不透明。我该如何解决这个问题?
答案 0 :(得分:149)
Label控件很好地支持透明度。只是设计师不会让你正确放置标签。 PictureBox控件不是容器控件,因此Form成为标签的父级。所以你看到了表格的背景。
通过向表单构造函数添加一些代码很容易修复。您需要更改标签的Parent属性并重新计算它的位置,因为它现在相对于图片框而不是表单。像这样:
public Form1() {
InitializeComponent();
var pos = this.PointToScreen(label1.Location);
pos = pictureBox1.PointToClient(pos);
label1.Parent = pictureBox1;
label1.Location = pos;
label1.BackColor = Color.Transparent;
}
在运行时看起来像这样:
另一种方法是解决设计时问题。这只是一个属性。添加对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 {}
答案 1 :(得分:36)
你可以使用
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent; // You can also set this in the designer, as stated by ElDoRado1239
答案 2 :(得分:9)
您可以使用TextRenderer绘制文本,它将在没有背景的情况下绘制文本:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics,
overallpercent.ToString("#0") + "%",
this.Font,
new Point(10, 10),
Color.Red);
}
当totalpercent值更改时,刷新pictureBox:
pictureBox1.Refresh();
您也可以使用Graphics.DrawString但TextRenderer.DrawText(使用GDI)比DrawString(GDI +)更快
答案 3 :(得分:5)
方便您的设计。 您可以将标签放在面板内。并设置面板的背景图像是你想要的每个图像。设置标签背景是透明的
答案 4 :(得分:1)
尝试大多数提供的解决方案都没有成功之后,以下对我有用:
label1.FlatStyle = FlatStyle.Standard
label1.Parent = pictureBox1
label1.BackColor = Color.Transparent
答案 5 :(得分:0)
一种适用于所有事物的方法,但是您需要处理位置,调整大小,移动等。
{
"test" : [
{
"dozzs":
{
"doz": {
"-type": "Person",
"-key": "125"
}
}
},
{
"dozzs": [
{
"doz": {
"-type": "Person",
"-key": "123"
}
},
{
"doz": {
"-type": "Person",
"-key": "124,"
}
}
]
}
]
}
答案 6 :(得分:-2)
将Visual Studio与Windows窗体一起使用,可以通过将使用System.Drawing; 添加到 Form1.Designer.cs 中,将透明度应用于标签或其他元素。这样您将拥有“属性”面板中的透明度(在BackColor中的外观中)。或者只是编辑Designer.cs中的代码 this.label1.BackColor = System.Drawing.Color.Transparent;