Windows Message Queuing

Source: Internet
Author: User

Many friends do not use the MSDN Library (although MSDN is not the same thing as the MSDN Library, but this is abbreviated as MSDN), it is really important.


In fact, all the instructions can be found on MSDN.


Environment: VS2008 + SP1 patch MSDN LIBRARY + SP1 patch


After opening this version of MSDN, you can see the interface


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/48/5B/wKioL1QHCtSCw6KTAAjmJivhfJA497.jpg "title=" 001. JPG "alt=" wkiol1qhctscw6ktaajmjivhfja497.jpg "/>


In the lower left corner, we can see tabs such as Contents, index, help Favorites, and more importantly, Contents and index, where Contents is to categorize the content on MSDN as relevance, and index Mainly for the specific function of the query, such as we switch to the Index tab page, and then in the above looking for any one of the function name: CreateWindow, and then a double click on the CreateWindow listed below, you can see the right side With the information of the function. In fact, a function is likely to have a variety of platforms to implement it, such as this function, wince platform can also be used, which requires the use of the lower left corner of the Index Results function, here will enumerate all the relevant information.


In fact, I think everyone should be this way, about the message map, if you want the system to understand what should be done? Just look at GetMessage such a function is not see anything, we need to navigate to MSDN on the message map to explain and explain the place, the system to learn, and then to understand. In fact, other functions, such as about the window, about the control, about MFC, about C and C + + language, C # and other languages. NET class library, on the system aspects, such as threads, processes, system services, dynamic link library, in fact, MSDN can be found in the description, but many people do not know how to find.


Below, take a look at the following two graphs, then expand the nodes in the diagram, and note to select the Contents


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/48/5B/wKioL1QHDUryL5GZAAJvdRbvhy0371.jpg "style=" float: none; "title=" 002.jpg "alt=" Wkiol1qhduryl5gzaajvdrbvhy0371.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/48/5A/wKiom1QHDUfgP8HPAAJVtgcjfPs544.jpg "style=" float: none; "title=" 003.jpg "alt=" Wkiom1qhdufgp8hpaajvtgcjfps544.jpg "/>


Well, I'm sure you've seen the message and Message Queue section. Open it up and see.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/48/5B/wKioL1QHDbGRxY1-AAKPAX1zDZA619.jpg "title=" 004. JPG "alt=" wkiol1qhdbgrxy1-aakpax1zdza619.jpg "/>


Can see that it is roughly divided into a few parts: overviews overview, there is about and use two small branches, about is the more important part, describes the message queue commonly used concepts and understanding, the use of the main is to cite some examples, and then reference, mainly including the function list, The notification used in the function and the struct body.


Let's focus on some of the sections in the overview.


Unlike DOS system-based applications, Windows-based applications are event-driven. Windows-based applications do not use the displayed calling function to get input (like scanf in the C language). Instead, they wait for the operating system to pass the input to them.


All input from an application is passed by the operating system to a large number of windows in the program. Each window has a function called the window procedure. The operating system will call this window process when the window has input. The window procedure processes the input and returns control to the operating system.


In an XP system, if a top-level window stops responding to a message for more than a few seconds, the system considers it to handle an unresponsive state. In this case, the system hides it and replaces it with a ghost window, which has the same z Order, position, size, and appearance as the top-level window. This allows the user to move it, modify its size, and even about the application. However, only these actions are valid for this top-level window because the application is not actually responding. When in debug mode, the system does not produce ghost windows.


Windows Messages window messages

The system passes the input to the window procedure in the form of a message. Messages are generated by the system itself and by the application itself. Each input event causes a message to be generated by the operating system itself, for example, when the user presses the keyboard, moves the mouse, or clicks on a scroll bar control. When the application has made the system changes, the system will also generate messages, such as when the user modified the system color (do not modify it, right-click on the blank area of the desktop, properties, appearance, advanced, which can be set in this area), the system will produce

Wm_syscolorchange message.

The application can also generate messages to direct its own window to perform tasks or to communicate with windows in other applications.


When the system sends a message to the window procedure, the message is a window handle with four parameters, a message identifier, and two parameters. The window handle identifies which window the message belongs to, and the system uses it to determine which window procedure should receive the message. A message identifier is a constant that, when a window procedure receives a message, uses the message identifier to decide what to do with the message. For example, the message identifier WM_PAINT tells the window that the client area of the process window has changed and needs to be redrawn. The message parameter specifies where the data or data exists, which can be used by the window procedure. As to what two parameters mean, it depends on the specific message, it can be an integer, or it can be a pointer to a struct object, and so on. When a message does not need to use a parameter, the message argument is usually set to null, and the window procedure must validate the message identifier to determine how the message's parameters are interpreted.


Message Types Messages Type

There are two types of messages: system-defined messages and application-customized messages


When the operating system communicates with an application, the operating system sends or posts system-defined messages to the application. The operating system uses these messages to control the operation of the application and provides input and other information to the application for processing by the application. Applications can also send or post system-defined messages. Applications typically use these messages to control the operation of a control window, which is a window created using the pre-registered Windows class of the operating system, such as a common BUTTON, EDIT, and so on.


