《Windows程序设计》1至3章,代码注释汇总

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <Windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
// win32中hPrevInstance总是NULL
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
truestatic TCHAR szAppName[] = TEXT("HelloWin");
trueHWND hwnd; // 视窗代号
trueMSG msg; // 讯息结构
truetruetruetrue/*
truetruetruetruetruetypedef struct tagMSG {
truetruetruetruetruetrueHWND hwnd;
truetruetruetruetruetrueUINT message;
truetruetruetruetruetrueWPARAM wParam; // 一个32位元的讯息参数
truetruetruetruetruetrueLPARAM lParam; // 一个32位元的讯息参数
truetruetruetruetruetrueDWORD time; // 讯息放入讯息伫列的时间
truetruetruetruetruetruePOINT pt; // 讯息放入讯息伫列时的滑鼠坐标
truetruetruetruetrue} MSG, *PMSG;
truetruetruetruetruetypedef struct tagPOINT {
truetruetruetruetruetrueLONG x;
truetruetruetruetruetrueLONG y;
truetruetruetruetrue} POINT, *PPOINT;
truetruetruetrue*/
trueWNDCLASS wndclass; // 视窗类别结构
truetruetruetrue/*
truetruetruetruetrue// Unicode版本
truetruetruetruetruetypedef struct tagWNDCLASSW {
truetruetruetruetruetrueUINT style;
truetruetruetruetruetrueWNDPROC lpfnWndProc;
truetruetruetruetruetrueint cbClsExtra;
truetruetruetruetruetrueint cbWndExtra;
truetruetruetruetruetrueHINSTANCE hInstance;
truetruetruetruetruetrueHICON hIcon;
truetruetruetruetruetrueHCURSOR hCursor;
truetruetruetruetruetrueHBRUSH hbrBackground;
truetruetruetruetruetrueLPCWSTR lpszMenuName;
truetruetruetruetruetrueLPCWSTR lpszClassName;
truetruetruetruetrue} WNDCLASSW, *PWNDCLASSW, NEAR *NPWNDCLASSW, FAR *LPWNDCLASSW;
truetruetruetrue*/
truewndclass.style = CS_HREDRAW | CS_VREDRAW;
true// 处理依据这个视窗类别建立的所有视窗的全部信息(提供的是指向函数的指针)
truewndclass.lpfnWndProc = WndProc; // lpfn:指向函数的长指针
truewndclass.cbClsExtra = 0; // 预留的位元数组
truewndclass.cbWndExtra = 0;
truewndclass.hInstance = hInstance; // 程序执行实体代号
truewndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
truewndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
truewndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); // 背景颜色
truewndclass.lpszMenuName = NULL; // 视窗类别功能表
truewndclass.lpszClassName = szAppName;
trueif (!RegisterClass(&wndclass)) { // 注册视窗类别
truetrueMessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
truetruereturn 0;
true}
true// 传回被建立的视窗的代号,如果一个程序建立了许多视窗,则每个视窗具有一个代号
truehwnd = CreateWindow( szAppName, // window class
TEXT("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL, // creation parameters
true);
trueShowWindow(hwnd, iCmdShow);
trueUpdateWindow(hwnd);
true// 讯息回圈
truewhile (GetMessage(&msg, NULL, 0, 0)) {
truetrueTranslateMessage(&msg);
truetrueDispatchMessage(&msg);
true}
truereturn msg.wParam;
}
// 必须返回0
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
trueHDC hdc; // 装置内容代号
truePAINTSTRUCT ps; // 绘图结构
truetruetruetrue/*
truetruetruetruetruetypedef struct tagPAINTSTRUCT {
truetruetruetruetruetrue// 使用者程序使用
truetruetruetruetruetrueHDC hdc;
truetruetruetruetruetrueBOOL fErase; // 大多数时候为FALSE(0),意味着已经擦除了无效矩形的背景
truetruetruetruetruetrueRECT rcPaint; // 无效边框的边界
truetruetruetruetruetrue// windows内部使用
truetruetruetruetruetrueBOOL fRestore;
truetruetruetruetruetrueBOOL fIncUpdate;
truetruetruetruetruetrueBYTE rgbReserved[32];
truetruetruetruetrue} PAINTSTRUCT;
truetruetruetrue*/
true// 四个栏位为:left, top, right, bottom,以像素为单位,相对于显示区域的左上角
trueRECT rect; // 矩形结构
truestatic int i = 0;
trueswitch(message) {
truecase WM_CREATE:
truetruereturn 0;
truecase WM_PAINT:
truetruehdc = BeginPaint(hwnd, &ps); // 从BeginPaint取得的hdc为无效区域
truetrueGetClientRect(hwnd, &rect);
truetruestatic TCHAR szBuffer[40];
truetrueDrawText(hdc, TEXT("Hello, Windows 7!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
truetrueTextOut(hdc, 40, 60, szBuffer, wsprintf(szBuffer, TEXT("Hello, Windows %d"), i++));
truetrueEndPaint(hwnd, &ps);
truetruereturn 0;
truecase WM_DESTROY:
truetruePostQuitMessage(0); // 在讯息伫列中插入一个WM_QUIT讯息
truetruereturn 0;
true}
truereturn DefWindowProc(hwnd, message, wParam, lParam);
}