本例演示了把路徑中的數據保存到一個文本文件, 然後再讀出的過程.
本例效果圖:
代碼文件: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;
const
FilePath = 'c:\temp\path.txt';
var
path: TGPGraphicsPath;
p: TGPPen;
procedure TForm1.FormCreate(Sender: TObject);
var
pts: array[0..6] of TGPPoint;
rect: TGPRect;
begin
pts[0].X := 10; pts[0].Y := 50;
pts[1].X := 40; pts[1].Y := 90;
pts[2].X := 80; pts[2].Y := 10;
pts[3].X := 110; pts[3].Y := 50;
pts[4].X := 140; pts[4].Y := 10;
pts[5].X := 180; pts[5].Y := 90;
pts[6].X := 210; pts[6].Y := 50;
path := TGPGraphicsPath.Create;
path.AddBeziers(PGPPoint(@pts), Length(pts));
path.GetBounds(rect);
path.AddEllipse(rect);
p := TGPPen.Create(aclBlue, 2);
Button1.Caption := '保存路徑數據';
Button2.Caption := '讀取路徑數據';
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
path.Free;
p.Free;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
begin
g := TGPGraphics.Create(Canvas.Handle);
g.DrawPath(p, path);
g.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
points: array of TGPPoint;
types: array of Byte;
List: TStringList;
i: Integer;
begin
SetLength(points, path.GetPointCount);
SetLength(types, path.GetPointCount);
path.GetPathPoints(PGPPoint(points), Length(points));
path.GetPathTypes(PByte(types), Length(types));
List := TStringList.Create;
for i := 0 to Length(points) - 1 do
List.Add(Format('%d,%d,%d', [points[i].X, points[i].Y, types[i]]));
List.SaveToFile(FilePath);
List.Free;
Text := FilePath;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
points: array of TGPPoint;
types: array of Byte;
List1,List2: TStringList;
i: Integer;
begin
List1 := TStringList.Create;
List2 := TStringList.Create;
if not FileExists(FilePath) then Exit;
List1.LoadFromFile(FilePath);
SetLength(points, List1.Count);
SetLength(types, List1.Count);
for i := 0 to List1.Count - 1 do
begin
if List1[i] = '' then Break;
List2.CommaText := List1[i];
points[i].X := StrToIntDef(List2[0], 0);
points[i].Y := StrToIntDef(List2[1], 0);
types[i] := StrToIntDef(List2[2], 0);
end;
path.Reset;
path.Free;
path := TGPGraphicsPath.Create(PGPPoint(points), PByte(types), List1.Count);
p.SetColor(aclRed);
Repaint;
List1.Free;
List2.Free;
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 147
ClientWidth = 224
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 = 13
Top = 114
Width = 94
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 117
Top = 114
Width = 94
Height = 25
Caption = 'Button2'
TabOrder = 1
OnClick = Button2Click
end
end