在程序中使用GDI+的步骤为:
1, 添加库头文件,在include path中加入“C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\include\\”,具体要根据SDK的安装路径来定,也可以在代码中直接#include
2, 添加Lib库,直接引用#pragma comment(lib,"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\GDIplus.lib")
3, 使用GDI+命名空间using namespace Gdiplus;
4, GDI资源初始与销毁。在使用GDI+资源之间,应该通过GdiplusStartup函数进行GDI+系统资源的初始化,在程序结束前,调用GdiplusShutdown函数进行GDI+资源的销毁。在App类的InitInstance和ExitInstance或析购函数中分别调用。
code 示例如下:
CGDI_Plus_LearningApp theApp;
ULONG_PTR gdiplusToken; //定义全局变量,表明对GDI+的一个引用
/////////////////////////////////////////////////////////////////////////////
// CGDI_Plus_LearningApp initialization
BOOL CGDI_Plus_LearningApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object.
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
// create and load the frame with its resources
pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);
// The one and only window has been initialized, so show and update it.
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
int CGDI_Plus_LearningApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
Gdiplus::GdiplusShutdown(gdiplusToken); //也可以在析构函数中调用此函数
return CWinApp::ExitInstance();
}