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

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 136
  ClientWidth = 287
  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 = 43
    Top = 104
    Width = 97
    Height = 17
    Caption = 'CheckBox1'
    TabOrder = 0
    OnClick = CheckBox1Click
  end
  object CheckBox2: TCheckBox
    Left = 138
    Top = 104
    Width = 125
    Height = 17
    Caption = 'CheckBox2'
    TabOrder = 1
    OnClick = CheckBox2Click
  end
end

posted on 2008-06-20 23:23  万一  阅读(1978)  评论(0编辑  收藏  举报