程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 再學GDI+[59]: 路徑 - TGPGraphicsPathIterator

再學GDI+[59]: 路徑 - TGPGraphicsPathIterator

編輯:Delphi

通過路徑的輔助類 TGPGraphicsPathIterator , 可以獲得更多路徑數據和控制能力.

本例效果圖:

代碼文件:unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;
type
 TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  procedure FormPaint(Sender: TObject);
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
var
 path: TGPGraphicsPath;
procedure TForm1.FormCreate(Sender: TObject);
var
 pt1, pt2: TPoint;
begin
 Button1.Caption := '查看路徑基本數據';
 Button2.Caption := '重繪路徑中的子路徑';
 pt1 := Point(30, 20);
 pt2 := Point(150, 120);
 {建立路徑, 並添加四個子圖形}
 path := TGPGraphicsPath.Create;
 path.StartFigure;
 path.AddRectangle(MakeRect(Rect(pt1, pt2)));
 path.CloseFigure;
 path.StartFigure;
 path.AddEllipse(MakeRect(Rect(pt1, pt2)));
 path.CloseFigure;
 path.StartFigure;
 path.AddLine(pt1.X, pt1.Y, pt2.X, pt2.Y);
 path.CloseFigure;
 path.StartFigure;
 path.AddLine(pt1.X, pt2.Y, pt2.X, pt1.Y);
 path.CloseFigure;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
 path.Free;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
 g: TGPGraphics;
 p: TGPPen;
begin
 g := TGPGraphics.Create(Canvas.Handle);
 p := TGPPen.Create(aclSilver, 2);
 g.DrawPath(p, path);
 p.Free;
 g.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
 PathIterator: TGPGraphicsPathIterator;
 str: string;
begin
 PathIterator := TGPGraphicsPathIterator.Create(path);
 str := str + Format('路徑中的總點數: %d', [PathIterator.GetCount]);
 str := str + Format(sLineBreak + '路徑中的子路徑數: %d', [PathIterator.GetSubpathCount]);
 str := str + Format(sLineBreak + '路徑中是否包含曲線: %s', [BoolToStr (PathIterator.HasCurve, True)]);
 ShowMessage(str);
 PathIterator.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
const
 ColorArr: array[0..3] of TGPColor = (aclRed, aclGreen, aclBlue, aclYellow);
var
 PathIterator: TGPGraphicsPathIterator;
 PathSection: TGPGraphicsPath;
 bool: LongBool;
 g: TGPGraphics;
 p: TGPPen;
 i: Integer;
begin
 g := TGPGraphics.Create(Canvas.Handle);
 p := TGPPen.Create(aclRed, 2);
 PathIterator := TGPGraphicsPathIterator.Create(path);
 PathIterator.Rewind; {到路徑起始點}
 for i := 0 to PathIterator.GetSubpathCount - 1 do
 begin
  PathSection := TGPGraphicsPath.Create;
  PathIterator.NextSubpath(PathSection, bool);
  p.SetColor(ColorArr[i]);
  g.DrawPath(p, PathSection);
  PathSection.Free;
 end;
 PathIterator.Free;
 p.Free;
 g.Free;
end;
end.

窗體文件:object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClientHeight = 198
 ClientWidth = 181
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 Position = poDesktopCenter
 OnCreate = FormCreate
 OnDestroy = FormDestroy
 OnPaint = FormPaint
 PixelsPerInch = 96
 TextHeight = 13
 object Button1: TButton
  Left = 24
  Top = 134
  Width = 137
  Height = 25
  Caption = 'Button1'
  TabOrder = 0
  OnClick = Button1Click
 end
 object Button2: TButton
  Left = 24
  Top = 165
  Width = 137
  Height = 25
  Caption = 'Button2'
  TabOrder = 1
  OnClick = Button2Click
 end
end

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved