再学GDI+[59]: 路径

通过路径的辅助类 TGPGraphicsPathIterator , 可以获得更多路径数据和控制能力.

本例效果图:

代码文件: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;
var
 path: TGPGraphicsPath;
procedure TForm1.FormCreate(Sender: TObject);
var
 pt1, pt2: TPoint;
begin
 Button1.Caption := '查看路径基本数据';
 Button2.Caption := '重绘路径中的子路径';
 pt1 := Point(30, 20);
 pt2 := Point(150, 120);
 {建立路径, 并添加四个子图形}
 path := TGPGraphicsPath.Create;
 path.StartFigure;
 path.AddRectangle(MakeRect(Rect(pt1, pt2)));
 path.CloseFigure;
 path.StartFigure;
 path.AddEllipse(MakeRect(Rect(pt1, pt2)));
 path.CloseFigure;
 path.StartFigure;
 path.AddLine(pt1.X, pt1.Y, pt2.X, pt2.Y);
 path.CloseFigure;
 path.StartFigure;
 path.AddLine(pt1.X, pt2.Y, pt2.X, pt1.Y);
 path.CloseFigure;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
 path.Free;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
 g: TGPGraphics;
 p: TGPPen;
begin
 g := TGPGraphics.Create(Canvas.Handle);
 p := TGPPen.Create(aclSilver, 2);
 g.DrawPath(p, path);
 p.Free;
 g.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
 PathIterator: TGPGraphicsPathIterator;
 str: string;
begin
 PathIterator := TGPGraphicsPathIterator.Create(path);
 str := str + Format('路径中的总点数: %d', [PathIterator.GetCount]);
 str := str + Format(sLineBreak + '路径中的子路径数: %d', [PathIterator.GetSubpathCount]);
 str := str + Format(sLineBreak + '路径中是否包含曲线: %s', [BoolToStr (PathIterator.HasCurve, True)]);
 ShowMessage(str);
 PathIterator.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
const
 ColorArr: array[0..3] of TGPColor = (aclRed, aclGreen, aclBlue, aclYellow);
var
 PathIterator: TGPGraphicsPathIterator;
 PathSection: TGPGraphicsPath;
 bool: LongBool;
 g: TGPGraphics;
 p: TGPPen;
 i: Integer;
begin
 g := TGPGraphics.Create(Canvas.Handle);
 p := TGPPen.Create(aclRed, 2);
 PathIterator := TGPGraphicsPathIterator.Create(path);
 PathIterator.Rewind; {到路径起始点}
 for i := 0 to PathIterator.GetSubpathCount - 1 do
 begin
  PathSection := TGPGraphicsPath.Create;
  PathIterator.NextSubpath(PathSection, bool);
  p.SetColor(ColorArr[i]);
  g.DrawPath(p, PathSection);
  PathSection.Free;
 end;
 PathIterator.Free;
 p.Free;
 g.Free;
end;
end.

时间: 2024-09-26 13:55:43

再学GDI+[59]: 路径的相关文章

再学GDI+[69]: 路径画刷(9)

SetInterpolationColors 与 SetSurroundColors 的区别 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, ExtCtrls; type TForm1 = class(TForm) procedure FormPaint(Sen

再学GDI+[58]: 路径

本例演示了把路径中的数据保存到一个文本文件, 然后再读出的过程. 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(

再学GDI+[57]: 路径

路径的 Widen 方法可以把路径中的线, 根据指定画笔的宽度与样式, 转换为一个范围(有点 类似区域); 但转换后再描绘路径就只能使用 FillPath 而不是 DrawPath 了. 本例没有测试它 的两个默认参数, 因为前面已多次提到了. 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdC

再学GDI+[65]: 路径画刷(5)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormPaint(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses GDIPOBJ

再学GDI+[64]: 路径画刷(4)

在本例中没有指定 CenterColor, 将默认白色; SurroundColors 原来是对应路径中的点(但按下面的做法在椭圆里不灵). 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormPaint(Sender: T

再学GDI+[56]: 路径

通过路径的 Warp 方法可以让路径在一个范围内(四个点决定的范围)变换; 第一个参数可以是 3 个点或 4 个点的数组, 如果是 3 个的数组, 那么第 4 个点将自动跟 随一个平行四边形; 第二个参数用来指定点数组的个数, 本例就是通过这个参数控制了点的个数; 第三个参数是一个矩形, 这个矩形应该是路径的外接矩形, 之后的参数都是可选的; 第四个参数也是一个矩阵变换, 因由专门的 例子 , 这里没有尝试; 第五个参数是 TWarpMode(WarpModePerspective, WarpM

再学GDI+[55]: 路径

通过路径的 Flatten 方法可以把路径中的曲线拉直, 拉直到什么程度是由它的第二个参数( 默认0.25)决定的; 它的第一个参数又是一个矩阵变换, 也就是说 Flatten 可以同时进行矩阵 变换, 本例没有测试它, 其详情参见: http://www.cnblogs.com/del/archive/2008/06/20/1226293.html 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Varia

再学GDI+[53]: 路径

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) RadioGroup1: TRadioGroup; procedure FormCreate(Sender: TObject); procedure FormP

再学GDI+[52]: 路径

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) CheckBox1: TCheckBox; CheckBox2: TCheckBox; procedure FormCreate(Sender: TObject); procedu