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

如何在WinForm控件中实现圆角与阴影效果?

实现带阴影的圆角WinForm控件方法

下面提供两种实用方案,分别适配自定义控件和窗体场景:

方案一:自定义重绘控件(灵活适配各类控件)

通过重写控件的OnPaint方法,手动绘制圆角主体和阴影,可自由调整样式参数:

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

public class RoundedShadowControl : Control
{
    // 可配置属性
    private int _cornerRadius = 15;
    private int _shadowBlur = 10;
    private Color _shadowColor = Color.FromArgb(100, 0, 0, 0);

    public int CornerRadius
    {
        get => _cornerRadius;
        set { _cornerRadius = value; Invalidate(); }
    }

    public int ShadowBlur
    {
        get => _shadowBlur;
        set { _shadowBlur = value; Invalidate(); }
    }

    public Color ShadowColor
    {
        get => _shadowColor;
        set { _shadowColor = value; Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        var graphics = e.Graphics;
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        // 绘制渐变阴影
        var shadowRect = new Rectangle(_shadowBlur, _shadowBlur, Width - 2 * _shadowBlur, Height - 2 * _shadowBlur);
        using (var shadowPath = GetRoundedRectanglePath(shadowRect, _cornerRadius))
        using (var shadowBrush = new PathGradientBrush(shadowPath))
        {
            var colorBlend = new ColorBlend(3);
            colorBlend.Colors = new[] { _shadowColor, Color.Transparent, Color.Transparent };
            colorBlend.Positions = new[] { 0f, 0.1f, 1f };
            shadowBrush.InterpolationColors = colorBlend;
            graphics.FillPath(shadowBrush, shadowPath);
        }

        // 绘制圆角主体
        var mainRect = new Rectangle(0, 0, Width - _shadowBlur, Height - _shadowBlur);
        using (var mainPath = GetRoundedRectanglePath(mainRect, _cornerRadius))
        using (var mainBrush = new SolidBrush(BackColor))
        {
            graphics.FillPath(mainBrush, mainPath);
            // 可选:绘制边框
            using (var pen = new Pen(ForeColor, 1))
            {
                graphics.DrawPath(pen, mainPath);
            }
        }

        // 绘制文本
        TextRenderer.DrawText(graphics, Text, Font, mainRect, ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
    }

    // 生成圆角矩形路径
    private GraphicsPath GetRoundedRectanglePath(Rectangle rect, int radius)
    {
        var path = new GraphicsPath();
        path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
        path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
        path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
        path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
        path.CloseAllFigures();
        return path;
    }

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

使用时,将这个自定义控件添加到WinForm项目,即可在设计器中拖拽使用,还能通过属性面板调整圆角半径、阴影参数。

方案二:调用系统API实现原生阴影(Windows 11+)

如果是给窗体添加圆角和系统级自然阴影,可调用Windows的DwmApi,效果和系统原生窗口一致:

using System.Runtime.InteropServices;
using System.Windows.Forms;

public partial class RoundedShadowForm : Form
{
    public RoundedShadowForm()
    {
        InitializeComponent();
    }

    protected override void OnLoad(System.EventArgs e)
    {
        base.OnLoad(e);
        // 设置窗体圆角样式(DWMWCP_ROUND为全圆角,DWMWCP_ROUNDSMALL为小圆角)
        int cornerPreference = DwmApi.DWMWCP_ROUND;
        DwmApi.DwmSetWindowAttribute(this.Handle, DwmApi.DWMWA_WINDOW_CORNER_PREFERENCE, ref cornerPreference, sizeof(int));
        
        // 确保窗体保留系统阴影(若自定义了FormBorderStyle,需设置为带边框样式)
        this.FormBorderStyle = FormBorderStyle.Sizable;
    }
}

// DwmApi P/Invoke声明
public static class DwmApi
{
    [DllImport("dwmapi.dll")]
    public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

    public const int DWMWA_WINDOW_CORNER_PREFERENCE = 33;
    public const int DWMWCP_ROUND = 2;
    public const int DWMWCP_ROUNDSMALL = 1;
}

注意事项

  • 方案一的重绘方式兼容所有Windows版本,适合自定义按钮、面板等各类控件;
  • 方案二仅支持Windows 11及以上系统,且仅适用于窗体;
  • 重绘控件时必须开启SmoothingMode.AntiAlias,否则圆角边缘会出现锯齿;
  • 若需要支持背景图片,可修改方案一中的主体绘制逻辑,用graphics.DrawImage替代FillPath。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:01:04