程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 再學 GDI+[37]: TGPPen - TGPCustomLineCap、SetCustomStartCap、SetCustomEndCap

再學 GDI+[37]: TGPPen - TGPCustomLineCap、SetCustomStartCap、SetCustomEndCap

編輯:Delphi

 GDI+ 可以自定義線帽, 本例定義的起始線帽是一個小矩形、終止線帽是一個小三角形.

  本例效果圖:

再學 GDI+[37]: TGPPen - TGPCustomLineCap、SetCustomStartCap、SetCustomEndCap 再學 GDI+[37]: TGPPen

  代碼文件:unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, ExtCtrls;
type
 TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Integer);
  procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Integer);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
var
 ph1,ph2: TGPGraphicsPath;
 cap1,cap2: TGPCustomLineCap;
 x1,y1,x2,y2: Integer;
 f: Boolean;
 PenColor: TColor;
procedure TForm1.FormCreate(Sender: TObject);
const
 pts: array[0..2] of TGPPoint = ((X:-3; Y:0),(X:3; Y:0),(X:0; Y:3));
begin
 {起始線帽需要的路徑}
 ph1 := TGPGraphicsPath.Create;
 ph1.AddRectangle(MakeRect(-2, 0, 4, 3));
 {終止線帽需要的路徑}
 ph2 := TGPGraphicsPath.Create;
 ph2.AddPolygon(PGPPoint(@pts), Length(pts));
 {自定義兩個線帽}
 cap1 := TGPCustomLineCap.Create(nil, ph1);
 cap2 := TGPCustomLineCap.Create(nil, ph2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
 ph1.Free;
 ph2.Free;
 cap1.Free;
 cap2.Free;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Integer);
begin
 f := True;
 x1 := X;
 y1 := Y;
 x2 := X;
 y2 := Y;
 Randomize;
 PenColor := Random($FFFFFF);       {給畫筆一個隨機顏色}
 Canvas.Pen.Color := $FFFFFF xor PenColor; {取畫筆反色, 為了在 pmXor 模式下顯示真實的顏色}
 Canvas.Pen.Width := 3;
 Canvas.Pen.Mode := pmXor;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
 Y: Integer);
begin
 if not f then Exit;
 Canvas.MoveTo(x1, y1);
 Canvas.LineTo(x2,y2);
 x2 := X;
 y2 := Y;
 Canvas.MoveTo(x1, y1);
 Canvas.LineTo(x2,y2);
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Integer);
var
 g: TGPGraphics;
 p: TGPPen;
begin
 if not f then Exit;
 f := False;
 g := TGPGraphics.Create(Canvas.Handle);
 p := TGPPen.Create(ColorRefToARGB(PenColor), 3);
 {使用自定義線帽}
 p.SetCustomStartCap(cap1);
 p.SetCustomEndCap(cap2);
 g.DrawLine(p, x1, y1, x2, y2);
 p.Free;
 g.Free;
end;
end.
窗體文件:object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClIEntHeight = 193
 ClIEntWidth = 296
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 Position = poDesktopCenter
 OnCreate = FormCreate
 OnDestroy = FormDestroy
 OnMouseDown = FormMouseDown
 OnMouseMove = FormMouseMove
 OnMouseUp = FormMouseUp
 PixelsPerInch = 96
 TextHeight = 13
end


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved