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

Delphi API 實現異形視窗:圓角矩形、橢圓形、多邊形與星形區域 (SetWindowRgn) 完整範例

     編輯:Delphi

核心原理:視窗區域 (Window Region)

呼叫 SetWindowRgn(hWnd, hRgn, bRedraw) 將視窗裁剪為指定 區域 (HRGN)。區域可由三種 API 建立:

API形狀關鍵參數
CreateRoundRectRgn圓角矩形左、上、右、下、圓角寬、圓角高
CreateEllipticRgn橢圓形左、上、右、下 (外接矩形)
CreatePolygonRgn多邊形/星形頂點陣列、頂點數、填充模式 (WINDING/ALTERNATE)

完整實作單元


unit Form_Statue;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Spin;

type
  TForm1 = class(TForm)
    Button1, Button2, Button3, Button4: TButton;
    SpinEdit1: TSpinEdit;
    Panel1: TPanel;
    procedure DrawRndRectRegion(Wnd: HWND; Rect: TRect);
    procedure DrawEllipticRegion(Wnd: HWND; Rect: TRect);
    procedure DrawPolygonRegion(Wnd: HWND; Rect: TRect; NumPoints: Integer; DoStar: Boolean);
    procedure Button1Click(Sender: TObject); // 圓角矩形
    procedure Button2Click(Sender: TObject); // 多邊形
    procedure Button3Click(Sender: TObject); // 橢圓形
    procedure Button4Click(Sender: TObject); // 星形 (Panel)
  private
    Rgn: HRGN;
    Rect: TRect;
  public
  end;

implementation

procedure TForm1.DrawRndRectRegion(Wnd: HWND; Rect: TRect);
begin
  Rgn := CreateRoundRectRgn(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, 30, 30);
  SetWindowRgn(Wnd, Rgn, True);
end;

procedure TForm1.DrawEllipticRegion(Wnd: HWND; Rect: TRect);
begin
  Rgn := CreateEllipticRgn(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom);
  SetWindowRgn(Wnd, Rgn, True);
end;

procedure TForm1.DrawPolygonRegion(Wnd: HWND; Rect: TRect; NumPoints: Integer; DoStar: Boolean);
const
  RadConvert = Pi / 180;
  Degrees = 360;
  MaxLines = 100;
var
  X, Y, XCenter, YCenter, Radius, Pts, I: Integer;
  Angle, Rotation: Extended;
  ArPts: array[0..MaxLines] of TPoint;
begin
  XCenter := (Rect.Right - Rect.Left) div 2;
  YCenter := (Rect.Bottom - Rect.Top) div 2;
  if DoStar then begin
    Rotation := Degrees / (2 * NumPoints);
    Pts := 2 * NumPoints;
  end else begin
    Rotation := Degrees / NumPoints;
    Pts := NumPoints;
  end;
  Radius := YCenter;
  for I := 0 to Pts - 1 do begin
    if DoStar then
      if (I mod 2) = 0 then Radius := Round(Radius / 2) else Radius := YCenter;
    Angle := ((I * Rotation) + 90) * RadConvert;
    X := XCenter + Round(Cos(Angle) * Radius);
    Y := YCenter - Round(Sin(Angle) * Radius);
    ArPts[I].X := X; ArPts[I].Y := Y;
  end;
  Rgn := CreatePolygonRgn(ArPts, Pts, WINDING);
  SetWindowRgn(Wnd, Rgn, True);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin DrawRndRectRegion(Form1.Handle, Form1.ClientRect); end;

procedure TForm1.Button2Click(Sender: TObject);
begin DrawPolygonRegion(Panel1.Handle, Panel1.BoundsRect, SpinEdit1.Value, False); end;

procedure TForm1.Button3Click(Sender: TObject);
begin DrawEllipticRegion(Form1.Handle, Form1.ClientRect); end;

procedure TForm1.Button4Click(Sender: TObject);
begin DrawPolygonRegion(Panel1.Handle, Panel1.BoundsRect, SpinEdit1.Value, True); end;

end.

關鍵技術細節

  • SetWindowRgn 第三參數 True 觸發立即重繪
  • 區域建立後由系統管理,**無需手動 DeleteObject** (系統接管)
  • CreatePolygonRgn 需按順序提供頂點,**自動閉合**
  • 星形邏輯:雙倍頂點,奇數索引縮小半徑 → 內凹頂點
  • 對 **任何 TWinControl 後裔** (Panel、Button、Edit 等) 同樣有效

應用場景

  • 非矩形視窗 (媒體播放器皮膚、特效視窗)
  • 控件局部裁剪 (圓形頭像、特形按鈕)
  • 遊戲/教學軟體特殊互動區域

注意事項

  • 區域座標為**客戶區座標** (ClientRect) 或**螢幕座標** (BoundsRect) 視 Handle 而定
  • 複雜區域可能影響點擊測試與繪製效能
  • 多顯示器環境下座標系相對視窗原點
  • 現代替代:分層視窗 (WS_EX_LAYERED) + Alpha 混合 實現不規則透明效果

總結

掌握 CreateRoundRectRgn/CreateEllipticRgn/CreatePolygonRgn + SetWindowRgn 組合,即可輕鬆實現圓角、橢圓、多邊形、星形等任意異形視窗。程式碼簡潔、效能佳、適用於所有 VCL 控件,是 Delphi 視覺特效開發的經典技法。

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