標籤:
下面用 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")
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;
}
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();
}
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;
}
}
編譯運行,Demo 程式完成。留住這個 Demo 程式,今後我們會利用它進行更加深入的 GdiPlus 學習。
在 VC6 中使用 GdiPlus-使用