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

再學GDI+[58]: 路徑 - 保存與讀取路徑數據

編輯:Delphi

本例演示了把路徑中的數據保存到一個文本文件, 然後再讀出的過程.

本例效果圖:

代碼文件: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.

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