• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi中使用GDI+进行绘图(1)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Delphi的VCL类库中,默认使用的是GDI绘图接口,该接口封装了Win32 GDI接口,能够满足基本的绘图功能,但如果要实现更高级的绘图功能,往往比较困难,GDI+是微软在GDI之后的一个图形接口,功能比GDI丰富很多,在VCL中使用GDI+,能够实现很多高级绘图功能。

 

目前有多种Delphi对GDI+的封装实现,以下介绍最简单的两种:

 

1、使用Delphi内置的GDI+接口

 

首先,创建一个VCL Form应用,窗口文件如下:

Pascal Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
object GDIPlusDemoForm1: TGDIPlusDemoForm1
  Left = 0 
  Top = 0 
  Caption = 'GDIPlusDemoForm1' 
  ClientHeight = 247 
  ClientWidth = 524 
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11 
  Font.Name = 'Tahoma' 
  Font.Style = []
  OldCreateOrder = False 
  PixelsPerInch = 96 
  TextHeight = 13 
  object pb1: TPaintBox
    Left = 24 
    Top = 16 
    Width = 473 
    Height = 209 
    OnPaint = pb1Paint
  end 
end 

 

代码如下:

Pascal Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
unit GDIPlusDemo1;

interface 

uses 
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls,
  Winapi.GDIPAPI, Winapi.GDIPOBJ, Winapi.GDIPUTIL;

type 
  TGDIPlusDemoForm1 = class(TForm)
    pb1: TPaintBox;
    procedure pb1Paint(Sender: TObject);
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end;

var 
  GDIPlusDemoForm1: TGDIPlusDemoForm1;

implementation 

{$R *.dfm} 

procedure TGDIPlusDemoForm1.pb1Paint(Sender: TObject);
var 
  g: TGPGraphics;
  p: TGPPen;
  b: TGPBrush;
  r: TGPRect;
begin 
  g := TGPGraphics.Create(pb1.Canvas.Handle);
  p := TGPPen.Create(aclRed, 2);
  b := TGPSolidBrush.Create(aclAliceBlue);
  try 
    r := MakeRect(20, 20, 100, 60);
    g.FillRectangle(b, r);
    g.DrawRectangle(p, r);
  finally 
    p.Free;
    b.Free;
    g.Free;
  end;
end;

end.

以下是运行结果:

其中Winapi.GDIPAPI, Winapi.GDIPOBJ, Winapi.GDIPUTIL三个单元是Delphi提供的对GDI+的封装,可以看到使用GDI+是很方便的。

 

仔细看代码可以发现,使用Delphi内置的GDI+支持,还是有些不足,关键的地方是所有的绘图对象都是普通的对象,需要手工释放,增加了很多不必要的代码。

Delphi利用接口可以实现自动的垃圾收集,所以使用接口可以有效地简化代码。

http://blog.sina.com.cn/s/blog_591968570102vwsw.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Font.Style(Delphi)发布时间:2022-07-18
下一篇:
Delphi多线程编程之同步读写全局数据发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap