GdiPlus[2]: 获取绘图表面(Graphics)

本文介绍使用GdiPlus在Delphi中绘制图形的方法,包括从窗口句柄、HDC、Canvas及图像对象获取绘图表面(IGPGraphics),并通过具体代码示例展示了如何用这些方法画椭圆。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

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

本例效果图:

26153528_ycwX.png

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.

 
 
 
 
 

 

 

  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值