Each window of the application has a window procedure function associated with it, and Windows passes the message to the window by invoking the window procedure. The window procedure handles the message accordingly, and then returns control to Windows.
The window class identifies the window procedure used to process messages passed to a window, allowing multiple windows to share the same window class.
Message Queuing holds messages for all windows that an application might create, and the message loop retrieves messages from the message queue and sends them to the appropriate window procedure.
#include <Windows.h>#include<mmsystem.h>#pragmaComment (lib, "WINMM. LIB ")LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); HWND hwnd;intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szCmdLine,inticmdshow) { StaticTCHAR szappname[] = TEXT ("Hellowin"); MSG msg; Wndclass Wndclass; Wndclass.style= Cs_hredraw |Cs_vredraw; Wndclass.lpfnwndproc=WndProc; Wndclass.cbclsextra=0; Wndclass.cbwndextra=0; Wndclass.hinstance=hinstance; Wndclass.hicon= LoadIcon (null,idi_application);//load icon for use by the programWndclass.hcursor = LoadCursor (NULL, Idc_arrow);//load the mouse cursor for use by the programWndclass.hbrbackground = (hbrush) getstockobject (White_brush);//gets a drawing object that is used in this example to redraw the background of a window. Wndclass.lpszclassname =Szappname; Wndclass.lpszmenuname=NULL; if(! RegisterClass (&wndclass))//Registering a window class for the application's window{MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror);//Show message Box return 0; } hwnd=CreateWindow (Szappname, TEXT ("The Hello program"), Ws_overlappedwindow, Cw_usedefault, Cw_usedefa ULT, Cw_usedefault, Cw_usedefault, NULL, NULL, HINSTANCE, NULL); //Create a window based on a window classShowWindow (hwnd, icmdshow); //displaying windows in the screenUpdateWindow (HWND);//instructs the window to redraw itself while(GetMessage (&msg,null,0,0))//getting messages from Message Queuing{TranslateMessage (&MSG);//Translate some keyboard messagesDispatchMessage (&MSG);//send a message to a window procedure } 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);//play a sound file return 0; CaseWM_PAINT:HDC= BeginPaint (HWND,&PS);//mark the start of the window drawingGetClientRect (Hwnd,&rect);//get the size of the window client areaDrawText (HDC, TEXT ("Hello, Windows 98!"), -1, &rect, Dt_singleline | Dt_center | Dt_vcenter);//display a text stringEndPaint (HWND,&PS);//End Window Drawing return 0; CaseWm_destroy:postquitmessage (0);//inserting an "exit" message into a message queue return 0; } returnDefWindowProc (Hwnd,message,wparam,lparam);//perform the default message handling}
Prefix |
Constant |
Cs |
Class style options |
CW |
Create Window Options |
Dt |
Text drawing Options |
IDI |
ID number of the icon |
Idc |
The ID number of the cursor |
MB |
message box options |
Snd |
Sound options |
Wm |
Window messages |
Ws |
Window style |
uint:unsigned int; wparam:unsigned ing; lparam:long.
WINAPI and callback are defined as __stdcall, and the sequence of function calls between windows and applications is established.
Four types of structures:
Structure |
Meaning |
MSG |
Message structure |
Wndclass |
Window class Structure |
Paintstruct |
Drawing structures |
RECT |
Rectangular structure |
A handle is essentially a numeric value that refers to an object, and the application uses a handle in other Windows functions to refer to the corresponding object.
Identifier |
Meaning |
HInstance |
Instance handle-The program itself |
HWND |
Window handle |
HDC |
Device Environment handle |
Hungarian notation: The variable name begins with a lowercase letter indicating the data type of the variable.
Prefix |
Data type |
C |
char or WCHAR or TCHAR |
By |
BYTE (unsigned character) |
N |
Short (shorter integer type) |
I |
int (integral type) |
X, y |
int, which represents the x-coordinate and y-coordinate |
Cx,cy |
int, which represents the length of x or Y, and C indicates count (count) |
B or F |
BOOL (int); F denotes flag |
W |
WORD (unsigned short integer) |
L |
Long (integer) |
Dw |
DWORD (unsigned long integer) |
Fn |
Function |
S |
String |
Sz |
A zero-terminated string |
H |
Handle |
P |
Pointer |
Chapter III, Windows and messages