在C++Builder中使用GDI+的流程:
1、 GDI+主要是通过gdiplus.dll进行调用,而BCB没有直接提供与gdiplus.dll对应的静态链接库,所以需要我们自己建立。如果在自己的计算机没有找到文件gdiplus.dll,可以到微软的官方网站进行下载。然后复制一份到自己的工程目录中,然后使用BCB提供的工具implib生成对应的静态链接库:implib gdiplus.lib gdiplus.dll。具体实现方法是在开始à运行àcmdàimplib 路径1/gdiplus.lib 路径2/gdiplus.dll 回车即可在指定的路径1处生成gdiplus.lib静态链接库,(搜索的方法找gdiplus.lib)然后将gdiplus.lib复制到C:/Program Files/Borland/CBuilder6/Lib中。
2、 使用BCB的”Project->Add to project…”命令把gdiplus.lib添加到工程中。
3、 修改编译选项:打开BCB菜单”Project->Options”和”Directories/Conditionals”在”Conditionals defines:”中添加“STRICT”编译选项,如果有多项则需要用分隔号”;”进行分隔。而这个选项也可以通过 #define STRICT 指令直接加到文件中。
4、 在.cpp文件中的语句“#pragma hdrstop”后加入以下内容:
include //运算法则
using std::min;
using std::max;
5、 在.cpp文件中的语句块“#pragma package(smart_init)…”后加入以下内容:using namespace Gdiplus;
6、 在工程头文件中加入#include
ifndef Unit1H
define Unit1H
//—————————————————————————
include
include
include
include
include “sButton.hpp”
include “sSkinManager.hpp”
include
include
endif
/**********Unit1.cpp*********/
include
pragma hdrstop
include //运算法则
using std::min;
using std::max;
include “Unit1.h”
include
pragma package(smart_init)
pragma link “gdiplus.lib”
pragma link “sButton”
pragma link “sSkinManager”
pragma resource “*.dfm”
TForm1 *Form1;
using namespace Gdiplus;
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DoubleBuffered = true;
// 初始化GDI+
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
}
//—————————————————————————
__fastcall TForm1::~TForm1()
{
// 闭关GDI+
GdiplusShutdown(m_gdiplusToken);
}
//—————————————————————————
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
}
//—————————————————————————
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
//GdiplusShutdown(m_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;
}
//—————————————————————————
void __fastcall TForm1::sButton1Click(TObject *Sender)
{
//实现一个颜色渐变的椭圆和直线
Gdiplus::Graphics graphics(this->Image1->Canvas->Handle);
LinearGradientBrush linGrBrush(
Gdiplus::Point(0, 10), //需要加Gdiplus::指定Point(),Color函数属于Gdiplus类
Gdiplus::Point(200, 10),
Gdiplus::Color(255, 255, 0, 0), // opaque red
Gdiplus::Color(255, 0, 0, 255)); // opaque blue
Pen pen(&linGrBrush);
graphics.DrawLine(&pen, 0, 10, 200, 10); //直线
graphics.FillEllipse(&linGrBrush, 0, 30, 200, 100);//椭圆
graphics.FillRectangle(&linGrBrush, 0, 155, 500, 30);//长方形
}
//—————————————————————————