private void UserControl1_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Opaque, false);
this.BackColor = Color.Transparent;
}
我这么设置了都不能透明
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.Opaque, false);
this.BackColor = Color.Transparent;
}
我这么设置了都不能透明
{
public UserControl1()
{
InitializeComponent();
} private void UserControl1_Load(object sender, EventArgs e)
{ }
}
将自定义控件继承Form的基类就可以有Opacity属性 就可以让控件透明
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.Selectable |
ControlStyles.ResizeRedraw, true);
public partial class IUControl: UserControl
{
public IUControl()
{
InitializeComponent();
SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.Selectable |
ControlStyles.ResizeRedraw, true);
}
}
应用控件的页面代码 this.iuControl= new IUControl();
this.Controls.Add(this.iuControl);
this.iuControl.AutoSize = true;
this.iuControl.Location = new System.Drawing.Point(65, 193);
this.iuControl.Name = "iuCompass";
this.iuControl.Size = new System.Drawing.Size(250,250);
this.iuControl.Text = "我的控件"; iuControl.BackColor = System.Drawing.Color.Transparent;
但运行iuControl背景还是灰色的!
this.BackColor = Color.Transparent; //这条语句加在控件代码中
this.AllowTransparency = true; //这句代码加到form的代码里
Get
Dim cParms As CreateParams = MyBase.CreateParams
cParms.ExStyle = cParms.ExStyle Or &H80000
' WS_EX_LAYERED
Return cParms
End Get
End Property