using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.XtraEditors; using System.Drawing.Drawing2D; namespace ProjectBase.Controls { public partial class PanelEnhanced : DevExpress.XtraEditors.XtraUserControl { private Color _borderColor; private int _borderWidth = 1; private DashStyle _borderStyle = DashStyle.Solid; private int _opacity = 125; public PanelEnhanced() { InitializeComponent(); } #region Property [Category("Custom"), Description("Border Color")] public Color BorderColor { set { _borderColor = value; } get { return _borderColor; } } [Category("Custom"), Description("Border Width"), DefaultValue(1)] public int BorderWidth { set { if (value < 0) value = 0; _borderWidth = value; } get { return _borderWidth; } } [Category("Custom"), Description("Border Style"), DefaultValue(DashStyle.Solid)] public new DashStyle BorderStyle { set { this._borderStyle = value; this.Invalidate(); } get { return this._borderStyle; } } [Bindable(true), Category("Custom"), DefaultValue(125), Description("背景的透明度. 有效值0-255")] public int Opacity { get { return _opacity; } set { if (value > 255) value = 255; else if (value < 0) value = 0; _opacity = value; this.Invalidate(); } } #endregion protected override void OnPaintBackground(PaintEventArgs e) { //base.OnPaintBackground(e); return; } protected override void OnPaint(PaintEventArgs e) { //使用双缓冲 this.DoubleBuffered = true; //背景重绘移动至此 if (this.BackgroundImage != null) { e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height), 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height, System.Drawing.GraphicsUnit.Pixel); } if (this._opacity > 0) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(this._opacity, this.BackColor)), this.ClientRectangle); } if (this._borderWidth > 0) { Pen pen = new Pen(this._borderColor, this._borderWidth); pen.DashStyle = this._borderStyle; e.Graphics.DrawRectangle(pen, e.ClipRectangle.Left, e.ClipRectangle.Top, this.Width - 1, this.Height - 1); pen.Dispose(); } base.OnPaint(e); } } }