路徑 - GetPointCount、GetPathPoints、GetLastPoint、GetBounds
本例效果圖:
代碼文件:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure CheckBox3Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
CheckBox1.Caption := '提取路徑中所有的點';
CheckBox2.Caption := '提取路徑中最後一個點';
CheckBox3.Caption := '顯示路徑的外接矩形';
end;
procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
path: TGPGraphicsPath;
p: TGPPen;
b: TGPBrush;
pts: array[0..6] of TGPPoint;
ptf: TGPPointF;
ppts: array of TGPPoint;
rect: TGPRect;
i: Integer;
begin
{給 Bezier 線准備的點, 用於建立路徑}
pts[0].X := 22; pts[0].Y := 44;
pts[1].X := 11; pts[1].Y := 111;
pts[2].X := 33; pts[2].Y := 222;
pts[3].X := 60; pts[3].Y := 88;
pts[4].X := 80; pts[4].Y := 222;
pts[5].X := 170; pts[5].Y := 111;
pts[6].X := 200; pts[6].Y := 11;
g := TGPGraphics.Create(Canvas.Handle);
{建立路徑, 並給路徑添加兩條 Bezier 線}
path := TGPGraphicsPath.Create;
path.AddBeziers(PGPPoint(@pts), Length(pts));
{繪制路徑}
p := TGPPen.Create(aclRed);
g.DrawPath(p, path);
{虛畫原始數據點}
p.SetColor(aclGray);
for i := 0 to Length(pts)-1 do
g.DrawRectangle(p, MakeRect(pts[i].X-2, pts[i].Y-2, 4, 4));
{提取路徑中所有的點}
b := TGPSolidBrush.Create(aclBlue);
if CheckBox1.Checked then
begin
SetLength(ppts, path.GetPointCount);
path.GetPathPoints(PGPPoint(ppts), Length(pts));
for i := 0 to Length(ppts)-1 do
g.FillRectangle(b, MakeRect(ppts[i].X-3, ppts[i].Y-3, 6, 6));
end;
{可以專門獲取最後一個點}
TGPSolidBrush(b).SetColor(aclRed);
if CheckBox2.Checked then
begin
path.GetLastPoint(ptf);
g.FillRectangle(b, MakeRect(ptf.X-3, ptf.Y-3, 6, 6));
end;
{路徑的外接矩形}
if CheckBox3.Checked then
begin
path.GetBounds(rect);
g.DrawRectangle(p, rect);
end;
b.Free;
p.Free;
path.Free;
g.Free;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
Repaint;
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
Repaint;
end;
procedure TForm1.CheckBox3Click(Sender: TObject);
begin
Repaint;
end;
end.