再学 GDI+[57]: 路径 - Widen

GDI+ Widen 方法详解
本文详细介绍了GDI+中的Widen方法,该方法能够将路径中的线条根据指定画笔的宽度和样式转换为一个区域。文章通过一个具体实例展示了如何使用此方法,并提供了完整的代码实现。
路径的 Widen 方法可以把路径中的线, 根据指定画笔的宽度与样式, 转换为一个范围(有点类似区域); 但转换后再描绘路径就只能使用 FillPath 而不是 DrawPath 了. 本例没有测试它的两个默认参数, 因为前面已多次提到了.

本例效果图:

26153604_0TRG.gif

代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses GDIPOBJ, GDIPAPI;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CheckBox1.Caption := '执行 Widen';
  CheckBox2.Caption := '显示路径中所有的点';
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  g: TGPGraphics;
  p: TGPPen;
  b1,b2: TGPBrush;
  path: TGPGraphicsPath;
  pts: array of TGPPoint;
  i: Integer;
begin
  g := TGPGraphics.Create(Canvas.Handle);
  p := TGPPen.Create(aclSlateGray, 20);
  p.SetEndCap(LineCapArrowAnchor);
  b1 := TGPSolidBrush.Create(aclRed);
  b2 := TGPHatchBrush.Create(HatchStyleDiagonalCross, aclSilver, aclSlateGray);

  path := TGPGraphicsPath.Create;
  path.AddLine(40, 50, ClientWidth-40, 50);

  if CheckBox1.Checked then
  begin
    path.Widen(p);
    g.FillPath(b2, path);
  end else g.DrawPath(p, path);

  if CheckBox2.Checked then
  begin
    SetLength(pts, path.GetPointCount);
    path.GetPathPoints(PGPPoint(pts), Length(pts));
    TGPSolidBrush(b1).SetColor(aclRed);
    for i := 0 to Length(pts) - 1 do
      g.FillRectangle(b1, pts[i].X-3, pts[i].Y-3, 6, 6);
  end;

  path.Free;
  b1.Free;
  b2.Free;
  p.Free;
  g.Free;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  Repaint;
end;

procedure TForm1.CheckBox2Click(Sender: TObject);
begin
  Repaint;
end;

end.

 
 
 
 
 

 

 

  
窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 136
  ClientWidth = 287
  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 CheckBox1: TCheckBox
    Left = 43
    Top = 104
    Width = 97
    Height = 17
    Caption = 'CheckBox1'
    TabOrder = 0
    OnClick = CheckBox1Click
  end
  object CheckBox2: TCheckBox
    Left = 138
    Top = 104
    Width = 125
    Height = 17
    Caption = 'CheckBox2'
    TabOrder = 1
    OnClick = CheckBox2Click
  end
end

 
 
 
 
 

 

 

  

转载于:https://my.oschina.net/hermer/blog/320228

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值