本例效果圖:
代碼文件:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
LabeledEdit1: TLabeledEdit;
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure LabeledEdit1Change(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
var
arr1: array[0..5] of Single; {用於均分線復合畫筆; 數組大小沒有限制, 可用動態數 組}
arr2: array[0..3] of Single; {用於粗細線復合畫筆; 數組大小沒有限制, 可用動態數 組}
procedure TForm1.FormCreate(Sender: TObject);
begin
{均分線復合畫筆; 6 個元素決定 3 條子線; 數組元素為百分比, 應在 0-1 之間}
arr1[0] := 0.00; {第一線起點}
arr1[1] := 0.10; {第一線終點}
arr1[2] := 0.45; {第二線起點}
arr1[3] := 0.55; {第二線終點}
arr1[4] := 0.90; {第三線起點}
arr1[5] := 1.00; {第三線終點}
{粗細線復合畫筆; 4 個元素決定 2 條子線; }
arr2[0] := 0.00; {第一線起點}
arr2[1] := 0.50; {第一線終點}
arr2[2] := 0.90; {第二線起點}
arr2[3] := 1.00; {第二線終點}
LabeledEdit1.EditLabel.Caption := '筆寬';
LabeledEdit1.Text := '10';
RadioGroup1.Caption := '線型';
RadioGroup1.Items.CommaText := '單實線,均分線,粗細線';
RadioGroup1.ItemIndex := 0;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
g: TGPGraphics;
p: TGPPen;
r: TRect;
begin
g := TGPGraphics.Create(Canvas.Handle);
g.Clear($FFFFFFFF);
p := TGPPen.Create(aclCoral);
p.SetWidth(StrToFloatDef(LabeledEdit1.Text, 10));
case RadioGroup1.ItemIndex of
1: p.SetCompoundArray(@arr1, Length(arr1));
2: p.SetCompoundArray(@arr2, Length(arr2));
end;
g.DrawLine(p, 0, 0, ClientWidth, ClientHeight);
r := ClientRect;
g.DrawEllipse(p, MakeRect(r));
InflateRect(r, -ClientWidth div 4, -ClientHeight div 4);
g.DrawLine(p, 0, 0, ClientWidth, ClientHeight);
g.DrawRectangle(p, MakeRect(r));
p.Free;
g.Free;
end;
procedure TForm1.LabeledEdit1Change(Sender: TObject);
begin
Repaint;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
Repaint;
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 206
ClientWidth = 339
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 LabeledEdit1: TLabeledEdit
Left = 264
Top = 19
Width = 67
Height = 21
EditLabel.Width = 61
EditLabel.Height = 13
EditLabel.Caption = 'LabeledEdit1'
TabOrder = 0
OnChange = LabeledEdit1Change
end
object RadioGroup1: TRadioGroup
Left = 264
Top = 46
Width = 67
Height = 66
Caption = 'RadioGroup1'
TabOrder = 1
OnClick = RadioGroup1Click
end
end