//
?INCLUDES?
//////////////////////////////////////////////
/
#define
?WIN32_LEAN_AND_MEAN??
//
?just?say?no?to?MFC
#include?
<
windows.h
>
???
//
?include?all?the?windows?headers
#include?
<
windowsx.h
>
??
//
?include?useful?macros
#include?
<
stdio.h
>
?????
#include?
<
math.h
>
//
?DEFINES?
////////////////////////////////////////////////
//
?defines?for?windows?
#define
?WINDOW_CLASS_NAME?"WINCLASS1"
//
?GLOBALS?
////////////////////////////////////////////////
//
?FUNCTIONS?
//////////////////////////////////////////////
LRESULT?CALLBACK?WindowProc(HWND?hwnd,?
????????????????????????????UINT?msg,?
????????????????????????????WPARAM?wparam,?
????????????????????????????LPARAM?lparam)
{
????
//
?this?is?the?main?message?handler?of?the?system
????PAINTSTRUCT????????ps;????????
//
?used?in?WM_PAINT
????HDC????????????????hdc;????
//
?handle?to?a?device?context
????
//
?what?is?the?message?
????
switch
(msg)
????
{????
????
case
?WM_CREATE:?
????????
{
????????????
//
?do?initialization?stuff?here
????????????
//
?return?success
????????????
return
(
0
);
????????}
?
break
;
????
case
?WM_PAINT:?
????????
{
????????????
//
?simply?validate?the?window
????????????hdc?
=
?BeginPaint(hwnd,
&
ps);?????
????????????
//
?you?would?do?all?your?painting?here
????????????EndPaint(hwnd,
&
ps);
????????????
//
?return?success
????????????
return
(
0
);
????????}
?
break
;
????
case
?WM_DESTROY:?
????????
{
????????????
//
?kill?the?application,?this?sends?a?WM_QUIT?message?
????????????PostQuitMessage(
0
);
????????????
//
?return?success
????????????
return
(
0
);
????????}
?
break
;
????
default
:
break
;
????}
?
//
?end?switch
????
//
?process?any?messages?that?we?didn't?take?care?of?
????
return
?(DefWindowProc(hwnd,?msg,?wparam,?lparam));
}
?
//
?end?WinProc
//
?WINMAIN?
////////////////////////////////////////////////
int
?WINAPI?WinMain(????HINSTANCE?hinstance,
???????????????????HINSTANCE?hprevinstance,
???????????????????LPSTR?lpcmdline,
???????????????????
int
?ncmdshow)
{
????WNDCLASSEX?winclass;?
//
?this?will?hold?the?class?we?create
????HWND???????hwnd;?????
//
?generic?window?handle
????MSG???????????msg;?????????
//
?generic?message
????
//
?first?fill?in?the?window?class?stucture
????winclass.cbSize?????????
=
?
sizeof
(WNDCLASSEX);
????winclass.style????????????
=
?CS_DBLCLKS?
|
?CS_OWNDC?
|
?
????????CS_HREDRAW?
|
?CS_VREDRAW;
????winclass.lpfnWndProc????
=
?WindowProc;
????winclass.cbClsExtra????????
=
?
0
;
????winclass.cbWndExtra????????
=
?
0
;
????winclass.hInstance????????
=
?hinstance;
????winclass.hIcon????????????
=
?LoadIcon(NULL,?IDI_APPLICATION);
????winclass.hCursor????????
=
?LoadCursor(NULL,?IDC_ARROW);
????winclass.hbrBackground????
=
?(HBRUSH)GetStockObject(BLACK_BRUSH);
????winclass.lpszMenuName????
=
?NULL;
????winclass.lpszClassName????
=
?WINDOW_CLASS_NAME;
????winclass.hIconSm????????
=
?LoadIcon(NULL,?IDI_APPLICATION);
????
//
?register?the?window?class
????
if
?(
!
RegisterClassEx(
&
winclass))
????????
return
(
0
);
????
//
?create?the?window
????
if
?(
!
(hwnd?
=
?CreateWindowEx(NULL,??????????????????
//
?extended?style
????????WINDOW_CLASS_NAME,?????
//
?class
????????
"
Your?Basic?Window++
"
,?
//
?title
????????WS_OVERLAPPEDWINDOW?
|
?WS_VISIBLE,
????????
0
,
0
,????????
//
?initial?x,y
????????
400
,
400
,??
//
?initial?width,?height
????????NULL,????????
//
?handle?to?parent?
????????NULL,????????
//
?handle?to?menu
????????hinstance,
//
?instance?of?this?application
????????NULL)))????
//
?extra?creation?parms
????????
return
(
0
);
????
//
?enter?main?event?loop,?but?this?time?we?use?PeekMessage()
????
//
?instead?of?GetMessage()?to?retrieve?messages
????
while
(TRUE)
????
{
????????
//
?test?if?there?is?a?message?in?queue,?if?so?get?it
????????
if
?(PeekMessage(
&
msg,NULL,
0
,
0
,PM_REMOVE))
????????
{?
????????????
//
?test?if?this?is?a?quit
????????????
if
?(msg.message?
==
?WM_QUIT)
????????????????
break
;
????????????
//
?translate?any?accelerator?keys
????????????TranslateMessage(
&
msg);
????????????
//
?send?the?message?to?the?window?proc
????????????DispatchMessage(
&
msg);
????????}
?
//
?end?if
????????
//
?main?game?processing?goes?here
????????
//
?Game_Main();?
//
?or?whatever?your?loop?is?called
????}
?
//
?end?while
????
//
?return?to?Windows?like?this
????
return
(msg.wParam);
}
?
//
?end?WinMain
一? include頭文件和宏定義
#define?WIN32_LEAN_AND_MEAN??//?不使用mfc
#include?<windows.h>???//?包含所有的windows頭文件,
#include?<windowsx.h>??//包含有用的宏定義二 winmain()函數
int?WINAPI?WinMain(????HINSTANCE?hinstance,
???????????????????HINSTANCE?hprevinstance,
???????????????????LPSTR?lpcmdline,
???????????????????int?ncmdshow);函數原型如上,其中hinstance是windows為應用程序生成的句柄,hprevinstance參數現在一般不用,用來向以前的兼容,lpcmdline就相當于dos程序的命令行參數,ncmdshow枚舉類型指出如何打開主應用程序窗口,比如最大化,最小化,最前端等
WINAPI 不能少,相當于以前的pascal 關鍵字
三 WNDCLASSEX 結構
????? WNDCLASSEX?winclass;?//?this?will?hold?the?class?we?create
????
????//?first?fill?in?the?window?class?stucture
????winclass.cbSize?????????=?sizeof(WNDCLASSEX);???????????????????? //指她本身的大小
????winclass.style????????????=?CS_DBLCLKS?|?CS_OWNDC?|??????? //窗口樣式,一般選這4個
????????CS_HREDRAW?|?CS_VREDRAW;
????winclass.lpfnWndProc????=?WindowProc;???????????????????????????????? //要回調的函數指針
????winclass.cbClsExtra????????=?0;???????????????????????????????????????????????????//額外的類信息空間,現在一般不用
????winclass.cbWndExtra????????=?0;????????????????????????????????????????????????//額外的窗口信息空間,現在一般不用
????winclass.hInstance????????=?hinstance;??????????????????????????????????????????//窗口的實例句柄,從winmain()傳來
????winclass.hIcon????????????=?LoadIcon(NULL,?IDI_APPLICATION); //圖標
????winclass.hCursor????????=?LoadCursor(NULL,?IDC_ARROW);?????? //鼠標
????winclass.hbrBackground????=?(HBRUSH)GetStockObject(BLACK_BRUSH); //背景刷,用于刷新窗口的畫刷句柄,可以用GetStockObject()
????winclass.lpszMenuName????=?NULL;?????????????????????? //菜單,要加入到窗口中的菜單名稱
????winclass.lpszClassName????=?WINDOW_CLASS_NAME;?? //要創建的窗口類的類名
????winclass.hIconSm????????=?LoadIcon(NULL,?IDI_APPLICATION); //圖標,顯示在標題蘭和狀態蘭
四 注冊windows類
RegisterClassEx(&winclass); 傳入指向類的指針
五 創建窗口
CreateWindowEx(NULL,??????????????????//?extended?style?????? 擴張的窗口樣式,高級,一般不用
????????WINDOW_CLASS_NAME,?????//?class??????? 創建窗口需要的類指針
????????"Your?Basic?Window++",?//?title??????????? 標題蘭的文本
????????WS_OVERLAPPEDWINDOW?|?WS_VISIBLE,???????? //窗口樣式
????????0,0,????????//?initial?x,y?? 窗口的左上角,象素表示
????????400,400,??//?initial?width,?height 寬高,象素表示
????????NULL,????????//?handle?to?parent? 父窗口句柄
????????NULL,????????//?handle?to?menu??????? 菜單句柄或子窗口標示
????????hinstance,//?instance?of?this?application? winmain(0中的instance
????????NULL))???//?extra?creation?parms? 高級參數,一般不用六 顯示窗口且刷新一下
ShowWindow()//可以控制不顯示,或在狀態蘭也不顯示
UpdateWindow() //刷新窗口,就是生成一個WM_PAINT消息
七 主消息循環
LRESULT?CALLBACK?WindowProc(HWND?hwnd,? //窗口句柄
????????????????????????????UINT?msg,?????????????????? //消息id
????????????????????????????WPARAM?wparam,??? //用于進一步確定msg指定的消息
????????????????????????????LPARAM?lparam)????? //用于進一步確定msg指定的消息LRESULT?CALLBACK 不能少,以下是簡單的幾種消息:
WM_CREATE: //可以此時執行各種初始化
WM_PAINT:??? ?hdc? =?BeginPaint(hwnd,&ps);?????//確認窗口是否有效
?????????????????????? //?you?would?do?all?your?painting?here? //繪制工作
???????????? ???????? EndPaint(hwnd,&ps);??????????????????????? //結束繪制WM_KEYDOWN:? //處理鍵盤按下
WM_DESTROY:???? //將要關閉應用程序,發出WM_QUIT消息
WM_QUIT:??????????? //推出程序
注意 函數DefWindowProc(),是處理其他的消息,除了WindowProc()已經處理的其他消息.
GetMessage(LPMSG,?????? //消息結構的地址
??????????????????? hWnd,??????????????? //窗口的句柄
?????????????????????uint,????????????????? //first message
?????????????????????? uint)?????????????????? //last messge
她從事件隊列獲得下一個消息,然后調用TranslateMessage()函數,進行消息的轉換和處理,然后通過DispatchMessage()來調用winproc()函數.
PeekMessage( &msg,NULL,0,0,PM_REMOVE))中參數:消息結構的指針,窗口的句柄,第一條消息,最后一條消息,刪除標記 ,其中參數刪除標記是GetMessage()中沒有的,該標記有2個值,PM_NOREMOVE和PM_REMOVE,前一個經過peekmessage()后不將其從消息隊列中刪除,后一個表示經過peekmessage()后從消息隊列中刪除.


