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

是否可在C# Windows Forms中为所有控件设置border-radius圆角?

C# WinForm 各类控件圆角效果实现方案

通用实现逻辑

WinForm 原生没有提供圆角属性,所有控件的圆角都可以通过 GraphicsPath 绘制圆角路径,再将路径赋值给控件的 Region 属性实现,不需要依赖第三方DLL。


方案1:通用辅助类(快速适配所有现有控件)

封装静态方法,调用一次即可给任意控件添加圆角,无需修改原有控件定义:

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

public static class RoundCornerUtility
{
    /// <param name="control">要设置圆角的控件</param>
    /// <param name="radius">圆角半径,单位px</param>
    public static void ApplyRoundCorner(Control control, int radius)
    {
        if (control == null || radius < 1) return;
        
        using (var roundPath = new GraphicsPath())
        {
            // 绘制四个圆角的闭合路径
            roundPath.AddArc(0, 0, radius, radius, 180, 90);
            roundPath.AddArc(control.Width - radius, 0, radius, radius, 270, 90);
            roundPath.AddArc(control.Width - radius, control.Height - radius, radius, radius, 0, 90);
            roundPath.AddArc(0, control.Height - radius, radius, radius, 90, 90);
            roundPath.CloseAllFigures();
            
            control.Region = new Region(roundPath);
        }
    }
}

使用示例

在窗体Load事件中调用即可:

private void Form1_Load(object sender, EventArgs e)
{
    // 给Button设置12px圆角
    RoundCornerUtility.ApplyRoundCorner(button1, 12);
    // 给PictureBox设置8px圆角
    RoundCornerUtility.ApplyRoundCorner(pictureBox1, 8);
    // 给TextBox设置6px圆角,同时建议将TextBox的BorderStyle设为None
    RoundCornerUtility.ApplyRoundCorner(textBox1, 6);
}

注意事项

  • 如果控件会动态调整大小,需要在控件的Resize事件中重新调用ApplyRoundCorner方法,避免圆角变形
  • 输入类控件(如TextBox)设置圆角后如果残留系统默认边框,将BorderStyle属性设为None即可

方案2:自定义圆角控件(适合高频复用场景)

如果项目中需要大量使用圆角控件,直接重写控件的OnPaint方法效果更稳定,以圆角Button为例:

public class RoundButton : Button
{
    // 公开圆角半径属性,可在属性面板直接调整
    public int CornerRadius { get; set; } = 10;

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.Clear(Parent.BackColor);

        using (var roundPath = new GraphicsPath())
        {
            roundPath.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
            roundPath.AddArc(Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
            roundPath.AddArc(Width - CornerRadius, Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
            roundPath.AddArc(0, Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
            roundPath.CloseAllFigures();

            // 填充控件背景
            using (var backBrush = new SolidBrush(BackColor))
            {
                e.Graphics.FillPath(backBrush, roundPath);
            }
            // 绘制控件文字
            TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, 
                TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }
    }
}

编译项目后,自定义的RoundButton会自动出现在VS工具箱中,拖拽即可使用,同理可自定义PictureBox、TextBox等其他控件的圆角实现。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 12:12:03