這篇文章介紹VC6下如何使用GDI+開發。
1. 下載下傳GDI+開發包,微軟的網站可以下載下傳到。大家找不到的話,可以在http://alantop.5166.info下載下傳。或者找我索取均可。
2. 在VC中的include files 和 library files設定,并保證程式工作目錄下有gdiplus.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. 在構造函數中:
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
析構函數中:
GdiplusShutdown(gdiplusToken);
6. OnDraw函數鍵入如下代碼
注意下面的注釋掉的代碼是CDC和HDC如何獲得,和他們互相如何轉換的。
這裡重要的是要提醒:
用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屬于一個視窗類,ReleaseDC必須在畫圖後被調用。在任何時候隻有5個一般的DC有效,如果沒有釋放,将阻止其他的應用程式通路DC.