Windows message processing process description

Source: Internet
Author: User

/*------------------------------------------------------------
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)
{
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;
Wndclass. hIcon = LoadIcon (NULL, IDI_APPLICATION );
Wndclass. hCursor = LoadCursor (NULL, IDC_ARROW );
Wndclass. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH );
Wndclass. lpszMenuName = NULL;
Wndclass. lpszClassName = szAppName;

If (! RegisterClass (& wndclass ))
{
MessageBox (NULL, TEXT ("This program requires Windows NT! "),
SzAppName, MB_ICONERROR );
Return 0;
}

Hwnd = CreateWindow (szAppName, // window class name
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 the first parameter is the newly created form, and the second parameter is passed in from the winmain function. SW_SHOWMAXIMIZED is changed here to maximize display.
ShowWindow (hwnd, SW_SHOWMAXIMIZED );
UpdateWindow (hwnd );

/* The msg variable is a MSG-type structure. The MSG type is defined in WINUSER. H as follows:
Typedef struct tagMSG
{
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
}
* The POINT data type is also a structure, which is defined in WINDEF. H as follows:
Typedef struct tagPOINT
{
LONG x;
LONG y;
}*/
While (GetMessage (& msg, NULL, 0, 0 ))
/* GetMessage
GetMessage (& msg, NULL, 0, 0)
This call is sent to a Windows metric pointing to the msg structure named MSG. The second, third, and fourth parameters are set to NULL or 0,
Indicates that the program receives all messages in all the windows it has created. Windows uses the next message retrieved from the message queue to fill the message structure.
The fields of the structure include:
The handle of the windows in which hwnd receives messages. In the HELLOWIN program, this parameter corresponds to the hwnd returned by CreateWindow.
The value is the same, because this is the only window owned by the program.
Message identifier. This is a numerical value used to identify a message. Each message has a corresponding identifier.
Identifier, which is defined in the Windows header file (most of which are in WINUSER. H 」,
Window message. For example, if you place the cursor in the HELLOWIN display area and press the left button
Put a message in the message queue. The message field of the message is WM_LBUTTONDOWN. This is a constant with a value of 0x0201.
WParam is a 32-bit "message parameter (message parameter)". Its meaning and value are different from those of the message.
.
LParam is a 32-bit Message Parameter whose value is related to the message.
The time when the message is placed in the message queue.
The mouse coordinate of the pt message placed in the message queue.
As long as the message Field Retrieved from the message queue is not WM_QUIT (its value is 0x0012), GetMessage returns a non-zero value.
WM_QUIT message will cause GetMessage to return 0. */
{

TranslateMessage (& msg); // transmits the msg structure to Windows for keyboard conversion.
DispatchMessage (& msg );
/* The DispatchMessage returns the msg structure to Windows. Then, Windows sends the message to the appropriate window message handler for processing. In other words, Windows calls the window message processing program. In HELLOWIN, the message processing program in this window is the number of WndProe functions. After the message is processed, WndProc is passed back to Windows. In this case, Windows is still in the DispatchMessage call. After the DispatchMessage call is processed, Windows returns to HELLOWIN and starts the message loop from the next GetMessage call. */
}
Return msg. wParam;
}

Lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
/* The four parameters of the window message processing program are the same as those of the first four fields in the MSG structure. The first parameter hwnd is the window for receiving messages.
It is the same as the return value of the CreateWindow function. For programs similar to HELLOWIN (only one window is created), this parameter
Number is the unique window handle that the program knows. If the program creates multiple
Window, then hwnd identifies a specific window for receiving messages.
The second parameter is the same as the message field in the MSG structure. It is the value that identifies the message. The last two parameters are both 32-bit message parameters,
Provides more information about messages. These parameters contain detailed information about each message type. Sometimes the message parameters are two 16-bit
Value, and sometimes the message parameter is a pointer to a string or data structure.
Generally, a program does not directly call the window message processing program. The window message processing program is usually called by Windows itself. Call SendMessage
Function, the program can directly call its own window message processing program. */
{
HDC hdc; // device content handle
PAINTSTRUCT ps; // Drawing Structure
RECT rect;

Switch (message)
{
Case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC );
Return 0;

Case WM_PAINT:
/* The second message processed by WndProc is WM_PAINT. This message is very important in Windows programming. When the window shows area 1
Some or all of the displayed content changes to "invalid", so that the Message notification program will be used when you must "update the screen.
How can the display content of the display area become invalid? When a window is initially created, the entire display area is invalid because the program does not
Draw something in the window. The first WM_PAINT message (usually when calling UpdateWindow in WinMain) indicates the window message.
The handler draws something on the display area.
After the user changes the size of the HELLOWIN window, the display content in the display area becomes invalid again. Readers should remember that in HELLOWIN
The style field of the wndclass structure is set to CS_HREDRAW and CS_VREDRAW. Such format settings indicate Windows, in the window
After the size changes, the content displayed in the entire window is invalid. Then, the window message processing program will receive a WM_PAINT message.
When the user minimizes HELLOWIN and then restores the window to the previous hour, Windows will not save the content of the display area.
In a graphic environment, the window display area involves a large amount of data. Therefore, Windows makes the window invalid, and the window message processing program receives
WM_PAINT message, and automatically restore its window content.
When a window is moved to overlap, Windows does not save the content covered by another window. This part will not be masked
It is marked as invalid. The window message processing program receives a WM_PAINT message to update the window content.
The processing of WM_PAINT almost always starts from a BeginPaint call:
Hdc = BeginPaint (hwnd, & ps );
End with an EndPaint call:
EndPaint (hwnd, & ps );
In both calls, the first parameter is the window handle of the program, and the second parameter is the structure pointer pointing to the PAINTSTRUCT type.
The PAINTSTRUCT structure contains some window message handlers that can be used to update the content of the display area. We will discuss the conclusion in the next chapter.
Fields. Now we only use it in the BeginPaint and EndPaint functions. */
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: // close the message of the form
PostQuitMessage (0); // This function inserts a WM_QUIT message in the message queue of the program. As mentioned above, GetMessage returns a non-zero value for all messages retrieved from the Message Queue except WM_QUIT. When GetMessage gets a WM_QUIT message, it returns 0. This will cause WinMain to exit the message loop and terminate the program. Then the program executes the following description:
Return 0;
}
// Sometimes, after DefWindowProc finishes processing the message, it will produce other messages. For example, assume that the user executes HELLOWIN and the user is the most
// Click the Close button, or if you select Close from the system menu with the keyboard or mouse, DefWindowProc can process the keyboard or
// Input the mouse. After the user detects that the Close option is selected, it sends a WM_SYSCOMMAND message to the window message processing program. The message WndProc is sent to DefWindowProc. DefWindowProc sends a WM_CLOSE message to the window message handler.
// Corresponding. WndProc sends it to DefWindowProc again. DestroyWindow calls DestroyWindow to respond to this WM_CLOSE message
// Information. DestroyWindow causes Windows to send a WM_DESTROY message to the window message handler. Call WndProc again
// PostQuitMessage: Put A WM_QUIT message into the message queue to respond to the message. This message causes consumption in WinMain.
// The information loop ends and the program ends.
Return DefWindowProc (hwnd, message, wParam, lParam );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.