C# WinForms中创建圆形按钮的方法

0 次阅读

C# WinForms默认提供的Button控件是矩形样式,如果希望实现更加现代化的用户界面,例如圆形按钮、悬浮按钮、图标按钮等效果,就需要对Button控件进行自定义绘制。通过修改控件区域(Region)、重写绘制逻辑以及结合GDI+技术,可以轻松实现圆形按钮效果。

圆形按钮不仅能够提升WinForms应用程序的视觉体验,还常用于登录界面、工具栏、快捷操作面板以及仪表盘等场景。下面介绍几种在C# WinForms中创建圆形按钮的方法。

使用Region属性实现圆形按钮

WinForms中的控件都有一个Region属性,用于定义控件的显示区域。默认情况下,Button控件的Region是一个矩形区域,通过设置Region为圆形区域即可实现圆形按钮。

实现思路是创建一个Ellipse(椭圆)区域,并将该区域赋值给按钮。

示例代码如下:

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        CreateRoundButton();
    }

    private void CreateRoundButton()
    {
        Button btn = new Button();

        btn.Text = "点击";
        btn.Size = new Size(100, 100);
        btn.Location = new Point(50, 50);

        GraphicsPath path = new GraphicsPath();
        path.AddEllipse(0, 0, btn.Width, btn.Height);

        btn.Region = new Region(path);

        this.Controls.Add(btn);
    }
}

运行程序后,Button控件会从原来的矩形变成圆形。

这种方式实现简单,适合大多数普通场景。不过需要注意,按钮的宽度和高度最好保持一致,否则生成的会是椭圆形。

使用自定义Button控件实现圆形按钮

如果项目中需要大量使用圆形按钮,推荐创建一个继承Button的自定义控件。这样可以重复使用,同时方便扩展颜色、边框、鼠标效果等功能。

创建一个RoundButton类:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class RoundButton : Button
{
    public RoundButton()
    {
        this.FlatStyle = FlatStyle.Flat;
        this.FlatAppearance.BorderSize = 0;
    }

    protected override void OnResize(System.EventArgs e)
    {
        base.OnResize(e);

        GraphicsPath path = new GraphicsPath();
        path.AddEllipse(0, 0, this.Width, this.Height);

        this.Region = new Region(path);
    }
}

然后在窗体中使用:

RoundButton button = new RoundButton();

button.Text = "确定";
button.Size = new Size(120, 120);
button.Location = new Point(100, 100);

this.Controls.Add(button);

相比直接修改Button,这种方式更加符合面向对象设计思想。

后续如果需要增加圆形按钮颜色、渐变背景、动画效果,只需要修改RoundButton类即可。

通过重写OnPaint绘制高级圆形按钮

如果希望实现更漂亮的效果,例如:

  • 自定义按钮背景颜色

  • 圆角边框

  • 鼠标悬停变色

  • 阴影效果

  • 图标按钮

可以通过重写OnPaint方法进行绘制。

示例:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class CustomCircleButton : Button
{
    public Color ButtonColor { get; set; } = Color.DodgerBlue;

    public CustomCircleButton()
    {
        this.FlatStyle = FlatStyle.Flat;
        this.FlatAppearance.BorderSize = 0;
        this.BackColor = Color.Transparent;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        g.SmoothingMode = SmoothingMode.AntiAlias;

        using (Brush brush = new SolidBrush(ButtonColor))
        {
            g.FillEllipse(
                brush,
                0,
                0,
                this.Width,
                this.Height
            );
        }

        TextRenderer.DrawText(
            g,
            this.Text,
            this.Font,
            this.ClientRectangle,
            Color.White,
            TextFormatFlags.HorizontalCenter |
            TextFormatFlags.VerticalCenter
        );
    }
}

其中:

  • SmoothingMode.AntiAlias用于开启抗锯齿,让圆形边缘更加平滑。

  • FillEllipse()用于绘制圆形背景。

  • TextRenderer.DrawText()负责绘制按钮文字。

这种方式可以完全控制按钮外观,非常适合开发具有现代UI风格的桌面应用。

设置圆形按钮鼠标交互效果

普通圆形按钮只有形状变化,如果想提高用户体验,可以增加鼠标进入和离开效果。

例如:

private Color normalColor = Color.Blue;
private Color hoverColor = Color.LightBlue;

public CustomCircleButton()
{
    this.MouseEnter += (s, e) =>
    {
        this.ButtonColor = hoverColor;
        this.Invalidate();
    };

    this.MouseLeave += (s, e) =>
    {
        this.ButtonColor = normalColor;
        this.Invalidate();
    };
}

当鼠标移动到按钮上方时,可以改变颜色,让用户感受到按钮状态变化。

圆形按钮中的文字和图标布局

圆形按钮空间有限,如果文字较长,会影响显示效果。因此实际开发中通常采用以下方式:

1. 使用短文本

例如:

  • 添加

  • 删除

  • 保存

  • 搜索

短文字更适合圆形区域。

2. 使用图片代替文字

例如:

button.Image = Image.FromFile("search.png");
button.Text = "";

这种方式常用于工具软件和管理系统。

3. 设置文字对齐方式

可以通过以下属性调整:

button.TextAlign = ContentAlignment.MiddleCenter;
button.ImageAlign = ContentAlignment.MiddleCenter;

保证内容始终位于圆形按钮中心。

处理圆形按钮点击区域问题

虽然Region可以改变按钮显示区域,但默认情况下控件仍然可能存在透明区域响应问题。

例如一个100×100的圆形按钮,四个角的位置虽然不可见,但仍可能触发点击。

通过设置Region可以解决:

GraphicsPath path = new GraphicsPath();
path.AddEllipse(button.ClientRectangle);