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

再學GDI+[50]: 路徑資料深度解析 - GetPathPoints、GetPathTypes、TPathData 完整指南

     編輯:Delphi

前言

理解路徑的點座標陣列與點類型陣列是掌握 GDI+ 向量圖形核心。本文對比兩種存取方式:傳統 GetPathPoints/GetPathTypes 組合,與封裝類 TPathData 的簡化操作。

點類型枚舉完整定義

常數值Delphi 列舉含義典型用途
$00PathPointTypeStart路徑/子路徑起始點MoveTo 起點
$01PathPointTypeLine直線終點LineTo 終點
$03PathPointTypeBezier貝塞爾曲線控制點/終點AddBezier/AddBeziers
$07PathPointTypePathTypeMask類型遮罩 (低 3 位)提取基本類型
$10PathPointTypeDashMode虛線模式標記虛線路徑段落
$20PathPointTypePathMarker路徑標記點自定義標記
$80PathPointTypeCloseSubpath子路徑閉合點CloseFigure 產生

方式一:傳統 GetPathPoints/GetPathTypes

var
  points: array of TGPPoint;
  types: PByte;
  typestr: string;
  i: Integer;
begin
  SetLength(points, path.GetPointCount);
  GetMem(types, path.GetPointCount);
  path.GetPathPoints(PGPPoint(points), Length(points));
  path.GetPathTypes(types, Length(points));
  for i := 0 to Length(points) - 1 do begin
    case types^ of
      $00: typestr := '路徑起始點';
      $01: typestr := '直線點';
      $03: typestr := '貝塞爾線點';
      $07: typestr := '遮蓋點';
      $10: typestr := '虛線點';
      $20: typestr := '路徑標記';
      $80: typestr := '子路徑結束點';
    end;
    StringGrid.Cells[0,i+1] := IntToStr(points[i].X);
    StringGrid.Cells[1,i+1] := IntToStr(points[i].Y);
    StringGrid.Cells[2,i+1] := typestr;
    Inc(types);
  end;
  Dec(types, Length(points)); FreeMem(types);
end;

方式二:TPathData 簡化版(推薦)

var
  PathData: TPathData;
  Points: PGPPointF;
  Types: PByte;
  TypeStr: string;
  i: Integer;
begin
  PathData := TPathData.Create;
  path.GetPathData(PathData);  {一次取得所有資料}
  Points := PathData.Points;   {Single 精度點陣列}
  Types := PathData.Types;
  StringGrid.RowCount := PathData.Count + 1;
  for i := 0 to PathData.Count - 1 do begin
    case Types^ of ... {同樣 case 判斷}
    StringGrid.Cells[0,i+1] := IntToStr(Trunc(Points.X));
    StringGrid.Cells[1,i+1] := IntToStr(Trunc(Points.Y));
    StringGrid.Cells[2,i+1] := TypeStr;
    Inc(Points); Inc(Types);
  end;
  PathData.Free;
end;

執行效果預覽

路徑資料檢視

兩種方式對比

特性GetPathPoints/TypesTPathData
精度Integer (TGPPoint)Single (TGPPointF)
記憶體管理手動 GetMem/FreeMem自動管理
資料存取分離呼叫統一物件
修改回寫需重建路徑修改後可重建
推薦場景舊代碼維護、整數座標需求新專案、高精度、編輯器

StringGrid 點選高亮功能

選中儲存格時,在畫布上繪製藍色方塊標示對應頂點位置。

表單定義檔

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 276
  ClientWidth = 303
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid ... {3 欄:X, Y, 點類型}
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved