Message transmission in the message mechanism, subwindow, and parent window

Source: Internet
Author: User

1. What is a window?

Msdn: In a graphical Win32-based application, a window is a rectangular area of the screen where the application Displays output and records es input from the user. therefore, one of the first tasks of a graphical Win32-based application is to create a window.
The window is a rectangular area, which is appliedProgramYou can use it to display the output, or obtain the input from the user (keyboard and mouse ). In Windows, "Everything is a window ". Although not very relevant, it illustrates the universality of the window and its importance. For example, the QQ chat window you see is composed of multiple windows!

A Windows program consists of a series of windows. Each window has its own window process.

2. What is a message?

Msdn: a data packet used for communicating information or a request. messages can be passed between the operating system and an application, different applications, threads within an application, and windows within an application.
A message is a group of data packets (struct) used to transmit information. Messages can be transmitted between the operating system and a process, or between two different processes, it can also be transferred between different threads of the same process or between different windows of the same process. For example, if you click and type in the QQ chat window, messages are generated. InCodeThe message is saved as the message structure MSG. Its members include the window handle for processing the message, the message code, the time when the message was generated, and the location of the optical mark.

Message category:

<1>.Queue messages and non-queue messages:There are two ways to send messages: queue messages and non-queue messages. The queue message is sent to the system message queue and then to the thread message queue. Non-queue messages are directly sent to the destination window. The message queue is described as follows:

Windows maintains a system message queue. Each GUI thread has a thread message queue ). Mouse and keyboard events are converted into input messages by the mouse or keyboard driver and placed into the system message queue, such as wm_mousemove, wm_lbuttonup, wm_keydown, and wm_char. Every time windows removes a message from the system message queue, it determines which window it sends and which thread created the window, and then puts it into the thread message queue of the window creation thread. The thread Message Queue receives messages sent to the window created by the thread. The thread extracts the message from the message queue and sends it to an appropriate window through windows for processing.
In addition to the keyboard and mouse messages, the queue messages include wm_paint, wm_timer, and wm_quit. Most messages other than these queue messages are non-queue messages.

<2>.System messages and application messages:The message sources can be divided into system-defined messages and application-defined messages. (When the control changes, you will also send messages to the system, such as invalidate.)

The system Message ID ranges from 0 to WM_USER-1, or 0x80000 to 0 xbffff; the application message ranges from wm_user (0x0400) to 0x7fff, or 0xc000 to 0 xFFFF; messages in the range of wm_user to 0x7fff are used by the application itself; messages in the range of 0xc000 to 0xffff are used to communicate with other applications. For the uniqueness of the ID, use :: registerwindowmessage to obtain the Message ID in this range.

<3>.Window message, Command Message, control notification message:Based on the processing process, there are three types of messages: Window messages, command messages, and control notification messages.
(1). Window message
Generally, Windows messages starting with WM _, such as wm_create, wm_size, and wm_mousemove are used for window-related event notifications. Window messages are processed by the system when they are allocated to the window.
(2). Command Message (wm_command)
A special window message, which is sent from one window to another to process requests from the user. It is usually sent from the Child Window to the parent window. For example, when you click a button, the button's parent window will receive the wm_command message to notify the parent window button to be clicked. After testing, the Child Window will send the wm_command message to the parent window, or the parent window will receive the wm_command message, the operating system does not put the wm_command message into the message queue of the parent window, but directly calls the Window Process of the parent window, using wm_command as the Message identification parameter (uint umsg ), the API function that implements this function is: lresult dispatchmessage (const MSG * lpmsg );
(3). Control notification message
Wm_notify message, when the user interacts with the control (edit, button ...) the notification message is sent from the control window to the parent window. The purpose of this message is not to process user commands, but to enable the parent window interface to change the control in a timely manner.

3. What is a window procedure function?

The mouse and keyboard messages are generated in the window. In Windows, messages are sent to the Message Queue of the thread to which the window belongs. Then the message is processed by the corresponding processing function of the window at a certain time ".
The "message processing function" bound to the window is the "Window Process function ".
Generally, a window class is bound to a window procedure function.

4. How to Create a window

First, for Windows systems, whether you are C #, C ++ or Java, you need to create a standard window to call the same API ~
Only when Vc, C #. net, or Java is used to develop Windows, the development tools help You encapsulate the "common part" of the window, which you cannot see.
In our C # code, the first window fomr1 has been created for us in the main function. It saves us the trouble of N-plus initialization, but we do not know how the window was created.
Therefore, the sample code here uses the most primitive and obvious method to create a window to display and process your messages. We use the C language to call the API to create a window.
Create a window in four steps:
1) initialize the window class (actually a struct)
Wndclass;
2) register this class
Registerclass (......);
3) create a window
Hwnd = createwindow (......);
4) display
Showwindow (hwnd, icmdshow );
The trunk of the streamlined window program after creation:
Winmain (......)
{
......
Hwnd; // used to store the window handle
MSG; // struct used to store messages
Wndclass; // 1) window struct
Registerclass (......); // 2) register the window class
Hwnd = createwindow (......); // 3) create a window
Showwindow (hwnd, icmdshow ); // 4) display
Updatewindow (hwnd );
// Below is the message loop
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return msg. wparam;
}

Note:
1) wndclass;
This struct stores various initial data of the window. Such as the style of the Form class, the icon displayed on the form, and the icon of the mouse.
2) registerclass (......);
Call this function to notify the System of the message of the registered class window. Windows prepares a series of resources for the subsequent window creation.
3) hwnd = createwindow (......);
Call this function to create a repeated, pop-up, or subwindow. It specifies the window class, window title, window style, and initial size and position of the window.
The created window is in the "active" status and has started message processing. It is not displayed.
4) showwindow (hwnd, icmdshow );
This function is used to set the "display status" of a specified window, such as the size, position, and whether to hide the window.

 

5. how messages are transmitted and processed
after messages are generated in the window, the system puts them in the message queue of the thread to which the window belongs, waiting for processing.
after the thread obtains the message in the queue, after translation, it "puts" it into the corresponding window process function for processing.

Core code of the Message Processing Mechanism:
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
According to msdn, the functions described above are as follows:
1. getmessage only obtains messages in the message queue of the thread to which it belongs, including Windows messages and thread messages sent through postthreadmessage.
2. translatemessage is used to "translate" the virtual key message into a character message, and then store the character message back to the thread message queue for the next getmessage request. (For details, refer to msdn)
3. The function of dispatchmessage is to distribute the messages obtained from getmessage to the corresponding wndproc, and then process the messages.

The usage of the while loop in the code above indicates that the loop is the root cause for the persistence of applications. If the application exits cyclically, the application ends.

Messages are distributed by window, regardless of its subordination. That is to say, the messages generated in the subwindow are only processed in the subwindow function. After processing, the messages are no longer transmitted to the parent window (unless you perform reflection on your own ).
Summary of the relationships between windows, threads, and messages:
1. Every window belongs to a thread. Note that it is a thread.
2. Each window is "Bound" with a "window function "! That is, the messages generated in each window will be sent to the corresponding function for processing!
3. Each thread has its own "message queue"
Figure:


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.