using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Rs.Controls { public partial class ControlBase : UserControl { public ControlBase() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.UserPaint, true); } /// /// The is radius /// private bool _isRadius = false; /// /// The corner radius /// private int _cornerRadius = 24; /// /// The is show rect /// private bool _isShowRect = false; /// /// The rect color /// private Color _rectColor = Color.FromArgb(220, 220, 220); /// /// The rect width /// private int _rectWidth = 1; /// /// The fill color /// private Color _fillColor = Color.Transparent; /// /// 是否圆角 /// /// true if this instance is radius; otherwise, false. [Description("是否圆角"), Category("自定义")] public virtual bool IsRadius { get { return this._isRadius; } set { this._isRadius = value; Refresh(); } } /// /// 圆角角度 /// /// The coner radius. [Description("圆角角度"), Category("自定义")] public virtual int ConerRadius { get { return this._cornerRadius; } set { this._cornerRadius = Math.Max(value, 1); Refresh(); } } /// /// 是否显示边框 /// /// true if this instance is show rect; otherwise, false. [Description("是否显示边框"), Category("自定义")] public virtual bool IsShowRect { get { return this._isShowRect; } set { this._isShowRect = value; Refresh(); } } /// /// 边框颜色 /// /// The color of the rect. [Description("边框颜色"), Category("自定义")] public virtual Color RectColor { get { return this._rectColor; } set { this._rectColor = value; this.Refresh(); } } /// /// 边框宽度 /// /// The width of the rect. [Description("边框宽度"), Category("自定义")] public virtual int RectWidth { get { return this._rectWidth; } set { this._rectWidth = value; Refresh(); } } /// /// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充 /// /// The color of the fill. [Description("当使用边框时填充颜色,当值为背景色或透明色或空值则不填充"), Category("自定义")] public virtual Color FillColor { get { return this._fillColor; } set { this._fillColor = value; Refresh(); } } /// /// 引发 事件。 /// /// 包含事件数据的 。 protected override void OnPaint(PaintEventArgs e) { if (this.Visible) { e.Graphics.SetGDIHigh(); if (this._isRadius) { this.SetWindowRegion(); } else { //关闭圆角后显示为原矩形 GraphicsPath g = new GraphicsPath(); g.AddRectangle(base.ClientRectangle); g.CloseFigure(); base.Region = new Region(g); } GraphicsPath graphicsPath = new GraphicsPath(); if (this._isShowRect || (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor)) { Rectangle clientRectangle = base.ClientRectangle; if (_isRadius) { graphicsPath.AddArc(0, 0, _cornerRadius, _cornerRadius, 180f, 90f); graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - 1, 0, _cornerRadius, _cornerRadius, 270f, 90f); graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - 1, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 0f, 90f); graphicsPath.AddArc(0, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 90f, 90f); graphicsPath.CloseFigure(); } else { graphicsPath.AddRectangle(clientRectangle); } } e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿 e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; if (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor) e.Graphics.FillPath(new SolidBrush(this._fillColor), graphicsPath); if (this._isShowRect) { Color rectColor = this._rectColor; Pen pen = new Pen(rectColor, (float)this._rectWidth); e.Graphics.DrawPath(pen, graphicsPath); } } base.OnPaint(e); } /// /// Sets the window region. /// private void SetWindowRegion() { GraphicsPath path = new GraphicsPath(); Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height); path = this.GetRoundedRectPath(rect, this._cornerRadius); base.Region = new Region(path); } /// /// Gets the rounded rect path. /// /// The rect. /// The radius. /// GraphicsPath. private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) { Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius)); GraphicsPath graphicsPath = new GraphicsPath(); graphicsPath.AddArc(rect2, 180f, 90f);//左上角 rect2.X = rect.Right - radius; graphicsPath.AddArc(rect2, 270f, 90f);//右上角 rect2.Y = rect.Bottom - radius; rect2.Width += 1; rect2.Height += 1; graphicsPath.AddArc(rect2, 360f, 90f);//右下角 rect2.X = rect.Left; graphicsPath.AddArc(rect2, 90f, 90f);//左下角 graphicsPath.CloseFigure(); return graphicsPath; } /// /// WNDs the proc. /// /// 要处理的 Windows 。 protected override void WndProc(ref Message m) { if (m.Msg != 20) { base.WndProc(ref m); } } } }