winforms 如何做一个标签的背景将没有颜色?

7vhp5slm  于 2023-03-31  发布在  其他
关注(0)|答案(9)|浏览(148)

我想添加一个标签到我的形式,我想它没有任何颜色-我只想它的文本是可见的,我没有找到这个选项在标签的属性,有人可以帮助我吗?

a5g8bdjr

a5g8bdjr1#

你是对的。但这里是使标签的背景色透明的最简单方法。在该标签的属性窗口中选择Web。。在Web中选择透明:)

ykejflvf

ykejflvf2#

this.label1.BackColor = System.Drawing.Color.Transparent;
pbwdgjma

pbwdgjma3#

让我们看看两种可能的情况。
1.背景是一种颜色,在VS构造函数中双击窗体背景,然后编写如下代码:

/*
This code will set all your object's background color to the same as the form.
This should be written in the body of <FormName>_Load(object, EventArgs).
*/
Control[] objs = new Control[] { /* your object list, e. g { myLabel, myPicture } */ };
foreach (Control control in objs) {
  control.BackColor = Color.Transparent;
  // OR
  control.BackColor = this.BackColor;
}
  1. Background是一个PictureBox。这也很简单。我们只需要将所有对象作为PictureBox的子对象,并将其颜色设置为透明。代码:
/*
This code will set all your object's background to transparent and show the PBox.
This should be written in the body of <FormName>_Load(object, EventArgs)'s foreach loop.
Put everything before it the same as in 1st code fragment.
*/
control.Parent = back;
control.BackColor = Color.Transparent;

让我们看看照片。

bis0qfac

bis0qfac4#

通常,出现在图像前面的标签和文本框最好在面板中组织。渲染时,如果标签需要对面板中的图像透明,您可以在表单初始化中切换到图像作为标签的父级,如下所示:

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

bq8i3lrv5#

它使用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;
    }

}
nzrxty8p

nzrxty8p6#

要使标签后面带有图片,一个简单的方法是使用标签本身的Image属性。这将在图片的顶部打印文本,并允许您根据需要对齐图像(上/下/左/右/中心)。picture

np8igboo

np8igboo7#

另一种选择可能是:

label1.BackColor = Color.Empty;
vshtjzan

vshtjzan8#

是否要使标签(除文本外)透明?Windows窗体(我假设是WinForms -这是真的吗)并不真正支持透明。最简单的方法有时是将标签的Backcolor设置为Transparent。

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
设置usercontrol的父级会阻止它透明
祝你好运!

f0brbegy

f0brbegy9#

如果你在背景中的图片框,然后使用这个:

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

相关问题