Message filtering - Posting and sending messages


Message Filtering

An application can choose specific messages to retrieve from the message queue (while  ignoring other messages) by using the GetMessage or PeekMessage function to specify  a message filter. The filter is a range of message identifiers (specified by a first and last  identifier), a window handle, or both. GetMessage and PeekMessage use a message  filter to select which messages to retrieve from the queue.
Message filtering is useful if an  application must search the message queue for messages that have arrived later in the  queue. It is also useful if an application must process input (hardware) messages before  processing posted messages.   The WM_KEYFIRST and WM_KEYLAST constants can be used as filter values to  retrieve all keyboard messages; the WM_MOUSEFIRST and WM_MOUSELAST  constants can be used to retrieve all mouse messages.   Any application that filters messages must ensure that a message satisfying the message
filter can be posted. For example, if an application filters for a WM_CHAR message in a  window that does not receive keyboard input, the GetMessage function does not return.  This effectively "hangs" the application. 

Posting and Sending Messages


Any application can post and send messages. Like the system, an application posts a  message by copying it to a message queue and sends a message by passing the message  data as arguments to a window procedure. To post messages, an application uses the  PostMessage function. An application can send a message by calling the SendMessage,  BroadcastSystemMessage, SendMessageCallback, SendMessageTimeout,  SendNotifyMessage, or SendDlgItemMessage function. 

Posting Messages

An application typically posts a message to notify a specific window to perform a task.  PostMessage creates an MSG structure for the message and copies the message to the  message queue. The application's message loop eventually retrieves the message and  dispatches it to the appropriate window procedure.   An application can post a message without specifying a window. If the application  supplies a NULL window handle when calling PostMessage, the message is posted to the  queue associated with the current thread. Because no window handle is specified, the
application must process the message in the message loop. This is one way to create a  message that applies to the entire application, instead of to a specific window. 

Occasionally, you may want to post a message to all top-level windows in the system. An  application can post a message to all top-level windows by calling PostMessage and  specifying HWND_TOPMOST in the hwnd parameter. 

A common programming error is to assume that the PostMessage function always posts  a message. This is not true when the message queue is full. An application should check  the return value of the PostMessage function to determine whether the message has been  posted and, if it has not been, repost it. 

Sending Messages

An application typically sends a message to notify a window procedure to perform a task  immediately. The SendMessage function sends the message to the window procedure  corresponding to the given window. The function waits until the window procedure  completes processing and then returns the message result. Parent and child windows
often communicate by sending messages to each other. For example, a parent window  that has an edit control as its child window can set the text of the control by sending a  message to it. The control can notify the parent window of changes to the text that are  carried out by the user by sending messages back to the parent.   The SendMessageCallback function also sends a message to the window procedure  corresponding to the given window. However, this function returns immediately. After  the window procedure processes the message, the system calls the specified callback
function.   Occasionally, you may want to send a message to all top-level windows in the system.  For example, if the application changes the system time, it must notify all top-level  windows about the change by sending a WM_TIMECHANGE message. An application  can send a message to all top-level windows by calling SendMessage and specifying
HWND_TOPMOST in the
hwnd
 parameter. You can also broadcast a message to all  applications by calling the BroadcastSystemMessage function and specifying  BSM_APPLICATIONS in the  lpdwRecipients
parameter.   By using the
InSendMessage
 or
InSendMessageEx function, a window procedure can  determine whether it is processing a message sent by another thread. This capability is  useful when message processing depends on the origin of the message.

0 comments:

Post a Comment