再学 GDI+[57]: 路径 - Widen
再学 GDI+[57]: 路径 - Widen
涂孟超 发表于3年前
再学 GDI+[57]: 路径 - Widen
  • 发表于 3年前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0
路径的 Widen 方法可以把路径中的线, 根据指定画笔的宽度与样式, 转换为一个范围(有点类似区域); 但转换后再描绘路径就只能使用 FillPath 而不是 DrawPath 了. 本例没有测试它的两个默认参数, 因为前面已多次提到了.

本例效果图:



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

 
 
 
 
 

 

 

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