這篇文章介紹VC6下如何使用GDI+開發(fā)。
1. 下載GDI+開發(fā)包,微軟的網(wǎng)站可以下載到。大家找不到的話,可以在http://alantop.5166.info下載。或者找我索取均可。
2. 在VC中的include files 和 library files設(shè)置,并保證程序工作目錄下有g(shù)diplus.dll
3. 在stdafx.h中包含文件的最下面鍵入如下代碼
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long *
#endif
#include "gdiplus.h"
using namespace Gdiplus;
4. 在視圖類中聲明成員變量
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
5. 在構(gòu)造函數(shù)中:
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
析構(gòu)函數(shù)中:
GdiplusShutdown(gdiplusToken);
6. OnDraw函數(shù)鍵入如下代碼
注意下面的注釋掉的代碼是CDC和HDC如何獲得,和他們互相如何轉(zhuǎn)換的。
這里重要的是要提醒:
用GetDC()獲得的CDC* dc一定要用ReleaseDC(dc)釋放掉。
// HDC hdc;
// CDC *dc=GetDC();
// hdc=dc->GetSafeHdc();



Image image(L"c:\\china.jpg");
printf("The width of the image is %u.\n", image.GetWidth());
printf("The height of the image is %u.\n", image.GetHeight());



Graphics graphics(pDC->GetSafeHdc());
Pen pen(Color(255, 0, 0, 255));
graphics.DrawLine(&pen, 0, 0, 200, 100);



graphics.DrawImage(&image, 0, 0);

graphics.DrawLine(&pen, 0, 0, 200, 100);



// ReleaseDC(dc);

除非DC屬于一個(gè)窗口類,ReleaseDC必須在畫圖后被調(diào)用。在任何時(shí)候只有5個(gè)一般的DC有效,如果沒有釋放,將阻止其他的應(yīng)用程序訪問DC.
在VC2005下使用GDIPLUS不用再去下載gdiplus庫,系統(tǒng)已經(jīng)內(nèi)置了。