通过程序理解windows程序的运行机制
vc6.0下的:
Code
1#include <windows.h>
2#include <stdio.h>
3
4LRESULT CALLBACK WinSunProc(
5 HWND hwnd, // handle to window
6 UINT uMsg, // message identifier
7 WPARAM wParam, // first message parameter
8 LPARAM lParam // second message parameter
9);
10
11int WINAPI WinMain(
12 HINSTANCE hInstance, // handle to current instance
13 HINSTANCE hPrevInstance, // handle to previous instance
14 LPSTR lpCmdLine, // command line
15 int nCmdShow // show state
16)
17{
18 WNDCLASS wndcls;
19 wndcls.cbClsExtra=0;
20 wndcls.cbWndExtra=0;
21 wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
22 wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
23 wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
24 wndcls.hInstance=hInstance;
25 wndcls.lpfnWndProc=WinSunProc;
26 wndcls.lpszClassName="Windows";
27 wndcls.lpszMenuName=NULL;
28 wndcls.style=CS_HREDRAW | CS_VREDRAW;
29 RegisterClass(&wndcls);
30
31 HWND hwnd;
32 hwnd=CreateWindow("Windows","典型Windows程序",WS_OVERLAPPEDWINDOW,
33 0,0,600,400,NULL,NULL,hInstance,NULL);
34
35 ShowWindow(hwnd,SW_SHOWNORMAL);
36 UpdateWindow(hwnd);
37
38 MSG msg;
39 while(GetMessage(&msg,NULL,0,0))
40 {
41 TranslateMessage(&msg);
42 DispatchMessage(&msg);
43 }
44 return 0;
45}
46
47LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
48{
49 switch(uMsg)
50 {
51 case WM_CHAR:
52 char szChar[20];
53 sprintf(szChar,"char is %d",wParam);
54 MessageBox(hwnd,szChar,"Windows",0);
55 break;
56 case WM_LBUTTONDOWN:
57 MessageBox(hwnd,"mouse clicked","weixin",0);
58 HDC hdc;
59 hdc=GetDC(hwnd);
60 TextOut(hdc,0,50,"鼠标左键单击",strlen("鼠标左键单击"));
61 ReleaseDC(hwnd,hdc);
62 break;
63 case WM_PAINT:
64 HDC hDC;
65 PAINTSTRUCT ps;
66 hDC=BeginPaint(hwnd,&ps);
67 TextOut(hDC,0,0,"Winows程序",strlen("Winows程序"));
68 EndPaint(hwnd,&ps);
69 break;
70 case WM_CLOSE:
71 if(IDYES==MessageBox(hwnd,"是否真的结束?","Winows",MB_YESNO))
72 {
73 DestroyWindow(hwnd);
74 }
75 break;
76 case WM_DESTROY:
77 PostQuitMessage(0);
78 break;
79 default:
80 return DefWindowProc(hwnd,uMsg,wParam,lParam);
81 }
82 return 0;
83}
1#include <windows.h>
2#include <stdio.h>
3
4LRESULT CALLBACK WinSunProc(
5 HWND hwnd, // handle to window
6 UINT uMsg, // message identifier
7 WPARAM wParam, // first message parameter
8 LPARAM lParam // second message parameter
9);
10
11int WINAPI WinMain(
12 HINSTANCE hInstance, // handle to current instance
13 HINSTANCE hPrevInstance, // handle to previous instance
14 LPSTR lpCmdLine, // command line
15 int nCmdShow // show state
16)
17{
18 WNDCLASS wndcls;
19 wndcls.cbClsExtra=0;
20 wndcls.cbWndExtra=0;
21 wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
22 wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
23 wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
24 wndcls.hInstance=hInstance;
25 wndcls.lpfnWndProc=WinSunProc;
26 wndcls.lpszClassName="Windows";
27 wndcls.lpszMenuName=NULL;
28 wndcls.style=CS_HREDRAW | CS_VREDRAW;
29 RegisterClass(&wndcls);
30
31 HWND hwnd;
32 hwnd=CreateWindow("Windows","典型Windows程序",WS_OVERLAPPEDWINDOW,
33 0,0,600,400,NULL,NULL,hInstance,NULL);
34
35 ShowWindow(hwnd,SW_SHOWNORMAL);
36 UpdateWindow(hwnd);
37
38 MSG msg;
39 while(GetMessage(&msg,NULL,0,0))
40 {
41 TranslateMessage(&msg);
42 DispatchMessage(&msg);
43 }
44 return 0;
45}
46
47LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
48{
49 switch(uMsg)
50 {
51 case WM_CHAR:
52 char szChar[20];
53 sprintf(szChar,"char is %d",wParam);
54 MessageBox(hwnd,szChar,"Windows",0);
55 break;
56 case WM_LBUTTONDOWN:
57 MessageBox(hwnd,"mouse clicked","weixin",0);
58 HDC hdc;
59 hdc=GetDC(hwnd);
60 TextOut(hdc,0,50,"鼠标左键单击",strlen("鼠标左键单击"));
61 ReleaseDC(hwnd,hdc);
62 break;
63 case WM_PAINT:
64 HDC hDC;
65 PAINTSTRUCT ps;
66 hDC=BeginPaint(hwnd,&ps);
67 TextOut(hDC,0,0,"Winows程序",strlen("Winows程序"));
68 EndPaint(hwnd,&ps);
69 break;
70 case WM_CLOSE:
71 if(IDYES==MessageBox(hwnd,"是否真的结束?","Winows",MB_YESNO))
72 {
73 DestroyWindow(hwnd);
74 }
75 break;
76 case WM_DESTROY:
77 PostQuitMessage(0);
78 break;
79 default:
80 return DefWindowProc(hwnd,uMsg,wParam,lParam);
81 }
82 return 0;
83}
vs2008下的:
Code
1#include<windows.h>
2
3HINSTANCE hInst;
4HWND wndHandle;
5
6bool initWindow(HINSTANCE hInstance);
7LRESULT CALLBACK WndProc(HWND ,UINT ,WPARAM ,LPARAM );
8
9int WINAPI WinMain(
10 HINSTANCE hInstance,
11 HINSTANCE hPrevInstance,
12 LPTSTR lpCmdLine,
13 int nCmdShow
14)
15{
16 if(!initWindow(hInstance))
17 {
18 return false;
19 }
20 MSG msg;
21 ZeroMemory(&msg,sizeof(msg));
22 while(msg.message!=WM_QUIT)
23 {
24 while(GetMessage(&msg,NULL,0,0))
25 {
26 TranslateMessage(&msg);
27 DispatchMessage(&msg);
28 }
29 }
30 return (int)msg.wParam;
31}
32bool initWindow(HINSTANCE hInstance)
33{
34 WNDCLASSEX wcex;
35 wcex.cbSize=sizeof(WNDCLASSEX);
36 wcex.style=CS_HREDRAW|CS_VREDRAW;
37 wcex.lpfnWndProc=(WNDPROC)WndProc;
38 wcex.cbClsExtra=0;
39 wcex.cbWndExtra=0;
40 wcex.hInstance=hInstance;
41 wcex.hIcon=0;
42 wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
43 wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
44 wcex.lpszMenuName=NULL;
45 wcex.lpszClassName="DirectxExample";
46 wcex.hIconSm=0;
47 RegisterClassEx(&wcex);
48
49 wndHandle=CreateWindow("DirectxExample","DirectxExample",
50 WS_OVERLAPPEDWINDOW,
51 CW_USEDEFAULT,
52 CW_USEDEFAULT,
53 640,
54 480,
55 NULL,
56 NULL,
57 hInstance,
58 NULL);
59 if(!wndHandle)
60 return false;
61 ShowWindow(wndHandle,SW_SHOW);
62 UpdateWindow(wndHandle);
63 return true;
64}
65LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
66{
67 switch(message)
68 {
69 case WM_DESTROY:
70 PostQuitMessage(0);
71 break;
72 }
73 return DefWindowProc(hWnd,message,wParam,lParam);
74}
1#include<windows.h>
2
3HINSTANCE hInst;
4HWND wndHandle;
5
6bool initWindow(HINSTANCE hInstance);
7LRESULT CALLBACK WndProc(HWND ,UINT ,WPARAM ,LPARAM );
8
9int WINAPI WinMain(
10 HINSTANCE hInstance,
11 HINSTANCE hPrevInstance,
12 LPTSTR lpCmdLine,
13 int nCmdShow
14)
15{
16 if(!initWindow(hInstance))
17 {
18 return false;
19 }
20 MSG msg;
21 ZeroMemory(&msg,sizeof(msg));
22 while(msg.message!=WM_QUIT)
23 {
24 while(GetMessage(&msg,NULL,0,0))
25 {
26 TranslateMessage(&msg);
27 DispatchMessage(&msg);
28 }
29 }
30 return (int)msg.wParam;
31}
32bool initWindow(HINSTANCE hInstance)
33{
34 WNDCLASSEX wcex;
35 wcex.cbSize=sizeof(WNDCLASSEX);
36 wcex.style=CS_HREDRAW|CS_VREDRAW;
37 wcex.lpfnWndProc=(WNDPROC)WndProc;
38 wcex.cbClsExtra=0;
39 wcex.cbWndExtra=0;
40 wcex.hInstance=hInstance;
41 wcex.hIcon=0;
42 wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
43 wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
44 wcex.lpszMenuName=NULL;
45 wcex.lpszClassName="DirectxExample";
46 wcex.hIconSm=0;
47 RegisterClassEx(&wcex);
48
49 wndHandle=CreateWindow("DirectxExample","DirectxExample",
50 WS_OVERLAPPEDWINDOW,
51 CW_USEDEFAULT,
52 CW_USEDEFAULT,
53 640,
54 480,
55 NULL,
56 NULL,
57 hInstance,
58 NULL);
59 if(!wndHandle)
60 return false;
61 ShowWindow(wndHandle,SW_SHOW);
62 UpdateWindow(wndHandle);
63 return true;
64}
65LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
66{
67 switch(message)
68 {
69 case WM_DESTROY:
70 PostQuitMessage(0);
71 break;
72 }
73 return DefWindowProc(hWnd,message,wParam,lParam);
74}