不管什麼曲線命令, 到來路徑中都會變成 Bezier 線; 也就是說路徑中只有直線和 BezIEr 線.
FlattenPath 和 WidenPath 都能夠把路徑中的 BezIEr 線轉換為近似的直線; 不同的是: 用 WidenPath 轉換後貌似加寬了線, 其實它是轉換成了一個包圍路徑的新路徑(類似區域).
本例效果圖:
代碼文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
procedure FormPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
RadioGroup1.Items.CommaText := 'Path,FlattenPath,WidenPath';
RadioGroup1.ItemIndex := 0;
end;
procedure TForm1.FormPaint(Sender: TObject);
type
TPArr = array[0..0] of TPoint;
TTArr = array[0..0] of Byte;
var
pts: ^TPArr;
types: ^TTArr;
count: Integer;
i,x,y: Integer;
begin
Canvas.Font.Size := 150;
Canvas.Font.Style := [fsBold];
SetBkMode(Canvas.Handle, TRANSPARENT);
BeginPath(Canvas.Handle);
Canvas.TextOut(50, 0, 'D');
Canvas.Arc(20, 20, 220, 220, 120, 120, 20, 120);
EndPath(Canvas.Handle);
Canvas.Pen.Width := 6;
if RadioGroup1.ItemIndex = 1 then FlattenPath(Canvas.Handle);
if RadioGroup1.ItemIndex = 2 then WidenPath(Canvas.Handle);
Canvas.Pen.Color := clWhite;
count := GetPath(Canvas.Handle, pts^, types^, 0);
GetMem(pts, count*SizeOf(TPoint));
GetMem(types, count);
count := GetPath(Canvas.Handle, pts^, types^, count);
Text := '路徑中點的總數是: ' + IntToStr(count);
StrokePath(Canvas.Handle);
Canvas.Brush.Color := clRed;
for i := 0 to count - 1 do
begin
x := pts^[i].X;
y := pts^[i].Y;
Canvas.FillRect(Rect(x-1,y-1,x+1,y+1));
end;
FreeMem(pts);
FreeMem(types);
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
Repaint;
end;
end.
窗體文件:
object Form1: TForm1
Left = 352
Top = 227
Caption = 'Form1'
ClIEntHeight = 215
ClIEntWidth = 339
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesigned
OnCreate = FormCreate
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object RadioGroup1: TRadioGroup
Left = 240
Top = 80
Width = 91
Height = 127
Caption = 'RadioGroup1'
TabOrder = 0
OnClick = RadioGroup1Click
end
end
關於描繪路徑中的點, 參見: WinAPI: GetPath - 獲取路徑中的點