本例效果圖:
代碼文件: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.object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 235
ClIEntWidth = 302
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object CheckBox1: TCheckBox
Left = 157
Top = 152
Width = 124
Height = 17
Caption = 'CheckBox1'
TabOrder = 0
OnClick = CheckBox1Click
end
object CheckBox2: TCheckBox
Left = 157
Top = 175
Width = 137
Height = 17
Caption = 'CheckBox2'
TabOrder = 1
OnClick = CheckBox2Click
end
object CheckBox3: TCheckBox
Left = 157
Top = 198
Width = 137
Height = 17
Caption = 'CheckBox3'
TabOrder = 2
OnClick = CheckBox3Click
end
end