Windows§S? (
(
fl
I K
JJ II
J I
11 13

w?
’ 4
a127 2007c ˇ? §
2007c12 7F ¢ ¢A405
Windows SDK§S O?:
§
=
&E? §?
chengjianjun@lzu.edu.cn
Windows§S? (
(
fl
I K
JJ II
J I
12 13

w?
’ 4
S N
a127 WindowsI §S? (
a127 WinMain…?
a127 I L§
a127 Windows§S Eˉ
a127 MSG( Na.
a127 E? E
a127 Windows§Sl ( L§
Windows§S? (
(
fl
I K
JJ II
J I
13 13

w?
’ 4
a127 Windows§S? (
~ { WindowsI §S
Windows§S? (
(
fl
I K
JJ II
J I
13 13

w?
’ 4
a127 Windows§S? (
~ { WindowsI §S
#include "windows.h"
char szClassName[]="FirstWindow";
char szAppTitle[]="? 1 WindowsI §S";
char szStrDisp[]=" [—§1 I?§S F"U < ^";
LRESULT CALLBACK WinProc(HWND hWin,UINT uMsg,WPARAM wPrm,LPARAM lPrm);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)f
WNDCLASS wc;
HWND hWin;
MSG msgMessage;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpfnWndProc=WinProc;
wc.lpszClassName=szClassName;
wc.lpszMenuName=NULL;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
Windows§S? (
(
fl
I K
JJ II
J I
14 13

w?
’ 4
if(!RegisterClass(&wc))f
MessageBox(NULL,"5 I a }"," ",MB_OK);
return -1;
g
Windows§S? (
(
fl
I K
JJ II
J I
14 13

w?
’ 4
if(!RegisterClass(&wc))f
MessageBox(NULL,"5 I a }"," ",MB_OK);
return -1;
g
hWin=CreateWindow(szClassName,szAppTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,0);
if(!hWin)f
MessageBox(NULL,"M I }"," ",MB_OK);
return -1;
g
ShowWindow(hWin,nCmdShow);
UpdateWindow(hWin);
Windows§S? (
(
fl
I K
JJ II
J I
14 13

w?
’ 4
if(!RegisterClass(&wc))f
MessageBox(NULL,"5 I a }"," ",MB_OK);
return -1;
g
hWin=CreateWindow(szClassName,szAppTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,0);
if(!hWin)f
MessageBox(NULL,"M I }"," ",MB_OK);
return -1;
g
ShowWindow(hWin,nCmdShow);
UpdateWindow(hWin);
while(GetMessage(&msgMessage,NULL,0,0))f
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
g
return 0;
g
Windows§S? (
(
fl
I K
JJ II
J I
15 13

w?
’ 4
LRESULT CALLBACK WinProc(HWND hWin,UINT uMsg,WPARAM wPrm,LPARAM lPrm)f
switch(uMsg)f
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWin,uMsg,wPrm,lPrm);
g
return 0;
g
Windows§S? (
(
fl
I K
JJ II
J I
16 13

w?
’ 4
a127 WinMain()…? Windows§S \,
…?,
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,
LPSTR lpCmdLine,int nShowCmd);
Windows§S? (
(
fl
I K
JJ II
J I
16 13

w?
’ 4
a127 WinMain()…? Windows§S \,
…?,
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,
LPSTR lpCmdLine,int nShowCmd);
¥ a17
APIENTRY =) #define APIENTRY WINAPI
WINAPI·X …?N^ ‰§ O@k FAR PASCAL§dX
g?= N^,"FAR@ˇ‰′?far§,a
§Win32 2?^§332 VC¥ ‰′·?PASCAL …?o
l m? §…?N^ £ d N^…?gC n
Windows§S? (
(
fl
I K
JJ II
J I
16 13

w?
’ 4
a127 WinMain()…? Windows§S \,
…?,
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,
LPSTR lpCmdLine,int nShowCmd);
¥ a17
APIENTRY =) #define APIENTRY WINAPI
WINAPI·X …?N^ ‰§ O@k FAR PASCAL§dX
g?= N^,"FAR@ˇ‰′?far§,a
§Win32 2?^§332 VC¥ ‰′·?PASCAL …?o
l m? §…?N^ £ d N^…?gC n a17
HINSTANCE·Windows¥‰′Ya.§hInst· cA^§S
¢~?Y
Windows§S? (
(
fl
I K
JJ II
J I
16 13

w?
’ 4
a127 WinMain()…? Windows§S \,
…?,
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,
LPSTR lpCmdLine,int nShowCmd);
¥ a17
APIENTRY =) #define APIENTRY WINAPI
WINAPI·X …?N^ ‰§ O@k FAR PASCAL§dX
g?= N^,"FAR@ˇ‰′?far§,a
§Win32 2?^§332 VC¥ ‰′·?PASCAL …?o
l m? §…?N^ £ d N^…?gC n a17
HINSTANCE·Windows¥‰′Ya.§hInst· cA^§S
¢~?Ya17
LPSTR· i? G 32 a,§ uchar *a
.§lpCmdLine·?-1o?
Windows§S? (
(
fl
I K
JJ II
J I
16 13

w?
’ 4
a127 WinMain()…? Windows§S \,
…?,
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,
LPSTR lpCmdLine,int nShowCmd);
¥ a17
APIENTRY =) #define APIENTRY WINAPI
WINAPI·X …?N^ ‰§ O@k FAR PASCAL§dX
g?= N^,"FAR@ˇ‰′?far§,a
§Win32 2?^§332 VC¥ ‰′·?PASCAL …?o
l m? §…?N^ £ d N^…?gC n a17
HINSTANCE·Windows¥‰′Ya.§hInst· cA^§S
¢~?Ya17
LPSTR· i? G 32 a,§ uchar *a
.§lpCmdLine·?-1o? a17
nShowCmd ‰§S I w?,
Windows§S? (
(
fl
I K
JJ II
J I
17 13

w?
’ 4
a17WNDCLASS( Na,‰I /G!aI! I1I?5
Windows§S? (
(
fl
I K
JJ II
J I
17 13

w?
’ 4
a17WNDCLASS( Na,‰I /G!aI! I1I?5
‰′Xe
typedef struct _WNDCLASS {
UINT style; //I,
WNDPROC lpfnWndProc; // I L§
int cbClsExtra; //' WNDCLASS( NC i!?
int cbWndExtra; //' I ¢~ i!?
HINSTANCE hInstance; //A^§S?Y
HICON hIcon; //I aI?Y
HCURSOR hCursor; // I1I?Y
HBRUSH hbrBackground; // xM?Y
LPCTSTR lpszMenuName; //I
LPCTSTR lpszClassName; //I a
} WNDCLASS,*PWNDCLASS;
Windows§S? (
(
fl
I K
JJ II
J I
17 13

w?
’ 4
a17WNDCLASS( Na,‰I /G!aI! I1I?5
‰′Xe
typedef struct _WNDCLASS {
UINT style; //I,
WNDPROC lpfnWndProc; // I L§
int cbClsExtra; //' WNDCLASS( NC i!?
int cbWndExtra; //' I ¢~ i!?
HINSTANCE hInstance; //A^§S?Y
HICON hIcon; //I aI?Y
HCURSOR hCursor; // I1I?Y
HBRUSH hbrBackground; // xM?Y
LPCTSTR lpszMenuName; //I
LPCTSTR lpszClassName; //I a
} WNDCLASS,*PWNDCLASS;
¥ a17
WNDPOROC· I L §(…?) a
.§HICON!HCURSOR HBRUSH ·?Ya.§LPCTSTR· ~
i?G 32 a.§ uconst char *a.
Windows§S? (
(
fl
I K
JJ II
J I
18 13

w?
’ 4
a17RegisterClass X 5 I a§–?M TI a
I,…?,
ATOM RegisterClass(CONST WNDCLASS *lpWndClass);
T…?N^§ £ 0 §?K £0
Windows§S? (
(
fl
I K
JJ II
J I
18 13

w?
’ 4
a17RegisterClass X 5 I a§–?M TI a
I,…?,
ATOM RegisterClass(CONST WNDCLASS *lpWndClass);
T…?N^§ £ 0 §?K £0a17
CreateWindow M I §T…?,
HWND CreateWindow(
LPCTSTR lpClassName,// registered class name
LPCTSTR lpWindowName,// window name
DWORD dwStyle,// window style
int x,// horizontal position of window
int y,// vertical position of window
int nWidth,// window width
int nHeight,// window height
HWND hWndParent,// handle to parent or owner window
HMENU hMenu,// menu handle or child identifier
HINSTANCE hInstance,// handle to application instance
LPVOID lpParam // window-creation data
);
Windows§S? (
(
fl
I K
JJ II
J I
18 13

w?
’ 4
a17RegisterClass X 5 I a§–?M TI a
I,…?,
ATOM RegisterClass(CONST WNDCLASS *lpWndClass);
T…?N^§ £ 0 §?K £0a17
CreateWindow M I §T…?,
HWND CreateWindow(
LPCTSTR lpClassName,// registered class name
LPCTSTR lpWindowName,// window name
DWORD dwStyle,// window style
int x,// horizontal position of window
int y,// vertical position of window
int nWidth,// window width
int nHeight,// window height
HWND hWndParent,// handle to parent or owner window
HMENU hMenu,// menu handle or child identifier
HINSTANCE hInstance,// handle to application instance
LPVOID lpParam // window-creation data
);a17
ShowWindow – ‰,w?M I
Windows§S? (
(
fl
I K
JJ II
J I
18 13

w?
’ 4
a17RegisterClass X 5 I a§–?M TI a
I,…?,
ATOM RegisterClass(CONST WNDCLASS *lpWndClass);
T…?N^§ £ 0 §?K £0a17
CreateWindow M I §T…?,
HWND CreateWindow(
LPCTSTR lpClassName,// registered class name
LPCTSTR lpWindowName,// window name
DWORD dwStyle,// window style
int x,// horizontal position of window
int y,// vertical position of window
int nWidth,// window width
int nHeight,// window height
HWND hWndParent,// handle to parent or owner window
HMENU hMenu,// menu handle or child identifier
HINSTANCE hInstance,// handle to application instance
LPVOID lpParam // window-creation data
);a17
ShowWindow – ‰,w?M I a17
UpdateWindow M#I r?
Windows§S? (
(
fl
I K
JJ II
J I
19 13

w?
’ 4
a127 Eˉ ! E !I L§
Windows§S? (
(
fl
I K
JJ II
J I
19 13

w?
’ 4
a127 Eˉ ! E !I L§a17

^r3I k5§ WindowsI §S 1v
k ‰ ·?§ k? ˉ5"Windows§S 1·fl
§I u),fl §X …Tfl ^ E A^§S
1ˇ "
Windows§S? (
(
fl
I K
JJ II
J I
19 13

w?
’ 4
a127 Eˉ ! E !I L§a17

^r3I k5§ WindowsI §S 1v
k ‰ ·?§ k? ˉ5"Windows§S 1·fl
§I u),fl §X …Tfl ^ E A^§S
1ˇ "
E^ ( Na.‰′
typedef struct tagMSG {
HWND hwnd; //I?Y
UINT message; // E£fl?I£
WPARAM wParam // E 1 N\? § message
LPARAM lParam; // E 1 N\? § message
DWORD time; //fl ˇ m
POINT pt; //fl ˇ I1I §?4 I
}MSG,*PMSG;
Windows§S? (
(
fl
I K
JJ II
J I
19 13

w?
’ 4
a127 Eˉ ! E !I L§a17

^r3I k5§ WindowsI §S 1v
k ‰ ·?§ k? ˉ5"Windows§S 1·fl
§I u),fl §X …Tfl ^ E A^§S
1ˇ "
E^ ( Na.‰′
typedef struct tagMSG {
HWND hwnd; //I?Y
UINT message; // E£fl?I£
WPARAM wParam // E 1 N\? § message
LPARAM lParam; // E 1 N\? § message
DWORD time; //fl ˇ m
POINT pt; //fl ˇ I1I §?4 I
}MSG,*PMSG;
z I o E? §X ˇ A^§S fl – E
/,3d? ¥
Windows§S? (
(
fl
I K
JJ II
J I
110 13

w?
’ 4
a17 E
while(GetMessage(&msgMessage,NULL,0,0)){
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}
Windows§S? (
(
fl
I K
JJ II
J I
110 13

w?
’ 4
a17 E
while(GetMessage(&msgMessage,NULL,0,0)){
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}
N ^GetMessage()l E? ¥ E § N
^TranslateMessage()? E? 1 = §,2 N
^DispatchMessage'u I L§?1?n
Windows§S? (
(
fl
I K
JJ II
J I
110 13

w?
’ 4
a17 E
while(GetMessage(&msgMessage,NULL,0,0)){
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}
N ^GetMessage()l E? ¥ E § N
^TranslateMessage()? E? 1 = §,2 N
^DispatchMessage'u I L§?1?n
* GetMessage l E? ¥ I£ WM_QUIT E § £0?
K £ 0? E? ¥? E §{l§S
* TranslateMessage rJ[U E= i?U E§? =
i?U E?# \ E? § e
T E?1?n
* DispatchMessage r E'u I L§?1?n
Windows§S? (
(
fl
I K
JJ II
J I
111 13

w?
’ 4
a17I L§? E§?1?n
I L§?A … –§ 35 I a I a( N
C £ ~¥·wc? lpfnWndProc? D– A …= §
/o £ k ‰ a."
Windows§S? (
(
fl
I K
JJ II
J I
111 13

w?
’ 4
a17I L§? E§?1?n
I L§?A … –§ 35 I a I a( N
C £ ~¥·wc? lpfnWndProc? D– A …= §
/o £ k ‰ a."?e /,
LRESULT CALLBACK XXXXXXX(
HWND hwnd,// handle to window
UINT uMsg,// message identifier
WPARAM wParam,// first message parameter
LPARAM lParam // second message parameter
);
Windows§S? (
(
fl
I K
JJ II
J I
111 13

w?
’ 4
a17I L§? E§?1?n
I L§?A … –§ 35 I a I a( N
C £ ~¥·wc? lpfnWndProc? D– A …= §
/o £ k ‰ a."?e /,
LRESULT CALLBACK XXXXXXX(
HWND hwnd,// handle to window
UINT uMsg,// message identifier
WPARAM wParam,// first message parameter
LPARAM lParam // second message parameter
);
…?N? switch-case( §ˇ~? Xe
switch(uMsg){
case WM_DESTROY,// I I£ WM_DESTROY E
PostQuitMessage(0); // E? ¥ \I£ WM_QUIT E
break;
case,:,,?break;
case,:,,;break;
default:
return DefWindowProc(hWin,uMsg,wPrm,lPrm);
}
Windows§S? (
(
fl
I K
JJ II
J I
112 13

w?
’ 4
a127 (a17
Windows§S I?WinMain()…? I L§?A …?
Windows§S? (
(
fl
I K
JJ II
J I
112 13

w?
’ 4
a127 (a17
Windows§S I?WinMain()…? I L§?A …?a17
WinMain()·§S \,§ u?ˇC§S main()…?§ k
‰I?5!5 I a§,M I §wM#I §?
\ E § l E? ¥ E?1= !'u
Windows§S? (
(
fl
I K
JJ II
J I
112 13

w?
’ 4
a127 (a17
Windows§S I?WinMain()…? I L§?A …?a17
WinMain()·§S \,§ u?ˇC§S main()…?§ k
‰I?5!5 I a§,M I §wM#I §?
\ E § l E? ¥ E?1= !'ua17
I L§KI E 'u5 E?1?n§ …?N?
switch-case(
Windows§S? (
(
fl
I K
JJ II
J I
112 13

w?
’ 4
a127 (a17
Windows§S I?WinMain()…? I L§?A …?a17
WinMain()·§S \,§ u?ˇC§S main()…?§ k
‰I?5!5 I a§,M I §wM#I §?
\ E § l E? ¥ E?1= !'ua17
I L§KI E 'u5 E?1?n§ …?N?
switch-case( a17
WindowsI §SA#include <windows.h>
Windows§S? (
(
fl
I K
JJ II
J I
112 13

w?
’ 4
a127 (a17
Windows§S I?WinMain()…? I L§?A …?a17
WinMain()·§S \,§ u?ˇC§S main()…?§ k
‰I?5!5 I a§,M I §wM#I §?
\ E § l E? ¥ E?1= !'ua17
I L§KI E 'u5 E?1?n§ …?N?
switch-case( a17
WindowsI §SA#include <windows.h>a17
Windows§S $1L§
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I a17
\ E,l E? ¥ E§eT EI£ WM_QUIT§
KGetMessage()…? £0§( E §? ( §S
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I a17
\ E,l E? ¥ E§eT EI£ WM_QUIT§
KGetMessage()…? £0§( E §? ( §Sa17
TranslateMessage()? E?1 ¨§DispatchMessage()r E'
u I L§§I L§ E I£1?n
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I a17
\ E,l E? ¥ E§eT EI£ WM_QUIT§
KGetMessage()…? £0§( E §? ( §Sa17
TranslateMessage()? E?1 ¨§DispatchMessage()r E'
u I L§§I L§ E I£1?na17
§S?E
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I a17
\ E,l E? ¥ E§eT EI£ WM_QUIT§
KGetMessage()…? £0§( E §? ( §Sa17
TranslateMessage()? E?1 ¨§DispatchMessage()r E'
u I L§§I L§ E I£1?na17
§S?E a17
^r ′’4U ‰ UeAlt+F4 §X TI uxWM_CLOSE
E§ˇ~§S¥?n§u·DefWindowProc()?nT E
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I a17
\ E,l E? ¥ E§eT EI£ WM_QUIT§
KGetMessage()…? £0§( E §? ( §Sa17
TranslateMessage()? E?1 ¨§DispatchMessage()r E'
u I L§§I L§ E I£1?na17
§S?E a17
^r ′’4U ‰ UeAlt+F4 §X TI uxWM_CLOSE
E§ˇ~§S¥?n§u·DefWindowProc()?nT Ea17
DefWindowProc()′ WM_CLOSE §N^DestroyWindow()rI
§DestroyWindow()quxWM_DESTROY E
Windows§S? (
(
fl
I K
JJ II
J I
113 13

w?
’ 4
WindowsI §S $1L§a17
\WinMain()…?y ‰I?5yN^RegisterClass()…?5
I ayN^CreateWindow()…?M I £CreateWindow()M I
§ N^I L§?nWM_CREATE E?§ XwM#I a17
\ E,l E? ¥ E§eT EI£ WM_QUIT§
KGetMessage()…? £0§( E §? ( §Sa17
TranslateMessage()? E?1 ¨§DispatchMessage()r E'
u I L§§I L§ E I£1?na17
§S?E a17
^r ′’4U ‰ UeAlt+F4 §X TI uxWM_CLOSE
E§ˇ~§S¥?n§u·DefWindowProc()?nT Ea17
DefWindowProc()′ WM_CLOSE §N^DestroyWindow()rI
§DestroyWindow()quxWM_DESTROY Ea17
§S?WM_DESTROY E?n ·N^PostQuitMessage()…?§T
…?rWM_QUIT E \ E? § e 1 GetMessage()…
T E? £0§ E ( §l ( §S