1. 程式人生 > 其它 >Winform繪製圓角矩形控制元件

Winform繪製圓角矩形控制元件

1、選擇自定義控制元件

2、

public partial class UCirleButton : Button
    {
        public UCirleButton()
        {
            InitializeComponent();
        }
        private Color bgColor = Color.Gray;
        [DefaultValue(typeof(Color), "Gray"), Description("控制元件顏色")]
        public Color BgColor
        {
            
get { return bgColor; } set { bgColor = value; Invalidate(); } } private Color bgColor2 = Color.Transparent; [DefaultValue(typeof(Color), "Transparent"), Description("控制元件顏色2")] public Color BgColor2 { get { return bgColor2; }
set { bgColor2 = value; Invalidate(); } } private Color borderColor = Color.Red; [DefaultValue(typeof(Color), "gray"), Description("控制元件邊框顏色")] public Color BorderColor { get { return borderColor; } set { borderColor = value; Invalidate(); } }
private int borderWidth = 0; [DefaultValue(typeof(int), "0"), Description("控制元件邊框粗細")] public int BorderWidth { get { return borderWidth; } set { borderWidth = value; Invalidate(); } } private int radius = 5; [DefaultValue(typeof(int), "0"), Description("控制元件邊框角度")] public int Radius { get { return radius; } set { radius = value; Invalidate(); } } private LinearGradientMode gradientMode = LinearGradientMode.Vertical; [DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("漸變方式")] public LinearGradientMode GradientMode { get { return gradientMode; } set { gradientMode = value; Invalidate(); } } private const int WM_PAINT = 0xf; protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT) { //重繪邊框 if (this.radius > 0) { using (Graphics gp = Graphics.FromHwnd(this.Handle)) { //消除鋸齒 gp.SmoothingMode = SmoothingMode.AntiAlias; Rectangle r = new Rectangle(); r.Width = this.Width; r.Height = this.Height; //繪製邊框和物件 DrawBorder(gp, r, this.radius); } } } } private void DrawBorder(Graphics gp, Rectangle rect, int radius) { rect.Width -= 1; rect.Height -= 1; GraphicsPath path = new GraphicsPath(); //繪製圓角矩形路徑 path = PaintCommon.GetRoundRectangle(rect, radius); if (BorderWidth > 0) { using (Pen pen = new Pen(this.borderColor, BorderWidth)) { //繪製邊框 gp.DrawPath(pen, path); } } //背景區域的矩形 Rectangle rect1 = new Rectangle(rect.X + BorderWidth, rect.Y + BorderWidth, rect.Width - 2 * BorderWidth, rect.Height - 2 * BorderWidth); //繪製背景 DrawBackCorol(gp, rect1, this.radius); //繪製按鈕文字 StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; gp.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), rect1, format); } /// <summary> /// 繪製背景 /// </summary> /// <param name="gp"></param> /// <param name="rect"></param> /// <param name="radius"></param> private void DrawBackCorol(Graphics gp, Rectangle rect, int radius) { //獲取背景區域圓角矩形 GraphicsPath path = PaintCommon.GetRoundRectangle(rect, radius); if (this.BgColor2 != Color.Transparent) { //線性漸變畫刷 LinearGradientBrush bush = new LinearGradientBrush(rect, BgColor, BgColor2, gradientMode); //填充圓角矩形內部 gp.FillPath(bush, path); } else { Brush b = new SolidBrush(BgColor); gp.FillPath(b, path); } } }
  public class PaintCommon
    {
        public static GraphicsPath GetRoundRectangle(Rectangle rectangle,int r)
        {
            int l = 2 * r;
            //把圓角矩形分成8段直線、弧的組合、一次加到路徑中
            GraphicsPath gp = new GraphicsPath();
            //上面的直線
            gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
            //右上角圓角的弧度
            gp.AddArc   (new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);

            //右邊豎線
            gp.AddLine(new Point(rectangle.Right , rectangle.Y+r), new Point(rectangle.Right, rectangle.Bottom-r));
            //右下角的弧度
            gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom-l, l, l), 0F, 90F);

            gp.AddLine(new Point(rectangle.Right-r, rectangle.Bottom), new Point(rectangle.X+r, rectangle.Bottom ));
            gp.AddArc(new Rectangle(rectangle.X , rectangle.Bottom-l,l,l ), 90F, 90F);

            gp.AddLine(new Point(rectangle.X , rectangle.Bottom-r), new Point(rectangle.X , rectangle.Y+r));
            gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);

            return gp;
        }
    }