Talking about Windows message mechanism

Source: Internet
Author: User

Start by understanding several basic concepts:

Message: Learn what is a message first to understand what an event is. Events can be divided into several types, triggered by the input device, such as the mouse keyboard and so on. Triggered by a form control, such as a button control, a File menu, and so on. There is also an event from within Windows. These three types are called events. And the message is translated by the event. Event generates a message.

From the data structure point of view, the message is a struct. The structure is as follows:

1typedefstructtagmsg2 3 {4 5HWND hwnd;//The window handle. 6 7UINT message;//The message type. 8 9WPARAM WPARAM;//32-bit additional information. Ten  OneLPARAM LPARAM;//32-bit additional information.  A  -DWORD time;//The time the message was sent.  -  thePoint pt;//the location of the mouse when the message is sent.  -  -}msg;

Message Queuing: There are two types of Message Queuing, divided into system Message Queuing and application Message Queuing. The resulting message is first captured by the Windows system, placed in the system message queue, and then copied to the corresponding application message queue. A 32/64-bit system maintains a message queue for each application.

Message loop: The system maintains a message loop for each application, and the message loop constantly retrieves its own message queue. For each message, use GetMessage () to remove the message.

1  while 0 0)) // Windows message loop.  23{4     translatemessage (&msg);   translate messages, such as keystroke messages, translated to Wm_char56     dispatchmessage (&msg);   Distributing messages to corresponding Windows 78 }

GetMessage has a blocking mechanism. When there is no message in the message queue, the program is not busy and so on, but let the right wait. When Wm_quit is received, GetMessage returns false, the loop stops, and the application terminates.

Message processing: DispatchMessage () assigns the extracted message to the appropriate window or thread, which is handled by the window procedure handler function DefWindowProc ().

=============================================================================================================== =============================================================== cut and cut cut

The above briefly describes the concept of Message Queuing, message looping, and message processing.

Windows applications rely on message-driven to implement functionality. Message-driven is handled by the message mechanism. The message mechanism is composed of message queue, message loop, message processing.

So how does the messaging mechanism work?

When a user runs an application, a specific event is generated by clicking on the mouse or keyboard key. Because Windows monitors I/O devices, the event is first translated into messages, captured by the system, and stored in the system message queue. After analysis, Windows knows that the message should be processed by that application, and then it is copied to the appropriate application message queue. Because the message loop constantly retrieves its own message queue, when it finds messages in the application message queue, it uses getmessage () to take out the message and encapsulate it into the MSG () structure. If the message is generated by a keyboard key, translated with TranslateMessage () to the WM_CHAR message, otherwise, the extracted message is distributed to the appropriate application window using DispatchMessage (), which is processed by the window handler. Windows reserves a procedure window function for each form, which is a fallback function that is called by the system and cannot be called by the application. Programmers can handle our "interesting" messages by overloading the function. For messages that are not interesting, the system's default window procedure handler processes the process.

Here's a look at this picture:

This diagram is a good explanation of how the message mechanism works.

When the Run program-I event action raises a message, the message first exists in the system message queue, and then into the application message queue, and then the message queue is returned with the message loop extraction message, processing message, ....

=============================================================================================================== =============================================== above is the generation and processing of system messages

Similarly, we can use the SendMessage () function to simulate sending a message to the window by customizing the user message, and then reload the window procedure handler function Defwndproc () to receive the user-defined message and make the processing.

Because SendMessage () is an API function of the system. So in. NET Platform calls API functions, involving the concept of dynamic link libraries. Use a dynamic-link library to declare a class, which indicates which. DLL files, and program entry points.

For example:

1 int 0x520 ; 2 int 0x521 // user-defined messages. 
1[DllImport ("User32.dll", entrypoint="SendMessage")]//entrypoint indicates that the function entry point to be called is the SendMessage () function in the DLL file. 2 Private Static extern intSendMessage (3Ntptr hwnd,//declaring a window handle, the so-called handle, is a reference to a "pointer" in C #, a window or other resource, but this "pointer" is not available for other special operations. 4//IntPtr is a platform-specific type int. is 32-bit and 64-bit on 32-and 64-bit systems respectively. Typically used to declare a handle. 4     intMSG,//indicates the message to be sent. 5intWParam,//additional information for 32 bits. 6     intIprarm//additional information for 32 bits. (change with the change of message)7  );8 //here because the SendMessage is sent to the window of this thread. So messages sent are not added to the message queue, so messages sent by SendMessage cannot be obtained through PeekMessage () or getmessage (). 
1 protected Override voidDefwndproc (refMessage m)//overloaded Receive handler functions2 {3     Switch(m.msg)4 {5        CaseXxx1:label1. Text ="there?"; Break;6        CaseXxx2:label1. Text ="Not available"; Break;//XXX1/XXX2 user-defined messages. 7       default:Base. Defwndproc (refm); Break;8 }9}

What is a dynamic link library? As a "warehouse", there's a whole bunch of functions to call. The dynamic indicates that the use of the attendant, rather than the program is not necessary to take a lot of code. When calling API functions in C # with [DllImport] class declarations, there are three conditions, one is that the function name must be identical to the original API function name, and the arguments must be identical, and the third is declared as "extern". The parameters about API functions are available on MSDN. Don't repeat it here.

I learned that a C + + friend knows that writing a program to add a #include<xxx.h> header file, in fact, this is a dynamic link.

=============================================================================================================== ===================== above is user-defined messages, as well as sending messages, and receiving processing messages

The following is the creation of a form to tell again. Windows applications actually have the same program structure and execution control flow. The execution process is as follows:

Program Entry point (DOS is main (), Windows is _tWinMain ()), Registration window class (registerclass), create window (CreateWindow ()), display window (ShowWindow ( Hwnd,ncmdshow) (UpdateWindow (HWND)), message loop (Wait for User Action window to generate messages), put messages in Message Queuing, and then read and execute the corresponding Code, window functions ( Decide what to display in the window, or how to respond to user input (such as the SendMessage () function above)), message processing (to determine what message the window function receives and how to handle it (such as the Defwndproc () function above).

Off-topic: Windows and DOS programming, a big difference is that Windows programming is event-driven, and DOS is process-driven. So, to learn Windows programming, at least a clear understanding of the message mechanism, otherwise it is difficult to compile a number of useful programs.

Reprint Please specify source: Http://www.cnblogs.com/gu-zhan/p/4051284.html Blog Park-Summer Fetish

Talking about Windows message mechanism

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.