如何在C#中使标签的背景透明 [英] How to make the background of a label transparent in c#

查看:336
本文介绍了如何在C#中使标签的背景透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格如下图所示.

我想通过label2查看label1和label3. (我只想只看到label2的边框).我已将label2中的BackColor更改为Transparent.但是结果和上面的图片一样.

I want to see label1 and label3 through label2. (I just want to see only the border of the label2). I have changed the BackColor in label2 to Transparent. But the result is same like the above picture.

推荐答案

在Windows Forms中,您不能直接执行此操作.您可以使用BackgroundImage.

In Windows Forms you can't do this directly. You can work with 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中:

and in Form_Load:

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

您会看到以下结果:

这篇关于如何在C#中使标签的背景透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