日期:2014-05-18 浏览次数:20796 次
namespace 启动系统服务 { [ToolboxBitmap(typeof(PanelAlphaEx))] public partial class PanelAlphaEx : UserControl { public PanelAlphaEx() { InitializeComponent(); this.SetStyle(ControlStyles.Opaque, true); this.CreateControl(); this.SetStyle(ControlStyles.ResizeRedraw, false); } #region 透明配置 // 属性设置 private bool _transparentBG = true; private int _alpha = 125; [Category("透明层设置"), Description("是否使用透明,默认为True")] public bool TransparentBG { get { return _transparentBG; } set { _transparentBG = value; } } [Category("透明层设置"), Description("设置透明度")] public int Alpha { get { return _alpha; } set { _alpha = value; } } #endregion /// <summary> /// 开启 WS_EX_TRANSPARENT,使控件支持透明 /// </summary> protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; //cp.ExStyle |= 0x20; cp.ExStyle |= 0x00000020; return cp; } } protected override void OnPaint(PaintEventArgs e) { Color drawColor = _transparentBG ? Color.FromArgb(this._alpha, this.BackColor) : this.BackColor; Pen penBorder = new Pen(drawColor, 0); SolidBrush brushBgColor = new SolidBrush(drawColor); e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; e.Graphics.DrawRectangle(penBorder, e.ClipRectangle); e.Graphics.FillRectangle(brushBgColor, e.ClipRectangle); base.OnPaint(e); } } }