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

时间:2014-09-14 04:31:17

标签: c# label labels

我的表格如下图所示。

enter image description here

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

1 个答案:

答案 0 :(得分:3)

在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);
}

你可以看到这个结果:

enter image description here

相关问题