当usercontrol背景设置透明时,子控件不可见

时间:2014-01-10 09:27:54

标签: c# .net winforms

我在Windows窗体应用程序中创建一个用户控件,如下所示:

enter image description here

我想让用户控件的唯一backColor / background是透明的,而不是它上面的控件。例如,这里栗色T形应该是原样但背景(即灰色)应该是透明的。

我在用户控件的构造函数中尝试了以下代码,但它不起作用。

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;

我已经通过了许多链接,但没有一个可以使用。

以下是usercontrol的代码。我使用了2个LineShape控件来绘制T形并在控件调整大小时更改它。我需要在Windows窗体中使用此控件,但具有透明背景。

public partial class UserControl1: UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;
    }

    private void UserControl1_Resize(object sender, EventArgs e)
    {
        this.lineShape1.StartPoint = new Point((int)(this.Width / 2), 2);
        this.lineShape1.EndPoint = new Point((int)(this.Width / 2), this.Height - 2);
        this.lineShape1.Update();

        this.lineShape2.StartPoint = new Point(1, 1);
        this.lineShape2.EndPoint = new Point(this.Width - 1, 1);
        this.lineShape2.Update();
        this.Refresh();
    }
}

我需要在windows窗体中使用此控件,其中winform具有不同的背景颜色,因此usercontrol的backcolor / background必须是透明的。

0 个答案:

没有答案
相关问题