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

Lak*_*ndu 2 c# label labels

我的表格如下图所示。

在此处输入图片说明

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

Meh*_*loo 6

在 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;
}
Run Code Online (Sandbox Code Playgroud)

并在 Form_Load 中:

private void Form1_Load(object sender, EventArgs e)
{
    TransparetBackground(label2);
}
Run Code Online (Sandbox Code Playgroud)

你可以看到这个结果:

在此处输入图片说明