Windows message mechanism highlights

Source: Internet
Author: User
Tags getmessage message queue

1. in a Window process, each Window has a callback function (WndProc), which has four parameters: Window Handle, Message ID ), and two message parameters (wParam, lParam). When the window receives a message, the system calls this window to process the message. (So called callback function)

2 Message Type

1) System-Defined message)

Messages pre-defined in the SDK, which are not user-defined, fall between [0x0000, 0x03ff] and can be divided into the following three types:

1> Windows Message)

It is related to the internal operation of the window, such as creating a window, drawing a window, and destroying a window. It can be a common window, a Dialog, or a control.

For example: WM_CREATE, WM_PAINT, WM_MOUSEMOVE, WM_CTLCOLOR, WM_HSCROLL...

2> Command Message)

It is related to processing user requests. For example, when you click a menu item or toolbar or control, a command message is generated.

WM_COMMAND, LOWORD (wParam) indicates the menu item, toolbar button or control ID. If it is a control, HIWORD (wParam) indicates the control message type

3> Notify Message)

Control notification Message, which is the most flexible Message format. Its Message, wParam, and lParam are respectively WM_NOTIFY, and the control ID points to the NMHDR pointer. NMHDR contains the control notification content, which can be expanded as needed.

2) Application-Defined Messages)

The range of custom messages is as follows: WM_USER: 0x0400-0x7FFF (ex. WM_USER + 10) WM_APP (winver> 4.0): 0x8000-0xBFFF (ex. WM_APP + 4)

RegisterWindowMessage: 0xC000-0xFFFF

3. Message Queue)

There are two types of message queues in Windows

1) System Message Queue)

This is the only Queue in the system. The device driver (mouse, keyboard) will convert the operation input into a message in the system Queue, the system then places the message to the queue (thread-specific message queue) of the thread in which the target window is located for medium waiting for processing.

2) Thread Message Queue (Thread-specific Message Queue)

Every GUI thread maintains such a thread message queue. (This queue is created only when the thread calls the GDI function. It is not created by default ). Then the messages in the thread message queue are sent to the corresponding window process (WndProc) for processing. note: In the thread message Queue, WM_PAINT and WM_TIMER are processed only when there are no other messages in the Queue, and WM_PAINT messages are merged to improve efficiency. All other messages are processed in FIFO mode.

4. Queue Messages (Queued Messages) and Non-queue Messages (Non-Queued Messages)

1) queue message (Queued Messages)

The message is saved in the message queue first, and the message loop will retrieve the message from the queue and distribute it to various windows for processing, such as mouse and keyboard messages.

2) Non-queue message (NonQueued Messages)

Messages are directly sent to the window without passing through the system message queue and thread message queue.

For example, WM_ACTIVATE, WM_SETFOCUS, WM_SETCURSOR, and WM_WINDOWPOSCHANGED. Note: The message sent by postMessage is a queue message, which will Post the message to the message queue. The message sent by SendMessage is a non-queue message, process sent directly to the window

5 PostMessage (PostThreadMessage) and SendMessage PostMessage: put the message in the thread message queue of the specified window and return immediately. PostThreadMessage: place the message

Return immediately after arriving in the message queue of the specified thread.

 

SendMessage: send the message directly to the window for processing. After processing, the message is returned.

6 GetMessage, PeekMessage

PeekMessage returns the message that can be retained immediately.

GetMessage: if a message is returned, the message will be deleted.

7. TranslateMessage, TranslateAccelerator TranslateMessage: converts a virtual-key message into a character message (character message) and stores it in the message queue of the current thread. The message is extracted and processed in the next cycle.

TranslateAccelerator: maps the shortcut key to the corresponding menu command. It converts WM_KEYDOWN or WM_SYSKEYDOWN to the corresponding WM_COMMAND or WM_SYSCOMMAND message in the shortcut table, and then sends the converted WM_COMMAND or WM_SYSCOMMAND to the window for processing. After processing, the system returns the result.

8 (Message Deadlocks)

Assume there are threads A and B, and now there are the following steps 1) thread A SendMessage to thread B, and thread A returns 2 after waiting for the message to be processed in thread B) thread B receives A message from thread A and processes it. During the processing process, Thread B also sends messgae to thread A and waits for the response from thread.

At this time, thread A is waiting to return from thread B and cannot process messages sent from thread B. As A result, thread A and thread B wait for each other to form A deadlock. Multiple threads can also form a circular deadlock.

You can use sendpolicymessage or SendMessageTimeout to avoid deadlocks.

9 BroadcastSystemMessage

Generally, all the messages we come into contact with are sent to the window. In fact, the message receiver can be a variety of applications ), installable drivers, network drivers, and system-level device drivers can be installed. The BroadcastSystemMessage API can send messages to the above system components.

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.