路徑的 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.