您的位置:首页 > 其它

vs2005 初始化GDI+环境

2011-07-21 17:32 169 查看
第一步:建立新的GdiNew.h头文件,并将其包含在stdafx.h中

#define iterator _iterator

#ifdef _DEBUG

namespace Gdiplus
{
namespace DllExports
{
#include <GdiplusMem.h>
};

#ifndef _GDIPLUSBASE_H
#define _GDIPLUSBASE_H

class GdiplusBase
{
public:
void (operator delete)(void* in_pVoid)
{
DllExports::GdipFree(in_pVoid);
}

void* (operator new)(size_t in_size)
{
return DllExports::GdipAlloc(in_size);
}

void (operator delete[])(void* in_pVoid)
{
DllExports::GdipFree(in_pVoid);
}

void* (operator new[])(size_t in_size)
{
return DllExports::GdipAlloc(in_size);
}

void * (operator new)(size_t nSize, LPCSTR lpszFileName, int nLine)
{
return DllExports::GdipAlloc(nSize);
}

void operator delete(void* p, LPCSTR lpszFileName, int nLine)
{
DllExports::GdipFree(p);
}

};
#endif // #ifndef _GDIPLUSBASE_H
}
#endif // #ifdef _DEBUG

#include <gdiplus.h>
#undef iterator
//// Ensure that Gdiplus.lib is linked.
#pragma comment(lib, "gdiplus.lib")


在stdafx.h包含GdiNew.h

#include "GdiNew.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")


第二步: 在类中添加成员变量

GdiplusStartupInput gdiplusStartupInput;

ULONG_PTR gdiplusToken;

第三步: 在类的初始化Init()中添加

GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

第四步: 在销毁函数ExitInstance()或类析构函数中添加

GdiplusShutdown(gdiplusToken);

................................................................................................
................................................................................................

可能出现的一些问题:

在第一步: 在stdafx.h头文件中 加入gdi+头文件时

#include <gdiplus.h>

using namespace Gdiplus;

#pragma comment(lib, "gdiplus.lib")

注意: 这样有时可能会跳出106个错误

类似 error C2146: 语法错误 : 缺少“;”(在标识符“GraphicsState”的前面)

error C2065: “META_SETBKCOLOR”: 未声明的标识符

原因:gdi+ 头文件中的一些宏定义未声明

解决方法: 在 #inclue<gdiplus.h>前 加入新的头文件 #include<afxdtctl.h> 即用如下代码:

#include <afxdtctl.h>

#include <gdiplus.h>

using namespace Gdiplus;

#pragma comment(lib, "gdiplus.lib")

以下为网上资料 : 在SDI单文档程序中,如何使用GDI+
1

在stdafx.h内添加如下代码:
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif
#include "GdiPlus.h"
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib")


2
在GdiApp.h里的CGdiAppApp中加入两个成员:
GdiplusStartupInput m_gdiplusStartupInput;//
ULONG_PTR m_pGdiToken;//


3
在GdiApp.cpp中的CGdiAppApp构造函数CGdiAppApp()添加如下代码:
GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);//


4
在GdiApp.h里给CGdiAppApp添加一个析构函数~CGdiAppApp();


5
在GdiApp.cpp中添加CGdiAppApp的析构函数~CGdiAppApp():

CGdiAppApp::~CGdiAppApp()//

{

GdiplusShutdown(m_pGdiToken);//

}
6
在GdiAppView.h的CGdiAppView中添加成员变量:
Image* m_pImage;  // 原文是BITMAP  不过我觉得还是使用Image方便些
并在GdiAppView.cpp中的GdiAppView构造函数和析构函数中添加如下代码:
CGdiAppView::CGdiAppView():m_pImage(0)//
{
// TODO: add construction code here
}
CGdiAppView::~CGdiAppView()
{
delete m_pImage;//
m_pImage=0;//
}


参考资料:
http://www.qylz.net/code/20110430/2854.html
http://topic.csdn.net/u/20091109/12/6f40ed2b-8e3c-465c-a49f-3f184d95fedd.html
http://d.download.csdn.net/down/2751392/hastings
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: