本例演示了把路径中的数据保存到一个文本文件, 然后再读出的过程.
本例效果图:
代码文件: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.