Windows message Mechanism Essentials

Source: Internet
Author: User
Tags getmessage

1. Window procedure
Each window will have a callback function called a window procedure (WndProc) with four parameters, namely: Window handle (Windows Handle), message ID, and two message parameters (WParam, LParam), This window procedure is called by the system to process the message when the window receives the message. (so called callback function)

2 message type  
1) system-defined message (system-defined Messages)
 
Pre-defined message in the SDK, non-user-defined, and its scope in [0x0000, 0X03FF], can be divided into the following three categories:
1> window messages (Windows message)  
is related to the internal workings of the window, such as creating Windows, drawing windows, destroying windows, and so on. It can be a normal window, a dialog, a control, and so on.
such as: Wm_create, WM_PAINT, Wm_mousemove, Wm_ctlcolor, Wm_hscroll ...
2> Command message: Note that this type of message is called Wm_command
and is related to handling user requests, such as when a menu item or toolbar or control is clicked, a command message is generated.
Wm_command, loword (WParam) represents the ID of a menu item, toolbar button, or control. If it is a control, HiWord (WParam) represents the control message type
3> control notification (Notify message)  
Control notification message, which is the most flexible message format,  Its message, WParam, lparam are: WM_NOTIFY, control ID, pointer to NMHDR. NMHDR contains the contents of the control notification and can be arbitrarily extended.
2) program-defined messages (application-defined Messages)  
User-defined messages for their scope are 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 (msg Queues)
There are two types of Message Queuing in Windows
1) System Message Queuing (systems messages queue)
This is a system-unique queue where the device driver (mouse, keyboard) translates the operation input into a message that exists in the system queue, which is then placed in the message queue of the thread on which the target window is located (thread-specific-message queues) Waiting for processing in
2) thread Message Queuing (thread-specific message queue)
Each GUI thread maintains such a thread message queue. (This queue is created only when the thread calls the GDI function and is not created by default). The message in the thread message queue is then sent to the appropriate window procedure (WNDPROC) for processing.
Note: In thread Message Queuing, Wm_paint,wm_timer is 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 first-in, in-and-out (FIFO) mode.

4 Queue messages (Queued Messages) and non-queue messages (non-queued Messages)
1) Queue message (Queued Messages)

Messages are saved in the message queue, and the message loop takes messages from this queue and distributes them to each window processing
such as mouse, keyboard messages.
2) Non-queue messages (nonqueued Messages)
Messages that bypass system Message Queuing and thread Message Queuing are sent directly to the window process are processed
such as: Wm_activate, Wm_setfocus, Wm_setcursor, wm_windowposchanged
Note: The message sent by PostMessage is a queue message that posts the message to the message queue; SendMessage sends a message that is not a queue message, and is sent directly to the window procedure processing

5 PostMessage (postthreadmessage), SendMessage
PostMessage: Returns the message immediately after it is placed in the thread message queue where the specified window is located. PostThreadMessage: Returns the message immediately after it is placed in the message queue of the specified thread.
SendMessage: The message is sent directly to the window process processing, the process is finished before returning.

6 GetMessage, PeekMessage
PeekMessage will return immediately to keep the message
GetMessage returns a message when there is a message

7 TranslateMessage, TranslateAccelerator
TranslateMessage: Converts a virtual-key message into a character message (character message) and puts it into the message queue of the current thread, and the next fetch of the message loop.
TranslateAccelerator: The shortcut key corresponds to the corresponding menu command. It transforms the Wm_keydown or wm_syskeydown into the corresponding WM_COMMAND or Wm_syscommand message in the accelerator table, and then sends the converted WM_COMMAND or Wm_syscommand directly to the window process, It will not be returned until the processing is finished.

8 (Message deadlock (msg deadlocks)
Assuming the threads A and B, now have the following steps
1) thread A sendmessage to thread B, a waits for the message to be returned after process B is processed
2) thread B receives a message from thread A and processes it, B also sendmessgae to thread a during processing, and waits for a return from a.
Because at this point, thread A is waiting to be returned from thread B, unable to process the message from B, which causes the/thread A, B to wait for each other to form a deadlock. Multiple threads can also form ring deadlocks.
You can use SendNotifyMessage or sendmessagetimeout to avoid deadlocks.

9 Broadcastsystemmessage
We generally contact the message is sent to the window, in fact, the recipient of the message can be a variety of, it can be an application (applications), can install the driver (installable drivers), network equipment (networks drivers), System-Level device drivers (System-level devices drivers), etc.,
Broadcastsystemmessage This API can send messages to the above system components.

http://blog.csdn.net/harbinzju/article/details/5832306

Windows message Mechanism Essentials

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.