首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# GDI+ 繪製圓角矩形:Flags 列舉與位元運算實作全攻略

     編輯:關於C#

前言:控件美化的基石

GDI+ 讓開發者直接控制繪圖邏輯,是自繪控件美化的關鍵。本文示範如何以 GraphicsPath 繪製各種圓角組合的矩形,並巧妙運用 [Flags] 列舉與位元運算管理圓角狀態。

圓角矩形效果圖
圖 1:各種圓角組合效果展示

圓角列舉設計:位元旗標

[Flags]
public enum RoundStyle
{
    None = 0,
    TopLeft = 1,
    TopRight = 2,
    BottomLeft = 4,
    BottomRight = 8,
    All = TopLeft | TopRight | BottomLeft | BottomRight
}

透過位元或 (|) 組合即可表示任意圓角組合,共 16 種情況 (含無圓角)。

策略模式:以字典對應繪製邏輯

建立 Dictionary<RoundStyle, Action<GraphicsPath, Rectangle, int, int>>,每種組合對應一個繪製委托:

private static IDictionary<RoundStyle, Action<GraphicsPath, Rectangle, int, int>> CreateRoundStylesEventContainer()
{
    var dict = new Dictionary<RoundStyle, Action<GraphicsPath, Rectangle, int, int>>();
    dict.Add(RoundStyle.None, (path, rect, r, adj) => path.AddRectangle(rect));
    dict.Add(RoundStyle.All, AddAllRoundStyle);
    dict.Add(RoundStyle.BottomLeft, AddBottomLeftRoundStyle);
    // ... 其餘 13 種組合
    return dict;
}

GraphicsPath.AddArc 參數詳解

AddArc(x, y, width, height, startAngle, sweepAngle):

  • x, y, width, height:定義橢圓外接矩形。
  • startAngle:從 X 軸 (0 度) 順時針起始角度。
  • sweepAngle:順時針掃過角度。
  • 注意:GDI+ 角度方向與數學座標系相反 (順時針為正)。

四大類圓角組合繪製邏輯

單一圓角 (4 種)

例:左下角圓角

private static void AddBottomLeftRoundStyle(GraphicsPath path, Rectangle rect, int radius, int adj)
{
    AddBottomLeftArc(rect, radius, path, adj);
    AddTopLine(rect, path, adj);
    AddRightLine(rect, path, adj);
}
單一圓角效果
圖 2:單一圓角四種情況

雙圓角 (6 種)

相鄰雙角 (同側) 需繪製連接線;對角雙角需計算偏移線條。

// 對角:右下 + 左上
private static void AddBottomRightAndTopLeftRoundStyle(GraphicsPath path, Rectangle rect, int radius, int adj)
{
    AddBottomRightArc(rect, radius, path, adj);
    AddBottomLineWithRightOffset(path, rect, radius, adj);
    AddTopLeftArc(rect, radius, path, adj);
    AddTopLineWithLeftOffset(path, rect, radius, adj);
}
雙圓角效果
圖 3:雙圓角六種組合

三圓角 (4 種)

僅一個銳角,路徑順序需繞過三個圓弧。

三圓角效果
圖 4:三圓角四種組合

四圓角 (全圓角)

private static void AddAllRoundStyle(GraphicsPath path, Rectangle rect, int radius, int adj)
{
    AddTopLeftArc(rect, radius, path, adj);
    AddTopRightArc(rect, radius, path, adj);
    AddBottomRightArc(rect, radius, path, adj);
    AddBottomLeftArc(rect, radius, path, adj);
}
四圓角效果
圖 5:標準圓角矩形

總結與原始碼

本文透過 [Flags] 列舉 + 字典策略模式,系統化處理 16 種圓角組合,邏輯清晰可擴充。實務上可直接套用於自繪按鈕、面板、ToolTip 等控件邊框美化。

原始碼下載:矩陣圓角繪製原始碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved