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;
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM