首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

C# WinForms 繪製 3D 立體邊框(白色高亮+灰色陰影)兩種實現方式

     編輯:程式設計疑難解答

效果描述

如圖所示,邊框呈現凹凸感:頂/左邊亮白色,底/右邊暗灰色,模擬 3D 凸起或凹陷效果。

方式一:直接使用 GroupBox 控件(零代碼,最省事)

// 設計器拖入 GroupBox,或代碼創建
GroupBox gb = new GroupBox();
gb.Text = "分組標題";      // 可選
gb.Location = new Point(20, 20);
gb.Size = new Size(300, 200);
// 系統自動繪製 3D 邊框(視系統主題而定)
this.Controls.Add(gb);
  • 優點:原生支持、自適應主題、無需繪圖代碼。
  • 缺點:樣式難高度定制(顏色、圓角、線寬),文字位置固定。

方式二:自定義繪製(完全可控,推薦用 Panel + Paint 事件)

public class Panel3D : Panel {
    public Color LightColor { get; set; } = Color.White;   // 高亮色
    public Color DarkColor { get; set; } = Color.Gray;     // 陰影色
    public int BorderWidth { get; set; } = 2;              // 邊框寬度

    public Panel3D() {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | 
                      ControlStyles.UserPaint | 
                      ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        Graphics g = e.Graphics;
        Rectangle rect = this.ClientRectangle;
        int w = BorderWidth;

        // 繪製四條邊:頂/左亮,底/右暗
        using (Pen lightPen = new Pen(LightColor, w), darkPen = new Pen(DarkColor, w)) {
            // 頂邊
            g.DrawLine(lightPen, rect.Left, rect.Top, rect.Right, rect.Top);
            // 左邊
            g.DrawLine(lightPen, rect.Left, rect.Top, rect.Left, rect.Bottom);
            // 底邊
            g.DrawLine(darkPen, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
            // 右邊
            g.DrawLine(darkPen, rect.Right, rect.Top, rect.Right, rect.Bottom);
        }
    }
}

// 使用
Panel3D panel = new Panel3D();
panel.Location = new Point(20, 20);
panel.Size = new Size(300, 200);
panel.BorderWidth = 2;
panel.LightColor = Color.FromArgb(255, 255, 255);
panel.DarkColor = Color.FromArgb(128, 128, 128);
this.Controls.Add(panel);

進階:圓角矩形邊框

protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e);
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
    int radius = 8;
    using (GraphicsPath path = GetRoundRectPath(rect, radius)) {
        // 繪製頂左亮弧 + 底右暗弧(需拆分路徑或多次繪製)
        // 簡化:整體用漸變筆刷模擬 3D
        using (LinearGradientBrush brush = new LinearGradientBrush(
            rect, Color.White, Color.Gray, LinearGradientMode.ForwardDiagonal)) {
            using (Pen pen = new Pen(brush, BorderWidth)) {
                g.DrawPath(pen, path);
            }
        }
    }
}

private GraphicsPath GetRoundRectPath(Rectangle rect, int radius) {
    GraphicsPath path = new GraphicsPath();
    path.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90);
    path.AddArc(rect.Right - radius * 2, rect.Y, radius * 2, radius * 2, 270, 90);
    path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
    path.AddArc(rect.X, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
    path.CloseFigure();
    return path;
}

選型建議

場景推薦
快速搭建、標準表單分組GroupBox
需自定義顏色、圓角、線寬、動畫自定義 Panel3D
高性能、大量邊框Override OnPaint + 緩存 GraphicsPath
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved