C#在工具箱中添加圆形Button

本文介绍了一个自定义的圆形按钮控件实现,该控件在Windows环境下使用C#编写,适用于VS2013及x86平台。文章详细阐述了如何通过继承System.Windows.Forms.Button类来创建一个具有圆形外观的按钮,并提供了对按钮半径、背景图片等属性的自定义设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.运行环境 :Window10 + VS2013

2.运行平台 :x86

3.运行时建立一个class.cs就可以

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace GxSingleCam
{
    class RoundButton : System.Windows.Forms.Button //注意有的环境可以直接写:Button
    {
        private int radius;//半径 

        //圆形按钮的半径属性
        [CategoryAttribute("布局"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
        public int Radius
        {
            set
            {
                radius = value;
                this.Height = this.Width = Radius;
            }
            get
            {
                return radius;
            }
        }

        private Image imageEnter;
        [CategoryAttribute("外观"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
        public Image ImageEnter
        {
            set
            {
                imageEnter = value;
            }
            get
            {
                return imageEnter;
            }
        }

        private Image imageNormal;
        [CategoryAttribute("外观"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
        public Image ImageNormal
        {
            set
            {
                imageNormal = value;
                BackgroundImage = imageNormal;
            }
            get
            {
                return imageNormal;
            }
        }

        //以下代码用于在VS中隐藏BackgroundImage属性,使得只能通过Diameter设置Height和Width
        [BrowsableAttribute(false)]
        public new Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {
                base.BackgroundImage = value;

            }
        }

        //以下代码用于在VS中隐藏Size属性,使得只能通过Diameter设置Height和Width
        [BrowsableAttribute(false)]
        public new Size Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;

            }
        }

        public RoundButton()
        {
            Radius = 64;
            this.Height = this.Width = Radius;
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.BackgroundImage = imageEnter;
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }

        //重写OnPaint
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddEllipse(0, 0, Radius, Radius);
            this.Region = new Region(path);
        }

        //重写OnMouseEnter
        //protected override void OnMouseEnter(EventArgs e)
        //{
        //    Graphics g = this.CreateGraphics();
        //    g.DrawEllipse(new Pen(Color.Blue), 0, 0, this.Width, this.Height);
        //    g.Dispose();
        //}

        //重写OnSizeChanged
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (Height != Radius)
            {
                Radius = Width = Height;
            }
            else if (Width != Radius)
            {
                Radius = Height = Width;
            }

        }

        //重写OnMouseEnter
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            BackgroundImage = ImageEnter;
        }

        //重写OnMouseLeave
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            BackgroundImage = ImageNormal;
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值