再学 GDI+[37]: TGPPen - TGPCustomLineCap、SetCustomStartCap、SetCustomEndCap
再学 GDI+[37]: TGPPen - TGPCustomLineCap、SetCustomStartCap、SetCustomEndCap
涂孟超 发表于3年前
再学 GDI+[37]: TGPPen - TGPCustomLineCap、SetCustomStartCap、SetCustomEndCap
  • 发表于 3年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

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

本例效果图:



代码文件:
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

 
 
 
 
 

 

 

  
共有 人打赏支持
粉丝 12
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: