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

再學GDI+[46]: 路徑基礎入門 - Create、FillPath、DrawPath 核心三步驟

     編輯:Delphi

前言

GDI+ 路徑(TGPGraphicsPath)是向量圖形的核心容器。本文展示最基礎的三步驟:建立路徑 → 添加圖形 → 填充/描邊繪製,包含直線、矩形、橢圓三種基本圖元的混合路徑。

核心三步驟流程

  1. path := TGPGraphicsPath.Create - 建立空路徑
  2. path.AddLine / AddRectangle / AddEllipse ... - 累加幾何圖元
  3. g.FillPath(brush, path) / g.DrawPath(pen, path) - 填充與/或描邊

完整極簡範例

procedure TForm1.FormPaint(Sender: TObject);
var
  g: TGPGraphics;
  path: TGPGraphicsPath;
  p: TGPPen;
  b: TGPBrush;
begin
  g := TGPGraphics.Create(Canvas.Handle);
  path := TGPGraphicsPath.Create;
  
  {添加三種基本圖元:同中心、同尺寸}
  path.AddLine(10, 10, 110, 110);           {對角線}
  path.AddRectangle(MakeRect(10, 10, 100, 100)); {矩形}
  path.AddEllipse(MakeRect(10, 10, 100, 100));   {橢圓}
  
  p := TGPPen.Create(aclRed);
  b := TGPSolidBrush.Create(aclYellow);
  
  g.FillPath(b, path);  {黃色填充}
  g.DrawPath(p, path);  {紅色描邊}
  
  b.Free; p.Free; path.Free; g.Free;
end;

執行效果預覽

基礎路徑繪製效果

新手必知關鍵點

  • 路徑累加性:多次 AddXxx 會疊加在同一路徑中,形成複合圖形
  • 填充規則:預設 Alternate 模式,重疊區域交替填充
  • 繪製順序:先 FillPath 再 DrawPath,邊框蓋在填充上方
  • 資源釋放:嚴格遵循 Create/Free 配對,順序相反
  • 座標系統:單位為像素,原點在視窗客戶區左上角

常用 AddXxx 方法速查

方法參數說明
AddLinex1,y1,x2,y2 或 Point 結構直線段
AddLines點陣列連續折線
AddRectangleTGPRect矩形(自動閉合)
AddRectangles矩形陣列多個矩形
AddEllipseTGPRect橢圓/圓(自動閉合)
AddArc矩形+起始角+掃過角橢圓弧
AddPie矩形+起始角+掃過角扇形(含中心線)
AddPolygon點陣列多邊形(自動閉合)
AddBezier4 點三次貝塞爾曲線
AddString字串+字體+版面矩形文字輪廓路徑

表單定義檔

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 179
  ClientWidth = 243
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved