通過畫筆的 DashStyle 屬性可設置或讀取虛線樣式.
通過 SetDashPattern 方法或 DashPattern 屬性可自定義虛線樣式.
通過 DashPattern 屬性還可以讀出已有樣式定義時的數組.
自定義虛線樣式樣式時, 需要元素數是雙數的數組數組, 元素值依次表示實線與空白的長度.
通過 DashOffset 屬性可設置虛線偏移.
系統提供的虛線樣式效果:
自定義的兩種樣式效果:
虛線偏移效果:
測試系統提供的虛線樣式:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics: IGPGraphics;
Pen: IGPPen;
Pt1,Pt2: TGPPoint;
i: Integer;
begin
Graphics := TGPGraphics.Create(Handle);
Pt1.Initialize(20 , 20 );
Pt2.Initialize(200 , 20);
Pen := TGPPen.Create(TGPColor.Red, 3);
for i := 0 to 4 do
begin
Pen.DashStyle := TGPDashStyle(i);
Graphics.DrawLine(Pen, Pt1, Pt2);
Graphics.TranslateTransform(0, Pt1.Y);
end;
end;
測試自定義樣式:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
const
DashPatternArr: array[0..1] of Single = (16, 2);
var
Graphics: IGPGraphics;
Pen: IGPPen;
Pt1,Pt2: TGPPoint;
begin
Pt1.Initialize(20, 20);
Pt2.Initialize(200, 20);
Graphics := TGPGraphics.Create(Handle);
Pen := TGPPen.Create($FF0000FF, 2);
Pen.SetDashPattern([8, 2, 1, 2]);
Graphics.DrawLine(Pen, Pt1, Pt2);
Graphics.TranslateTransform(0, Pt1.Y);
Pen.SetDashPattern(DashPatternArr);
Graphics.DrawLine(Pen, Pt1, Pt2);
Text := IntToStr(Ord(Pen.DashStyle)); //此值應該是 5, 也就是自定義樣式 DashStyleCustom
end;
虛線偏移測試:
uses GdiPlus;
procedure TForm1.FormPaint(Sender: TObject);
var
Graphics: IGPGraphics;
Pen: IGPPen;
Pt1,Pt2: TGPPoint;
begin
Graphics := TGPGraphics.Create(Handle);
Pt1.Initialize(20, 30);
Pt2.Initialize(200, 30);
Pen := TGPPen.Create($FFC71585, 4);
Pen.DashStyle := DashStyleDashDotDot;
Graphics.DrawLine(Pen, Pt1, Pt2);
Graphics.TranslateTransform(0, Pt1.Y);
Pen.DashOffset := Pen.DashPattern[0] + Pen.DashPattern[1];
Graphics.DrawLine(Pen, Pt1, Pt2);
end;