zoukankan      html  css  js  c++  java
  • Delphi中使用GDI+进行绘图(1)

    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.

    以下是运行结果:

    image

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

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

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

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

  • 相关阅读:
    在asp.net中执行存储过程(转)
    Linux DM9000网卡驱动程序完全分析
    Linux内核态抢占机制分析(转)
    操作系统原理(转)
    Keil4 每次选build 编译(F7)都全部编译的解决办法
    内核必看: spinlock、 mutex 以及 semaphore
    linux内核代码container_of
    Win10玩魔兽争霸不能全屏显示的设置教程
    在vmware里面免费安装纯净的xp虚拟机
    IAR之工程配置
  • 原文地址:https://www.cnblogs.com/findumars/p/5380264.html
Copyright © 2011-2022 走看看