由 VS2010中 已經有GDI+ ,所以就不用額外再安裝SDK,即可直接使用,若VC6就要另外安裝Windows SDK。
Step 1. 建立一個名為 GDITest 的 C++ Dialog專案,在 stdafx.h 加入以下GDI+ 的宣告。
#include "gdiplus.h" #pragma comment(lib,"gdiplus.lib") using namespace Gdiplus;
Step 2. 在 GDITestDlg.h 加入物件宣告
GdiplusStartupInput m_gdiplusStartupInput; ULONG_PTR m_pGdiToken;
Step 3. 在GDITestDlg.cpp 的 OnInitDialog 函數中,添加以下程式碼,初始化 GDI+ 物件。
GdiplusStartup(&m_pGdiToken, &m_gdiplusStartupInput, NULL);
Step 4. 在GDITestDlg.cpp 的 OnPaint 函數中,添加以下程式碼。
void CGDITestDlg::OnPaint()
{
CPaintDC dc(this);
CMemDC mdc(dc, this); //從VS2008含SP1之後才有支援,以雙緩衝防止畫面閃爍
Graphics grpx(mdc.GetDC());
Color *rgb = new Color(255, 255, 255);
Pen pen(Color(0,0,255), 2); //設定畫筆顏色與粗細
RectF rf;
CStringW str;
CRect rect;
//取得Dialog高寬
GetClientRect(&rect);
//設定Dialog顏色
grpx.Clear(*rgb);
//指定消除鋸齒的呈現
grpx.SetSmoothingMode(SmoothingModeAntiAlias);
//設定字型格式
FontFamily fontFamily(L"微軟正黑體");
int px = 20;
int style = FontStyleRegular;
Gdiplus::Font font(&fontFamily, (REAL)px, style, UnitPixel);
//設定字型內容
str = L"Hello GDI+!!";
//取得字串寬高
grpx.MeasureString(str, str.GetLength(), &font, PointF(0, 0), &rf );
//設定繪圖的位置
rf.X = rect.Width() / 2;
rf.Y = rect.Height() / 2 - rf.Height/2;
*rgb = Color(255, 0, 255);
//將X, Y平移至繪圖點上,並將X, Y 定為0, 0
grpx.TranslateTransform(rf.X, rf.Y);
for(int i = 0 ; i < 360 ; i+=45)
{
//設定文字旋轉角度
grpx.RotateTransform(i);
//畫矩形
grpx.DrawRectangle(&pen, 8, 5, 125, 20);
//設定文字為位置
rf.X = 10;
rf.Y = 0;
grpx.DrawString(str, str.GetLength(), &font, rf, 0, &SolidBrush(*rgb));
}
}
19行:設定繪圖的品質。SmoothingModeInvalid =-1; {指定一個無效模式}
SmoothingModeDefault =0; {指定不消除鋸齒}
SmoothingModeHighSpeed =1; {指定高速度、低品質呈現}
SmoothingModeHighQuality =2; {指定高品質、低速度呈現}
SmoothingModeNone =3; {指定不消除鋸齒}
SmoothingModeAntiAlias =4; {指定消除鋸齒的呈現}
參考資料:SmoothingMode Enumeration
22、25行:Font 為gdiplus中的Class。
參考資料:gdiplusheaders.h header
Step 5. 在GDITestDlg.cpp 的 OnDestroy() 函數中,添加以下程式碼。
GdiplusShutdown(m_pGdiToken);專案下載。
執行畫面

沒有留言:
張貼留言