如何在c#中使标签的背景透明

我的表格如下图所示。

在此处输入图像描述

我想看看label1和label3到label2。 (我只想看到label2的边框)。 我已将label2中的BackColor更改为Transparent。 但结果与上图相同。

在Windows窗体中,您无法直接执行此操作。 您可以使用BackgroundImage

试试这个:

 void TransparetBackground(Control C) { C.Visible = false; C.Refresh(); Application.DoEvents(); Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle); int titleHeight = screenRectangle.Top - this.Top; int Right = screenRectangle.Left - this.Left; Bitmap bmp = new Bitmap(this.Width, this.Height); this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height)); Bitmap bmpImage = new Bitmap(bmp); bmp = bmpImage.Clone(new Rectangle(C.Location.X+Right, C.Location.Y + titleHeight, C.Width, C.Height), bmpImage.PixelFormat); C.BackgroundImage = bmp; C.Visible = true; } 

并在Form_Load中:

 private void Form1_Load(object sender, EventArgs e) { TransparetBackground(label2); } 

你可以看到这个结果:

在此处输入图像描述