GdiPlus[2]: 获取绘图表面(Graphics)
GdiPlus[2]: 获取绘图表面(Graphics)
涂孟超 发表于3年前
GdiPlus[2]: 获取绘图表面(Graphics)
  • 发表于 3年前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   


绘图表面(Graphics, 这在 VCL 体系中叫 Canvas), 在 GdiPlus 中有四种获取方法:

1、通过窗口句柄获取;
2、通过窗口的 Canvas.Handle 获取;
3、通过 GdiPlus 利用 Helper 技术给部分 VCL 对象添加的 ToGPGraphics 方法获取;
4、通过图像对象获取.

本例效果图:



unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses GdiPlus, GdiPlusHelpers;

//从窗口句柄获取 IGPGraphics
procedure TForm1.Button1Click(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
begin
  Graphics := TGPGraphics.Create(Handle);
  Pen := TGPPen.Create(TGPColor.Red, 2);
  Graphics.DrawEllipse(Pen, 20, 10, 150, 40);
end;

//从 HDC 获取 IGPGraphics
procedure TForm1.Button2Click(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
begin
  Graphics := TGPGraphics.Create(Canvas.Handle);
  Pen := TGPPen.Create(TGPColor.Green, 2);
  Graphics.DrawEllipse(Pen, 20, 40, 150, 40);
end;

//使用 GdiPlusHelpers 为 Canvas 添加的 ToGPGraphics 方法获取 IGPGraphics
procedure TForm1.Button3Click(Sender: TObject);
var
  Graphics: IGPGraphics;
  Pen: IGPPen;
begin
  Graphics := Canvas.ToGPGraphics;
  Pen := TGPPen.Create(TGPColor.Blue, 2);
  Graphics.DrawEllipse(Pen, 20, 70, 150, 40);
end;

//从图像建立 IGPGraphics
procedure TForm1.Button4Click(Sender: TObject);
var
  GraphicsImg: IGPGraphics;
  Pen: IGPPen;
  Image: IGPImage;
begin
  Image := TGPBitmap.Create(152, 42);
  GraphicsImg := TGPGraphics.Create(Image);
  Pen := TGPPen.Create(TGPColor.Fuchsia, 2);
  GraphicsImg.DrawEllipse(Pen, 0, 0, Image.Width-2, Image.Height-2);

  Canvas.ToGPGraphics.DrawImage(Image, 20, 100);
end;

end.

 
 
 
 
 

 

 

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