Program run Effect: When creating the window, play a sound. and draw a sentence in the center of the client area of the window: Hello, Windows 98! No matter how the program moves or maximizes, the text is always in the center of the program.
The program is divided into six steps: definition, registration, creation, display, refresh, message loop. The definition section also includes a window callback function WndProc.
/*------------------------------------------------------------Hellowin. C--Displays "Hello, Windows 98!" In client area (c) Charles Petzold, 1998----------------------------- -------------------------------*/#include<windows.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szcmdline,inticmdshow) { //0th step, define variables in advance StaticTCHAR szappname[] = TEXT ("Hellowin") ;//the class name of the windowHWND hwnd;//the handle that was obtained after the window was createdMSG msg;//Message loop for message variables (just one)//The first step is to define the window classWndclass wndclass; Wndclass.style= Cs_hredraw |Cs_vredraw; Wndclass.lpfnwndproc= WndProc;//Important Properties: Window functions (callback functions)Wndclass.cbclsextra =0 ; Wndclass.cbwndextra=0 ; Wndclass.hinstance=hinstance; Wndclass.hicon=LoadIcon (NULL, idi_application); Wndclass.hcursor=loadcursor (NULL, Idc_arrow); Wndclass.hbrbackground=(Hbrush) getstockobject (White_brush); Wndclass.lpszmenuname=NULL; Wndclass.lpszclassname= Szappname;//Important Properties: Window class name//Second Step, Register window class if(! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror); return 0 ; } //step three, create the window classhwnd = CreateWindow (Szappname,//window class nameTEXT ("The Hello program"),//Window CaptionWs_overlappedwindow,//window StyleCw_usedefault,//Initial x positionCw_usedefault,//Initial y positionCw_usedefault,//Initial x SizeCw_usedefault,//Initial y sizeNull//parent Window HandleNull//Window menu HandleHINSTANCE,//Program Instance handleNULL);//Creation Parameters//Fourth Step, display windowShowWindow (hwnd, icmdshow); //Fifth Step, refresh the windowUpdateWindow (HWND); //Sixth step, message loop while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); } //In the final step, the wparam parameter of the last message is returned to the main function as the result of the function returnMsg.wparam;} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc; Paintstruct PS; Rect rect; Switch(message) { CaseWm_create:playsound (TEXT ("Hellowin.wav"), NULL, Snd_filename | Snd_async);//API return 0 ; CaseWM_PAINT:HDC= BeginPaint (hwnd, &PS);//APIGetClientRect (hwnd,&rect);//APIDrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect, Dt_singleline| Dt_center | Dt_vcenter);//APIEndPaint (hwnd,&PS);//API return 0 ; CaseWm_destroy:postquitmessage (0) ;//API return 0 ; } returnDefWindowProc (HWND, message, WParam, LParam);//API}
The most classic SDK program structure Hellowin