如何在BCB中配置和使用GDI+技术?(附动画实例)

王朝vc·作者佚名  2006-01-08
宽屏版  字体: |||超大  

主页:www.maxss.net

Email: maxss.net@163.com

GDI+是微软公司推出的新一代图形设备接口,功能强大。不再像传统的GDI那样让人的心志严重地受挫,GDI+具有很多新特性(如支持Alpha、渐变色填充、反锯齿等),并具有面向对象特性,这使得开发人员可以更方便、更形象地进行GDI+开发。有关GDI+更详细的介绍大家可以查阅MSDN中的专栏。

但目前在BCB环境中使用GDI+进行开发则还需要进行一定的设置和步骤才能成功编译和链接。以下我就以BCB6为例进行简单的说明:

1、建立编译链接环境:

GDI+主要是通过gdiplus.dll进行调用,而BCB没有直接提供与gdiplus.dll对应的静态链接库,所以需要我们自己建立。如果在自己的计算机没有找到文件gdiplus.dll,可以到微软的官方网站进行下载。然后复制一份到自己的工程目录中,然后使用BCB提供的工具implib生成对应的静态链接库:

implib gdiplus.lib gdiplus.dll

完成后切记要把gdiplus.lib添加到工程中(使用BCB的"Project->Add to project..."命令)。

(注意:这两个文章都需要保存在工程目录中)

2、修改编译选项:

打开BCB菜单"Project->Options",点击"Directories/Conditionals"页,在"Conditionals defines:"中添加“STRICT”编译选项,如果有多项则需要用分号";"进行分隔。

3、在.cpp文件中的语句“#pragma hdrstop”后加入以下内容:

#include <algorithm>

using std::min;

using std::max;

4、在.cpp文件中的语句块“#pragma package(smart_init)...”后加入以下内容:

using namespace Gdiplus;

(可以参阅后面的源码例子)

5、最后注意要在.h文件中引入GDI+的头文件:

#include <Gdiplus.h>

6、现在就可以进行基于GDI+的开发了,下面是我为大家编写的一个产生旋转的正方形的简单动画效果的例子:

(注意:需要添加一个Timer控件,并设置其Interval属性为50)

Unit1.h:

//---------------------------------------------------------------------------

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ExtCtrls.hpp>

#include <Gdiplus.h>

//---------------------------------------------------------------------------

class TForm1 : public TForm

{

__published:// IDE-managed Components

TTimer *Timer1;

void __fastcall Timer1Timer(TObject *Sender);

private:// User declarations

Gdiplus::GdiplusStartupInput gdiplusStartupInput;

ULONG_PTR gdiplusToken;

public:// User declarations

__fastcall TForm1(TComponent* Owner);

__fastcall ~TForm1(void);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

Unit1.cpp:

//---------------------------------------------------------------------------

#include <vcl.h>

#include <math.h>

#pragma hdrstop

#include <algorithm>

using std::min;

using std::max;

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

using namespace Gdiplus;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

DoubleBuffered = true;

// 初始化GDI+

GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

}

//---------------------------------------------------------------------------

__fastcall TForm1::~TForm1()

{

// 闭关GDI+

GdiplusShutdown(gdiplusToken);

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)

{

const static int OX = 200, OY = 200;

const static REAL C = 140;

const static REAL PI = 3.14;

static REAL offset = 0;

POINT p[4];

REAL k[4];

// 生成正文形四角的新坐标值

for (int i=0; i < 4; i++)

{

k[i] = offset + (PI / 2) * i;

p[i].x = (int)OX + C * sin(k[i]);

p[i].y = (int)OY + C * cos(k[i]);

}

Gdiplus::Graphics g(Canvas->Handle);

g.SetSmoothingMode(SmoothingModeHighQuality); //高画质、低速

// 重新填充背景

SolidBrush brush(Color::Color(0,0,0));

Pen pen(Color::Color(255, 255, 0), 3);

g.FillRectangle(&brush, 0, 0, ClientWidth, ClientHeight);

Gdiplus::Point point1(p[0].x, p[0].y);

Gdiplus::Point point2(p[1].x, p[1].y);

Gdiplus::Point point3(p[2].x, p[2].y);

Gdiplus::Point point4(p[3].x, p[3].y);

Gdiplus::Point point5(p[0].x, p[0].y);

// 在新坐标绘画正方形

Gdiplus::Point points[] = {point1, point2, point3, point4, point5};

g.DrawLines(&pen, points, 5);

offset += 0.1;

}

//---------------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有