透明控件代码
public partial class RoundButton : Control
{
public RoundButton()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawRectangle(Pens.DarkCyan, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
using (SolidBrush sb = new SolidBrush(this.ForeColor))
{
SizeF se = g.MeasureString(this.Text, this.Font);
g.DrawString(this.Text, this.Font, sb, (this.Width - se.Width) / 2, (this.Height - se.Height)/2);
}
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs e) //不画背景
{
base.OnPaintBackground(e);
}
protected override CreateParams CreateParams
{
get
{
CreateParams para = base.CreateParams;
para.ExStyle |= 0x20; //WS_EX_TRANSPARENT 透明支持
return para;
}
}
}
拖动图片时,透明控件会消失,静止才出现。