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

GDI+ 可以自定义线帽, 本例定义的起始线帽是一个小矩形、终止线帽是一个小三角形.

本例效果图:

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

代码文件:

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


 
   

你可能感兴趣的:(start)