C#圆形Button——RoundButton

本文介绍了一种使用C#自定义的圆形按钮控件,该控件可通过设置半径属性来调整大小,并能通过ImageNormal等属性设置按钮的显示图片。文章详细展示了控件的实现代码。

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

C#中自定义控件,圆形按钮。编译生成后会在工具箱生成自定义的控件,通过其半径属性设置大小,通过ImageNormal等属性设置图片。

 

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 YcPackageTool.Widgets
{
    class RoundButton : 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;
        }
    }
}

### 创建平滑且高质量的圆角按钮 为了在 C# WinForms 应用程序中创建平滑且高质量的圆角按钮,可以通过自定义绘制技术来实现这一目标。下面介绍一种通过继承 `Button` 类并重写其绘图逻辑的方法。 #### 定义圆形区域路径 首先,在类内部声明一个用于存储图形路径的对象,这有助于后续对按钮形状的操作: ```csharp private GraphicsPath _path; ``` 接着初始化此路径对象,并将其应用于按钮的轮廓。可以在构造函数内完成这些操作: ```csharp public RoundButton() { InitializeComponent(); this.DoubleBuffered = true; // 减少闪烁现象 SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); } ``` #### 绘制圆角矩形 利用 GDI+ 提供的功能,可以方便地画出具有指定半径的圆角矩形。这里提供了一个辅助方法 `_getRoundRectFromRectangle()` 来计算所需的几何参数: ```csharp protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (_path = GetRoundedRect(this.ClientRectangle, 20)) { Region region = new(_path); this.Region = region; Brush brush = Enabled ? Brushes.LightBlue : SystemBrushes.ControlDark; e.Graphics.FillPath(brush, _path); TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, BackColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); } } // 辅助方法:获取带有特定弧度的矩形边界 private static GraphicsPath GetRoundedRect(Rectangle bounds, int radius) { var path = new GraphicsPath(); if (radius <= 0 || Math.Min(bounds.Width, bounds.Height) / 2 < radius) { path.AddRectangle(bounds); path.CloseFigure(); return path; } pointF[] points = { new PointF(bounds.Left + radius, bounds.Top), new PointF(bounds.Right - radius, bounds.Top), new PointF(bounds.Right, bounds.Top), new PointF(bounds.Right, bounds.Top + radius), new PointF(bounds.Right, bounds.Bottom - radius), new PointF(bounds.Right, bounds.Bottom), new PointF(bounds.Right - radius, bounds.Bottom), new PointF(bounds.Left + radius, bounds.Bottom), new PointF(bounds.Left, bounds.Bottom), new PointF(bounds.Left, bounds.Bottom - radius), new PointF(bounds.Left, bounds.Top + radius), new PointF(bounds.Left, bounds.Top) }; path.StartFigure(); path.AddArc(new Rectangle(bounds.Location.X, bounds.Location.Y, 2 * radius, 2 * radius), 180f, 90f); path.AddLine(points[0], points[1]); path.AddArc(new Rectangle(bounds.Right - 2 * radius, bounds.Top, 2 * radius, 2 * radius), 270f, 90f); path.AddLine(points[3], points[4]); path.AddArc(new Rectangle(bounds.Right - 2 * radius, bounds.Bottom - 2 * radius, 2 * radius, 2 * radius), 0f, 90f); path.AddLine(points[6], points[7]); path.AddArc(new Rectangle(bounds.Left, bounds.Bottom - 2 * radius, 2 * radius, 2 * radius), 90f, 90f); path.CloseAllFigures(); return path; } ``` 上述代码片段展示了如何构建一个名为 `GetRoundedRect` 的静态方法,它接受两个参数——一个是表示按钮尺寸的矩形结构;另一个是指定四个角落处曲线部分所占比例的一个整数值。该方法返回的是由一系列线段和四分之一椭圆组成的闭合多边形,即所谓的“圆角矩形”。 最后一步是在窗体加载时实例化此类,并调整必要的样式属性以确保最佳视觉效果[^1]。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值