You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WinForms C# 2022中制作美观的圆角按钮?

WinForms 圆角按钮实现方案

下面是一个可直接使用的自定义圆角Button控件实现,适配你需要的样式,支持正常、悬停、按下三种状态的视觉反馈,避免失效或显示异常的问题:

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

public class RoundedButton : Button
{
    // 可自定义的圆角半径
    private int _cornerRadius = 12;
    public int CornerRadius
    {
        get => _cornerRadius;
        set
        {
            _cornerRadius = value;
            Invalidate(); // 更改后重绘控件
        }
    }

    public RoundedButton()
    {
        // 基础设置,避免原生样式干扰
        FlatStyle = FlatStyle.Flat;
        FlatAppearance.BorderSize = 0;
        BackColor = Color.FromArgb(0, 122, 204); // 默认蓝色背景,可自行修改
        ForeColor = Color.White;
        Size = new Size(120, 40);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // 抗锯齿,避免边缘模糊

        // 根据按钮状态设置背景色
        Color bgColor = BackColor;
        if (MouseButtons == MouseButtons.Left && ClientRectangle.Contains(PointToClient(MousePosition)))
        {
            bgColor = Color.FromArgb(0, 100, 170); // 按下状态颜色
        }
        else if (ClientRectangle.Contains(PointToClient(MousePosition)))
        {
            bgColor = Color.FromArgb(0, 140, 220); // 悬停状态颜色
        }

        // 绘制圆角背景
        using (GraphicsPath path = new GraphicsPath())
        using (SolidBrush brush = new SolidBrush(bgColor))
        {
            path.AddArc(0, 0, _cornerRadius, _cornerRadius, 180, 90);
            path.AddArc(Width - _cornerRadius, 0, _cornerRadius, _cornerRadius, 270, 90);
            path.AddArc(Width - _cornerRadius, Height - _cornerRadius, _cornerRadius, _cornerRadius, 0, 90);
            path.AddArc(0, Height - _cornerRadius, _cornerRadius, _cornerRadius, 90, 90);
            path.CloseAllFigures();
            e.Graphics.FillPath(brush, path);
        }

        // 绘制按钮文本
        TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
        TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, flags);
    }

    // 鼠标移入移出时触发重绘
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        Invalidate();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        Invalidate();
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        Invalidate();
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        Invalidate();
    }
}

使用方法

  1. 在你的WinForms项目中新建一个类文件,将上述代码粘贴进去
  2. 编译项目后,这个自定义按钮会出现在Visual Studio的工具箱中,直接拖到窗体上即可使用
  3. 你可以在属性面板中调整CornerRadius(圆角半径)、BackColor(背景色)等属性,匹配你想要的样式

注意事项

  • 如果需要添加边框,可以在OnPaint方法中增加Pen绘制圆角路径的代码
  • 确保按钮的FlatStyle保持为Flat,避免原生边框干扰
  • 若要适配高DPI,可在项目设置中开启高DPI支持,避免缩放时圆角变形

内容的提问来源于stack exchange,提问作者GalaxyGaming2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:28:04