winfom自定义一个椭圆按钮

前言

我们在上位机软件开发中,有时候需要制作椭圆按钮,本文就来给大家讲解如何制作一个椭圆按钮。

1、代码

 class SelfControl : Button
    {

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Brush brush = new SolidBrush(EllipseColor);//指定画刷的颜色
            e.Graphics.FillEllipse(brush, new Rectangle(0, 0, this.Width, this.Height));//填充一个圆

            // 绘制文字(居中)
            StringFormat sf = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, sf);
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddEllipse( new Rectangle(0, 0, this.Width, this.Height));
            this.Region = new Region(path);

        }

        private Color _EllipseColor = Color.Red;
        [CategoryAttribute("自定义属性"), DescriptionAttribute("椭圆背景色"), ReadOnly(false)]
        public Color EllipseColor
        {
            get
            {
                return _EllipseColor;
            }
            set
            {
                if (_EllipseColor != value)
                {
                    _EllipseColor = value;
                    this.Invalidate();
                }
            }
        }
    }

2、效果

在这里插入图片描述

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上位机马工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值