第一个窗口程序(HELLOWIN.cpp),和第一个系统度量程序(SYSMETS1.cpp).
代码注释,如下:
- //HELLOWIN.cpp
- #include <windows.h>
- /* #define CALLBACK _stdcall
- typedef LONG LRESULT;
- */
- LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,int nShowCmd)
- {
- HWND hWnd;
- MSG msg;
- WNDCLASS wndclass;
- static TCHAR szAppName[]=TEXT("HelloWin"); //static?
- //初始化窗口类的属性
- wndclass.cbClsExtra=0;
- wndclass.cbWndExtra=0;
- wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); //wndclass结构体的成员hbrBackground,类型是HBRUSH
- wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
- wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //第一个参数为什么为NULL?表示第二个参数加载的是系统默认图标ID.
- wndclass.hInstance=hInstance;
- wndclass.lpfnWndProc=WndProc;
- wndclass.lpszClassName=szAppName;
- wndclass.lpszMenuName=NULL;
- wndclass.style=CS_HREDRAW | CS_VREDRAW;
- //注册窗口类
- if(!RegisterClass(&wndclass))
- {
- MessageBox(NULL,TEXT("This pragram requiers Windows NT."),
- szAppName,MB_ICONERROR);
- return 0;
- }
- //创建窗口
- hWnd=CreateWindow(szAppName,TEXT("The Hello Pragram"),WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
- NULL,NULL,hInstance,NULL);
- ShowWindow(hWnd,nShowCmd); //第二个参数:nShowCmd与iCmdShow?除了名字,一样的.
- UpdateWindow(hWnd);
- //循环获取消息
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- RECT rect;
- switch(message)
- {
- case WM_CREATE:
- PlaySound(TEXT("hellowin.wav"),NULL,SND_FILENAME | SND_ASYNC);
- return 0;
- case WM_PAINT:
- hdc=BeginPaint(hWnd,&ps);
- GetClientRect(hWnd,&rect);
- DrawText(hdc,TEXT("Hello,Windows 98!"),-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
- EndPaint(hWnd,&ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0); //参数为什么为0?把0传给msg.wParam.
- return 0;
- }
- return DefWindowProc(hWnd,message,wParam,lParam);
- }
- //SYSMETS1.cpp
- #include <windows.h>
- #include "SYSMETS.H"
- /* #define CALLBACK _stdcall
- typedef LONG LRESULT;
- */
- LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,int nShowCmd)
- {
- HWND hWnd;
- MSG msg;
- WNDCLASS wndclass;
- static TCHAR szAppName[]=TEXT("SysMets1"); //static?
- //初始化窗口类的属性
- wndclass.cbClsExtra=0;
- wndclass.cbWndExtra=0;
- wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); //wndclass结构体的成员hbrBackground,类型是HBRUSH
- wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
- wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //第一个参数为什么为NULL?表示第二个参数加载的是系统默认图标ID.
- wndclass.hInstance=hInstance;
- wndclass.lpfnWndProc=WndProc;
- wndclass.lpszClassName=szAppName;
- wndclass.lpszMenuName=NULL;
- wndclass.style=CS_HREDRAW | CS_VREDRAW;
- //注册窗口类
- if(!RegisterClass(&wndclass))
- {
- MessageBox(NULL,TEXT("This pragram requiers Windows NT."),
- szAppName,MB_ICONERROR);
- return 0;
- }
- //创建窗口
- hWnd=CreateWindow(szAppName,TEXT("Get System Metrics No.1"),WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
- NULL,NULL,hInstance,NULL);
- ShowWindow(hWnd,nShowCmd); //第二个参数:nShowCmd与iCmdShow?除了名字,一样的.
- UpdateWindow(hWnd);
- //循环获取消息
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- //定义变量
- static int cxChar,cyChar,cxCaps; //static?
- TCHAR szBuffer[10];
- int i;
- HDC hdc;
- PAINTSTRUCT ps;
- //RECT rect;
- TEXTMETRIC tm;
- //处理消息
- switch(message)
- {
- case WM_CREATE:
- hdc=GetDC(hWnd);
- GetTextMetrics(hdc,&tm);
- cxChar=tm.tmAveCharWidth;
- cyChar=tm.tmHeight+tm.tmExternalLeading;
- cxCaps=((tm.tmPitchAndFamily&1)? 3:2)*cxChar / 2;
- ReleaseDC(hWnd,hdc);
- return 0;
- case WM_PAINT:
- hdc=BeginPaint(hWnd,&ps);
- for(i=0;i<NUMLINES;i++)
- {
- TextOut(hdc,0,cyChar * i,
- sysmetrics[i].szLabel,
- lstrlen(sysmetrics[i].szLabel));
- TextOut(hdc,22 * cxCaps,cyChar * i,
- sysmetrics[i].szDesc,
- lstrlen(sysmetrics[i].szDesc));
- SetTextAlign(hdc,TA_RIGHT | TA_TOP);
- TextOut(hdc,22*cxCaps + 40*cxChar,cyChar * i,
- szBuffer,
- wsprintf(szBuffer,TEXT("%5d"),GetSystemMetrics(sysmetrics[i].iIndex)));
- SetTextAlign(hdc,TA_LEFT | TA_TOP);
- }
- EndPaint(hWnd,&ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0); //参数为什么为0?把0传给msg.wParam.
- return 0;
- }
- return DefWindowProc(hWnd,message,wParam,lParam);
- }
函数原型: