下面用 VC6 来写一个 GdiPlus 的 Demo 工程
Step1:新建一个名为 Demo_GdiPlus 的 MFC AppWizard(exe) 工程
操作步骤:
(1)主菜单File->New...,选择 Projects 选项卡;
(2)在工程类型列表中选中 MFC AppWizard(exe);
(3)Project name 填入 Demo_GdiPlus,按 OK 进入下一页;
(4)选择单文档(Single document)类型的程序框架,按 Finish 完成工程创建工作。
Step2:添加头文件声明
在 StdAfx.h 中添加以下代码:
//
{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
typedef unsigned long ULONG_PTR, * PULONG_PTR;
#include < gdiplus.h >
using namespace Gdiplus;
#pragma comment (lib, "GdiPlus.lib")
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
typedef unsigned long ULONG_PTR, * PULONG_PTR;
#include < gdiplus.h >
using namespace Gdiplus;
#pragma comment (lib, "GdiPlus.lib")
Step3:在 CDemo_GdiPlusApp 中增加成员变量 m_gdiplusToken,并在构造函数中进行初始化
class
CDemo_GdiPlusApp :
public
CWinApp
{
private :
ULONG_PTR m_gdiplusToken;
// …… ……
};
CDemo_GdiPlusApp::CDemo_GdiPlusApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_gdiplusToken = NULL;
}
{
private :
ULONG_PTR m_gdiplusToken;
// …… ……
};
CDemo_GdiPlusApp::CDemo_GdiPlusApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_gdiplusToken = NULL;
}
Step4:添加安装和卸载 GdiPlus 的代码
通过 ClassWizard 在 CDemo_GdiPlusApp 中增加成员函数
//
.h 中的声明
virtual BOOL InitInstance();
virtual int ExitInstance();
// .cpp 中的实现
BOOL CDemo_GdiPlusApp::InitInstance()
{
// 加载 GdiPlus
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup( & m_gdiplusToken, & gdiplusStartupInput, NULL);
// …… ……
}
int CDemo_GdiPlusApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// 卸载 GdiPlus
if (m_gdiplusToken)
Gdiplus::GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
virtual BOOL InitInstance();
virtual int ExitInstance();
// .cpp 中的实现
BOOL CDemo_GdiPlusApp::InitInstance()
{
// 加载 GdiPlus
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup( & m_gdiplusToken, & gdiplusStartupInput, NULL);
// …… ……
}
int CDemo_GdiPlusApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// 卸载 GdiPlus
if (m_gdiplusToken)
Gdiplus::GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
Step5:找到 CDemo_GdiPlusView::OnDraw() 函数,在里面添加一段 GdiPlus 的绘图代码
void
CDemo_GdiPlusView::OnDraw(CDC
*
pDC)
{
CDemo_GdiPlusDoc * pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Graphics graphics(pDC -> GetSafeHdc());
// Pen can also be constructed using a brush or another pen. There is a second parameter - a width which defaults to 1.0f
Pen blue (Color( 255 , 0 , 0 , 255 ));
Pen red (Color( 255 , 255 , 0 , 0 ));
int y = 256 ;
for ( int x = 0 ; x < 256 ; x += 5 )
{
graphics.DrawLine( & blue, 0 , y, x, 0 );
graphics.DrawLine( & red, 256 , x, y, 256 );
y -= 5 ;
}
}
{
CDemo_GdiPlusDoc * pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Graphics graphics(pDC -> GetSafeHdc());
// Pen can also be constructed using a brush or another pen. There is a second parameter - a width which defaults to 1.0f
Pen blue (Color( 255 , 0 , 0 , 255 ));
Pen red (Color( 255 , 255 , 0 , 0 ));
int y = 256 ;
for ( int x = 0 ; x < 256 ; x += 5 )
{
graphics.DrawLine( & blue, 0 , y, x, 0 );
graphics.DrawLine( & red, 256 , x, y, 256 );
y -= 5 ;
}
}
编译运行,Demo 程序完成。留住这个 Demo 程序,今后我们会利用它进行更加深入的 GdiPlus 学习。