Each system-defined message has a unique identifier and a symbolic constant that describes its function, for example, WM_PAINT This constant requires the window to repaint its contents.


Symbolic constants specify collections, which are predefined by the system, and many messages are prefixed:


Prefix Message category
ABM Application Desktop Toolbar
Bm Button Control
Cb Combo Box control
CBEM Extended combo Box control
Cdm Common dialog box
Dbt Device
Dl Drag list Box
Dm Default push Button control
Dtm Date and Time picker control
Em Edit Control
HDM Header Control
HKM Hot Key Control
Ipm IP Address Control
Lb List Box control
Lvm List View Control
MCM Month Calendar Control
PBM Progress Bar
Pgm Pager Control
Psm property sheet
Rb Rebar Control
SB Status Bar Window
SBM Scroll Bar control
Stm Static Control
TB Toolbar
Tbm Trackbar
TCM Tab Control
Ttm Tooltip Control
TVM Tree-view Control
Udm Up-down Control
Wm General window


The General window message covers a large range including mouse and keyboard messages, menu and dialog input, window creation and management, and dynamic data exchange.


Application-Customized message: An application can create a message for its own window to use or communicate with a window in another process. If the application creates its own message, its window procedure function must receive the message, explain it, and handle it correctly.


Please refer to MSDN for the user-defined Message details section.


Here are some of the more important things in this article:

Message Routing Messages Routing

There are multiple processes in an operating system, each process has at least one main thread, and of course it can have other threads, each thread can create n windows, we first need to understand this object relationship.


The operating system uses two methods to route messages to a window procedure:

1, the delivery of the message to a first-out data structure, this is the message queue, it is the operating system defined a block of memory, used to temporarily store messages.

2. Send the message directly to the window procedure.


Messages that are posted to the message queue are called queue messages. Queue messages are mainly mouse messages, keyboard messages, character messages. Other queue messages also have timer messages (wm_timer), paint messages (WM_PAINT), exit messages (Wm_quit). Most of the other messages are sent directly to the window procedure, which are called non-queue messages.


Above we describe the concept of queue and non-queue messages. Let's see what they mean here.


Queued Messages: Queue message

The operating system can display any number of windows at the same time. To route input events such as mouse and keyboard to the appropriate window, the operating system uses Message Queuing.


The operating system maintains a separate system message queue and maintains a line thread message queue for each interface thread.


To avoid the overhead of creating message queues for non-interface threads, all threads are initialized without Message Queuing, and only the system will create a message queue for this thread the first time the user function or GDI function is called.


Now we can basically understand one thing, Message Queuing is thread-related, and a GUI thread has only one message queue, but it can create many windows.


When the user moves the mouse, clicks the mouse, or presses the keyboard, the keyboard or mouse driver converts the input event into a message and then sets it to the system's message queue, the system removes the message from the system message queue, removes only one at a time, checks their target window, and then delivers the message to the object that created the target window. The message queue for the thread. Thread Message Queuing receives all mouse and keyboard messages from the window that he created. The thread removes the message from its message queue and then instructs the operating system to send them to the appropriate window procedure for processing.


In addition to the WM_PAINT Wm_timer wm_quit three messages, the operating system always posts the message to the end of the message queue, which guarantees that the window receives the input message in the order of the queue. These three messages are sent to the window procedure only if there are no other messages in the thread's message queue. In addition, multiple WM_PAINT messages sent to the same window will eventually be merged into a single WM_PAINT message, which merges all invalid areas, primarily to improve efficiency.

The usual function of queue message is PostMessage


nonqueued Messages Non-queue messages


A non-queue message is a message that is sent directly to a window procedure, bypassing system Message Queuing and thread Message Queuing. The usual functions are sendmessage.


Well, I'm just picking up some important parts for the literal translation and not pondering it carefully.


Here's a summary:

1. There will be multiple processes in one operating system

2, each process has at least one main thread, of course, can also have other threads

3. Each thread can create multiple windows.

4. One operating system has only one system message queue

5, each GUI thread also has only one thread message queue

6, the operating system will record all information, such as which thread belongs to which process, which window belongs to which thread.

7, the message is divided into queue messages and non-queue messages

8. The queue message is that the hardware driver encapsulates the input event into a message queue, and then the operating system finds the thread that the window belongs to, one after the other, according to the window handle in the message, then posts the message to the thread message queue, and the thread message queue is obtained by some functions, such as GETMESSAGE PeekMessage, this function gets a message, the thread message queue removes a message, and then the program acquires the message and needs to call DispatchMessage, which sends the message to the Window's window procedure function, The window procedure function is then responsible for handling this message.

9, non-queue message is mainly refers to the operating system or application call SENDMESSAGE send the message directly to the window procedure handler function, does not go through two queue processing.


OK, okay. With these basics, we'll read the MSDN content again, and I guess it's almost there. There are some details I have not said, such as some other commonly used functions, message filtering, and so on, we read.



This article is from "Chin Que song" blog, please be sure to keep this source http://qianqianquege.blog.51cto.com/8004200/1548512

Windows Message Queuing

Related Article

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.