有关BCB使用GDI+

来源:互联网 发布:ps软件怎么写字 编辑:程序博客网 时间:2024/06/05 17:07

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

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

1、建立编译链接环境:
  GDI+主要是通过gdiplus.dll进行调用,而BCB没有直接提供与gdiplus.dll对应的静态链接库,所以需要我们自己建立。完成后切记要把gdiplus.lib添加到工程中。

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>

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;
}

 

原创粉丝点击