码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-04-11 22:33:49      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/findumars/p/5380264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!