Welcome

首页 / 软件开发 / Delphi / 再学GDI+[57]: 路径 - Widen

再学GDI+[57]: 路径 - Widen2012-05-12 cnblogs 万一路径的 Widen 方法可以把路径中的线, 根据指定画笔的宽度与样式, 转换为一个范围(有点 类似区域); 但转换后再描绘路径就只能使用 FillPath 而不是 DrawPath 了. 本例没有测试它 的两个默认参数, 因为前面已多次提到了.

本例效果图:

代码文件: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);
procedure FormPaint(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
CheckBox1.Caption := "执行 Widen";
CheckBox2.Caption := "显示路径中所有的点";
end;
procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
p: TGPPen;
b1,b2: TGPBrush;
path: TGPGraphicsPath;
pts: array of TGPPoint;
i: Integer;
begin
g := TGPGraphics.Create(Canvas.Handle);
p := TGPPen.Create(aclSlateGray, 20);
p.SetEndCap(LineCapArrowAnchor);
b1 := TGPSolidBrush.Create(aclRed);
b2 := TGPHatchBrush.Create(HatchStyleDiagonalCross, aclSilver, aclSlateGray);
path := TGPGraphicsPath.Create;
path.AddLine(40, 50, ClientWidth-40, 50);
if CheckBox1.Checked then
begin
path.Widen(p);
g.FillPath(b2, path);
end else g.DrawPath(p, path);
if CheckBox2.Checked then
begin
SetLength(pts, path.GetPointCount);
path.GetPathPoints(PGPPoint(pts), Length(pts));
TGPSolidBrush(b1).SetColor(aclRed);
for i := 0 to Length(pts) - 1 do
g.FillRectangle(b1, pts[i].X-3, pts[i].Y-3, 6, 6);
end;
path.Free;
b1.Free;
b2.Free;
p.Free;
g.Free;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
Repaint;
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
Repaint;
end;
end.