我想在我的表单中添加一个标签,我希望它没有任何颜色 - 我希望它的文字可见,我在标签的属性中找不到这个选项,有人可以帮我吗?
答案 0 :(得分:28)
你想让标签(文字除外)透明吗? Windows Forms(我假设WinForms - 这是真的)并不真正支持透明度。有时,最简单的方法是将Label的Backcolor变为透明。
label1.BackColor = System.Drawing.Color.Transparent;
但是你会遇到问题,因为WinForms确实没有正确支持透明度。否则,请看这里:
http://www.doogal.co.uk/transparent.php
http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx
http://www.daniweb.com/code/snippet216425.html
Setting the parent of a usercontrol prevents it from being transparent
祝你好运!答案 1 :(得分:3)
this.label1.BackColor = System.Drawing.Color.Transparent;
答案 2 :(得分:2)
如果您在后台显示图片框,则使用此:
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
将此代码放在InitializeComponent();
下方或Form_Load
方法中。
参考:https://www.c-sharpcorner.com/blogs/how-to-make-a-transparent-label-over-a-picturebox1
答案 3 :(得分:0)
你是对的。但这是使标签的背景色透明的最简单方法 在该标签的属性窗口中,选择 Web 。在Web中,选择透明 :)
答案 4 :(得分:0)
通常,出现在图像前面的标签和文本框最好组织在面板中。渲染时,如果标签需要对面板内的图像透明,您可以像这样在表单初始化中切换到图像作为标签的父级:
var oldParent = panel1;
var newParent = pictureBox1;
foreach (var label in oldParent.Controls.OfType<Label>())
{
label.Location = newParent.PointToClient(label.Parent.PointToScreen(label.Location));
label.Parent = newParent;
label.BackColor = Color.Transparent;
}
答案 5 :(得分:0)
这使用了 Graphics.CopyFromScreen,因此需要在它在屏幕上可见时添加控件。
public partial class TransparentLabelControl : Label
{
public TransparentLabelControl()
{
this.AutoSize = true;
this.Visible = false;
this.ImageAlign = ContentAlignment.TopLeft;
this.Visible = true;
this.Resize += TransparentLabelControl_Resize;
this.LocationChanged += TransparentLabelControl_LocationChanged;
this.TextChanged += TransparentLabelControl_TextChanged;
this.ParentChanged += TransparentLabelControl_ParentChanged;
}
#region Events
private void TransparentLabelControl_ParentChanged(object sender, EventArgs e)
{
SetTransparent();
if (this.Parent != null)
{
this.Parent.ControlAdded += Parent_ControlAdded;
this.Parent.ControlRemoved += Parent_ControlRemoved;
}
}
private void Parent_ControlRemoved(object sender, ControlEventArgs e)
{
SetTransparent();
}
private void Parent_ControlAdded(object sender, ControlEventArgs e)
{
if (this.Bounds.IntersectsWith(e.Control.Bounds))
{
SetTransparent();
}
}
private void TransparentLabelControl_TextChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_LocationChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_Resize(object sender, EventArgs e)
{
SetTransparent();
}
#endregion
public void SetTransparent()
{
if (this.Parent!= null)
{
this.Visible = false;
this.Image = this.takeComponentScreenShot(this.Parent);
this.Visible = true;
}
}
private Bitmap takeComponentScreenShot(Control control)
{
Rectangle rect = control.RectangleToScreen(this.Bounds);
if (rect.Width == 0 || rect.Height == 0)
{
return null;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
return bmp;
}
}