Winform圆角按钮的生成

发布于:2018-06-02    浏览: 198    作者:系统管理员

Winform圆角按钮的生成



usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;


namespaceGid_1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void Draw(Rectangle rectangle,Graphics g, int _radius, bool cusp, Color begin_color, Color end_color)

        {

            int span = 2;

            //抗锯齿

            g.SmoothingMode =SmoothingMode.AntiAlias;

            //渐变填充

            LinearGradientBrushmyLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color,end_color, LinearGradientMode.Vertical);

            //画尖角

            if (cusp)

            {

                span = 10;

                PointF p1 = newPointF(rectangle.Width - 12, rectangle.Y + 10);

                PointF p2 = newPointF(rectangle.Width - 12, rectangle.Y + 30);

                PointF p3 = newPointF(rectangle.Width, rectangle.Y + 20);

                PointF[] ptsArray = { p1, p2,p3 };

               g.FillPolygon(myLinearGradientBrush, ptsArray);

            }

            //填充

            g.FillPath(myLinearGradientBrush,DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span,rectangle.Height-1, _radius));

        }

        public static GraphicsPathDrawRoundRect(int x, int y, int width, int height, int radius)

        {

            //四边圆角

            GraphicsPath gp = newGraphicsPath();

            gp.AddArc(x, y, radius, radius,180, 90);

            gp.AddArc(width - radius, y,radius, radius, 270, 90);

            gp.AddArc(width - radius, height -radius, radius, radius, 0, 90);

            gp.AddArc(x, height - radius,radius, radius, 90, 90);

            gp.CloseAllFigures();

            return gp;

        }


        private void panel1_Paint(objectsender, PaintEventArgs e)

        {

            Draw(e.ClipRectangle, e.Graphics,18,true, Color.FromArgb(90, 143, 0), Color.FromArgb(41, 67, 0));

            base.OnPaint(e);

            Graphics g = e.Graphics;

            g.DrawString("其实我是个Panel", new Font("微软雅黑", 9,FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));

        }


        private void panel2_Paint(objectsender, PaintEventArgs e)

        {

            Draw(e.ClipRectangle, e.Graphics,18, false, Color.FromArgb(113, 113, 113), Color.FromArgb(0, 0, 0));

            base.OnPaint(e);

            Graphics g = e.Graphics;

            g.DrawString("其实我是个Panel", new Font("微软雅黑", 9,FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));

        }


        private void button1_Paint(objectsender, PaintEventArgs e)

        {

            Draw(e.ClipRectangle, e.Graphics,18, false, Color.FromArgb(0, 122, 204), Color.FromArgb(8, 39, 57));

            base.OnPaint(e);

            Graphics g = e.Graphics;

            g.DrawString("其实我是个按钮", new Font("微软雅黑", 9,FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));

        }


        private void label1_Paint(objectsender, PaintEventArgs e)

        {

            Draw(e.ClipRectangle, e.Graphics,18, false, Color.FromArgb(210, 210, 210), Color.FromArgb(242, 242, 242));

            base.OnPaint(e);


            Graphics g = e.Graphics;

            g.DrawString("其实我是Label", new Font("微软雅黑", 9,FontStyle.Regular), new SolidBrush(Color.Black), new PointF(10, 10));

        }

    }

}

int_radius 圆的度数


boolcusp 画不画尖角


Colorbegin_color, Color end_color 渐变色的起始和结束颜色


privatevoid Draw(Rectangle rectangle, Graphics g, int _radius, bool cusp, Colorbegin_color, Color end_color)

        {.......}


 


圆角按钮要设置



否则4个角还有颜色。


皆可画圆。

注意 此方法无法完成悬停变色功能

此文源于https://www.cnblogs.com/qiaoke/p/6124693.html(侵删!)

在线客服

售前咨询

售后服务

投诉/建议

服务热线
0731-82880004
18874148081
'); })();