今天在VC8.0中使用 GDI+,? 但有很多編譯錯(cuò)誤,向 VC8.0中不支持返回默認(rèn)int 這類錯(cuò)誤在VC6.0中可以編譯,起初被這類錯(cuò)誤搞的直迷糊,后來(lái)將 stdafx.h 中的宏定義
//#define WIN32_LEAN_AND_MEAN?
注釋掉就可以正常編譯了。
??1?//?win32app.cpp?:?定義應(yīng)用程序的入口點(diǎn)。
??2?//
??3?
??4?#include?"stdafx.h"
??5?#include?"win32app.h"
??6?#include?<gdiplus.h>
??7?
??8?using?namespace?Gdiplus;
??9?
?10?#define?MAX_LOADSTRING?100
?11?
?12?//?全局變量:
?13?HINSTANCE?hInst;????????????????????????????????//?當(dāng)前實(shí)例
?14?TCHAR?szTitle[MAX_LOADSTRING];????????????????????//?標(biāo)題欄文本
?15?TCHAR?szWindowClass[MAX_LOADSTRING];????????????//?主窗口類名
?16?
?17?//?此代碼模塊中包含的函數(shù)的前向聲明:
?18?ATOM????????????????MyRegisterClass(HINSTANCE?hInstance);
?19?BOOL????????????????InitInstance(HINSTANCE,?int);
?20?LRESULT?CALLBACK????WndProc(HWND,?UINT,?WPARAM,?LPARAM);
?21?INT_PTR?CALLBACK????About(HWND,?UINT,?WPARAM,?LPARAM);
?22?
?23?int?APIENTRY?_tWinMain(HINSTANCE?hInstance,
?24??????????????????????HINSTANCE?hPrevInstance,
?25??????????????????????LPTSTR????lpCmdLine,
?26??????????????????????int???????nCmdShow)
?27?{
?28?????//UNREFERENCED_PARAMETER(hPrevInstance);
?29?????//UNREFERENCED_PARAMETER(lpCmdLine);
?30?
?31??????//?TODO:?在此放置代碼。
?32?????GdiplusStartupInput?gdiplusStartupInput;
?33?????ULONG_PTR?gdiplusToken;
?34?????GdiplusStartup(&gdiplusToken,?&gdiplusStartupInput,?NULL);
?35?
?36?????MSG?msg;
?37?????HACCEL?hAccelTable;
?38?
?39?????//?初始化全局字符串
?40?????LoadString(hInstance,?IDS_APP_TITLE,?szTitle,?MAX_LOADSTRING);
?41?????LoadString(hInstance,?IDC_WIN32APP,?szWindowClass,?MAX_LOADSTRING);
?42?????MyRegisterClass(hInstance);
?43?
?44?????//?執(zhí)行應(yīng)用程序初始化:
?45?????if?(!InitInstance?(hInstance,?nCmdShow))
?46?????{
?47?????????return?FALSE;
?48?????}
?49?
?50?????hAccelTable?=?LoadAccelerators(hInstance,?MAKEINTRESOURCE(IDC_WIN32APP));
?51?
?52?????//?主消息循環(huán):
?53?????while?(GetMessage(&msg,?NULL,?0,?0))
?54?????{
?55?????????if?(!TranslateAccelerator(msg.hwnd,?hAccelTable,?&msg))
?56?????????{
?57?????????????TranslateMessage(&msg);
?58?????????????DispatchMessage(&msg);
?59?????????}
?60?????}
?61?
?62?????GdiplusShutdown(gdiplusToken);
?63?
?64?????return?(int)?msg.wParam;
?65?}
?66?
?67?
?68?
?69?//
?70?//??函數(shù):?MyRegisterClass()
?71?//
?72?//??目的:?注冊(cè)窗口類。
?73?//
?74?//??注釋:
?75?//
?76?//????僅當(dāng)希望
?77?//????此代碼與添加到?Windows?95?中的“RegisterClassEx”
?78?//????函數(shù)之前的?Win32?系統(tǒng)兼容時(shí),才需要此函數(shù)及其用法。調(diào)用此函數(shù)十分重要,
?79?//????這樣應(yīng)用程序就可以獲得關(guān)聯(lián)的
?80?//????“格式正確的”小圖標(biāo)。
?81?//
?82?ATOM?MyRegisterClass(HINSTANCE?hInstance)
?83?{
?84?????WNDCLASSEX?wcex;
?85?
?86?????wcex.cbSize?=?sizeof(WNDCLASSEX);
?87?
?88?????wcex.style????????????=?CS_HREDRAW?|?CS_VREDRAW;
?89?????wcex.lpfnWndProc????=?WndProc;
?90?????wcex.cbClsExtra????????=?0;
?91?????wcex.cbWndExtra????????=?0;
?92?????wcex.hInstance????????=?hInstance;
?93?????wcex.hIcon????????????=?LoadIcon(hInstance,?MAKEINTRESOURCE(IDI_WIN32APP));
?94?????wcex.hCursor????????=?LoadCursor(NULL,?IDC_ARROW);
?95?????wcex.hbrBackground????=?(HBRUSH)(COLOR_WINDOW+1);
?96?????wcex.lpszMenuName????=?MAKEINTRESOURCE(IDC_WIN32APP);
?97?????wcex.lpszClassName????=?szWindowClass;
?98?????wcex.hIconSm????????=?LoadIcon(wcex.hInstance,?MAKEINTRESOURCE(IDI_SMALL));
?99?
100?????return?RegisterClassEx(&wcex);
101?}
102?
103?//
104?//???函數(shù):?InitInstance(HINSTANCE,?int)
105?//
106?//???目的:?保存實(shí)例句柄并創(chuàng)建主窗口
107?//
108?//???注釋:
109?//
110?//????????在此函數(shù)中,我們?cè)谌肿兞恐斜4鎸?shí)例句柄并
111?//????????創(chuàng)建和顯示主程序窗口。
112?//
113?BOOL?InitInstance(HINSTANCE?hInstance,?int?nCmdShow)
114?{
115????HWND?hWnd;
116?
117????hInst?=?hInstance;?//?將實(shí)例句柄存儲(chǔ)在全局變量中
118?
119????hWnd?=?CreateWindow(szWindowClass,?szTitle,?WS_OVERLAPPEDWINDOW,
120???????CW_USEDEFAULT,?0,?CW_USEDEFAULT,?0,?NULL,?NULL,?hInstance,?NULL);
121?
122????if?(!hWnd)
123????{
124???????return?FALSE;
125????}
126?
127????ShowWindow(hWnd,?nCmdShow);
128????UpdateWindow(hWnd);
129?
130????return?TRUE;
131?}
132?
133?void?OnPaint(HDC?hdc)
134?{
135????/*
136????Graphics?graphics(hdc);
137????Pen??????pen(Color(255,?0,?0,?255));
138????graphics.DrawLine(&pen,?0,?0,?200,?100);
139????*/
140????Graphics????graphics(hdc);
141????SolidBrush??brush(Color(255,?0,?0,?255));
142????FontFamily??fontFamily(L"Times?New?Roman");
143????Font????????font(&fontFamily,?24,?FontStyleRegular,?UnitPixel);
144????PointF??????pointF(10.0f,?20.0f);
145????graphics.DrawString(L"Hello?World!",?-1,?&font,?pointF,?&brush);
146?}
147?//
148?//??函數(shù):?WndProc(HWND,?UINT,?WPARAM,?LPARAM)
149?//
150?//??目的:?處理主窗口的消息。
151?//
152?//??WM_COMMAND????-?處理應(yīng)用程序菜單
153?//??WM_PAINT????-?繪制主窗口
154?//??WM_DESTROY????-?發(fā)送退出消息并返回
155?//
156?//
157?LRESULT?CALLBACK?WndProc(HWND?hWnd,?UINT?message,?WPARAM?wParam,?LPARAM?lParam)
158?{
159?????int?wmId,?wmEvent;
160?????PAINTSTRUCT?ps;
161?????HDC?hdc;
162?
163?????switch?(message)
164?????{
165?????case?WM_COMMAND:
166?????????wmId????=?LOWORD(wParam);
167?????????wmEvent?=?HIWORD(wParam);
168?????????//?分析菜單選擇:
169?????????switch?(wmId)
170?????????{
171?????????case?IDM_ABOUT:
172?????????????DialogBox(hInst,?MAKEINTRESOURCE(IDD_ABOUTBOX),?hWnd,?About);
173?????????????break;
174?????????case?IDM_EXIT:
175?????????????DestroyWindow(hWnd);
176?????????????break;
177?????????default:
178?????????????return?DefWindowProc(hWnd,?message,?wParam,?lParam);
179?????????}
180?????????break;
181?????case?WM_PAINT:
182?????????hdc?=?BeginPaint(hWnd,?&ps);
183?????????OnPaint(hdc);
184?????????EndPaint(hWnd,?&ps);
185?????????break;
186?????case?WM_DESTROY:
187?????????PostQuitMessage(0);
188?????????break;
189?????default:
190?????????return?DefWindowProc(hWnd,?message,?wParam,?lParam);
191?????}
192?????return?0;
193?}
194?
195?//?“關(guān)于”框的消息處理程序。
196?INT_PTR?CALLBACK?About(HWND?hDlg,?UINT?message,?WPARAM?wParam,?LPARAM?lParam)
197?{
198?????UNREFERENCED_PARAMETER(lParam);
199?????switch?(message)
200?????{
201?????case?WM_INITDIALOG:
202?????????return?(INT_PTR)TRUE;
203?
204?????case?WM_COMMAND:
205?????????if?(LOWORD(wParam)?==?IDOK?||?LOWORD(wParam)?==?IDCANCEL)
206?????????{
207?????????????EndDialog(hDlg,?LOWORD(wParam));
208?????????????return?(INT_PTR)TRUE;
209?????????}
210?????????break;
211?????}
212?????return?(INT_PTR)FALSE;
213?}
214?