// note for hellowin
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口过程
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
//szCmdLine sz string-zero
//hinstance h for handle
//iCmdShow i for int
//PSTR ---> char *
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; // 定义一个窗口类对象
wndclass.style = CS_HREDRAW | CS_VREDRAW;// 定义类的风格
wndclass.lpfnWndProc = WndProc; //设定窗口过程
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; //WinMain的第一个参数
//基于该窗口类设定图标
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);/// 1 loadicon 加载程序图标
//当鼠标指针出现在这类窗口的客户区内时 将 变成一个箭头
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);/// 2 loadcursor 加载鼠标光标
//指定背景色 hbr--> handle to a brush//for 填充像素的着色模式
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);/// 3 getstockobject 获取图形对象 本例中用来对窗口背景进行重绘
// WHITE_BURSH 返回一个 白色画刷
//menu
wndclass.lpszMenuName = NULL;
//窗口类 的 名称
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))/// 4 为应用程序的敞口 注册一个窗口类
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),/// 5 显示消息框
szAppName, MB_ICONERROR);
return 0;
}
//createwindow 的 返回值 为一个指向所创建窗口的句柄
hwnd = CreateWindow(szAppName, // window class name/// 6 createwindow 创建新窗口
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
//下面这两行勇于窗口的显示
ShowWindow(hwnd, iCmdShow);/// 7 在屏幕中 显示窗口
UpdateWindow(hwnd);/// 8 指示窗口对其自身进行重绘
//updatawindow向wndproc 发送 vm_paint 完成
//Windows为每一个运行着的Windows程序都维护着一个消息队列
//当输入事件发生后 Windows会自动将这些事件 转化为消息 放在 消息队列中
while (GetMessage(&msg, NULL, 0, 0))/// 9 从消息队列获取消息
{
TranslateMessage(&msg);/// 10 翻译 一些键盘消息
DispatchMessage(&msg);/// 11 将消息发送给窗口过程
}
return msg.wParam;
}
//一个Windows程序可以包含多个窗口过程
// 但是一个窗口过程总是与一个通过调用registerclass注册的 窗口类 想关联
// createwindow是基于特定的窗口类来创建窗口 基于一个窗口类可以创建多个窗口
/*
typedef struct tagMSG
{
HWND hwnd;
UNIT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
)MSG;
*/
// hwnd for handle 接受消息的窗口句柄
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)/// return long
{//uint--> unsigned int //WPARAM //LPARAM ----> long //32
//
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:///
PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);/// 12 播放声音文件
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);/// 13 标明窗口绘制开始
GetClientRect(hwnd, &rect);/// 14 获取窗口客户区的尺寸
DrawText(hdc, TEXT("Hello, Windows 98!"), -1, &rect,/// 15 显示一个文本字符串
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);/// 16 结束窗口绘制
return 0;
case WM_DESTROY:
PostQuitMessage(0);/// 17 将退出消息 插入 消息队列
return 0;
}
// 对所有窗口过程没有处理的消息进行默认处理
return DefWindowProc(hwnd, message, wParam, lParam);/// 18 执行默认的消息处理
}
//another datastructure : MSG WNDCLASS PAINTSTRUCT RECT
///FOR :句柄
/*
HINCSTANCE : 实例句柄--程序本身
HWND : 窗口句柄
HDC : 设备环境句柄
//for another: HICON HCURSOR HBRUSH
*/
//句柄的是指是引用某个对象的数值
' note for hellowin!
最新推荐文章于 2024-06-19 19:42:53 发布