Winform绘制圆角按钮

最近做Winfrom项目,Winfrom不同于WPF,界面效果没有WPF那么好做,圆角按钮只能靠出背景图,但是总是出背景图也是有局限性的,所以研究下绘制Button来实现圆角效果

百度了很多例子,比较好的是winfrom圆角Button,但是实现后发现边缘毛刺太严重了,效果很不好

上面的效果

于是进行了优化,代码如下

public partial class ShapeButton : Button
    {
        private Color m_CustomClickColor;
        private Color m_CustomDisabledColor;
        private Color m_CustomDefaultColor;
        private Color m_CustomClickTextColor;
        private Color m_CustomDisabledTextColor;
        private Color m_CustomDefaultTextColor;
        private int m_Fillet;

        public ShapeButton()
        {
            InitializeComponent();

            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.Cursor = System.Windows.Forms.Cursors.Hand;
            this.MouseEnter += new EventHandler(UserControlRoundButton_MouseEnter);
            this.MouseLeave += new EventHandler(UserControlRoundButton_MouseLeave);
            this.EnabledChanged += new EventHandler(UserControlCustomButton_EnabledChanged);

        }

        /// 
        ///  不可用背景颜色
        /// 
        public Color CustomDisabledColor
        {
            get
            {
                return m_CustomDisabledColor;
            }
            set
            {
                m_CustomDisabledColor = value;
            }
        }

        /// 
        /// 默认背景颜色
        /// 
        [Browsable(true)]
        public Color CustomDefaultColor
        {
            get
            {
                return m_CustomDefaultColor;
            }
            set
            {
                m_CustomDefaultColor = value;
            }
        }

        /// 
        /// 光标放上去时颜色
        /// 
        [Browsable(true)]
        public Color CustomClickColor
        {
            get
            {
                return m_CustomClickColor;
            }
            set
            {
                m_CustomClickColor = value;
            }
        }


        /// 
        ///  不可用文字颜色
        /// 
        [Browsable(true)]
        public Color CustomDisabledTextColor
        {
            get
            {
                return m_CustomDisabledTextColor;
            }
            set
            {
                m_CustomDisabledTextColor = value;
            }
        }

        /// 
        /// 默认文字颜色
        /// 
        [Browsable(true)]
        public Color CustomDefaultTextColor
        {
            get
            {
                return m_CustomDefaultTextColor;
            }
            set
            {
                m_CustomDefaultTextColor = value;
            }
        }

        /// 
        /// 光标放上去时文字颜色
        /// 
        [Browsable(true)]
        public Color CustomClickTextColor
        {
            get
            {
                return m_CustomClickTextColor;
            }
            set
            {
                m_CustomClickTextColor = value;
            }
        }


        /// 
        /// 圆角度数、
        /// 
        [Browsable(true)]
        public int Fillet
        {
            get
            {
                return m_Fillet;
            }
            set
            {
                m_Fillet = value;
            }
        }

        /// 
        /// 按钮不可用
        /// 
        /// 
        /// 
        void UserControlCustomButton_EnabledChanged(object sender, EventArgs e)
        {
            if (!this.Enabled)
            {
                this.BackColor = m_CustomDisabledColor;
                this.ForeColor = m_CustomDisabledTextColor;
            }
            else{
                this.BackColor = m_CustomDefaultColor;
                this.ForeColor = m_CustomDefaultTextColor;
            }
        }

        /// 
        /// 鼠标离开
        /// 
        /// 
        /// 
        void UserControlRoundButton_MouseLeave(object sender, EventArgs e)
        {
            if (!this.Enabled)
                return;

            this.BackColor = m_CustomDefaultColor;
            this.ForeColor = m_CustomDefaultTextColor;
        }

        /// 
        /// 鼠标进入
        /// 
        /// 
        /// 
        void UserControlRoundButton_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = m_CustomClickColor;
            this.ForeColor = m_CustomClickTextColor;
            this.ResetFlagsandPaint();
        }

        /// 
        /// 绘制的路径
        /// 
        /// 
        /// 
        /// 
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            path.AddArc(arcRect, 180, 90);
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }

        /// 
        /// 绘制的代码,需要在设计页面声明事件
        /// 
        /// 
        /// 
        private void ShapeButton_Paint(object sender, PaintEventArgs e)
        {

            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);//this.Left-10,this.Top-10,this.Width-10,this.Height-10);                 
            e.Graphics.Clear(Color.White);
            FormPath = GetRoundedRectPath(rect, m_Fillet);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillPath(new SolidBrush(this.BackColor), FormPath);

            StringFormat gs = new StringFormat();
            gs.Alignment = StringAlignment.Center; //居中
            gs.LineAlignment = StringAlignment.Center;//垂直居中
           // gs.Alignment = StringAlignment.Far; //右对齐
            string str = "Hello World";
            Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
            Font fo = new Font("宋体", 10.5F);
            Brush brush = new SolidBrush(this.ForeColor);
            e.Graphics.DrawString(str, fo, brush, rc, gs);

        }
    }

需要在自定义按钮设计页面中注册事件
拖拽到窗体上设置好属性就可以使用了
最终效果

生命不息,代码不止!码农一枚,请多点赞

你可能感兴趣的:(Winform绘制圆角按钮)