Windows Messaging (Win32 API)
The Windows Messaging API is a fundamental component of the Win32 API that enables inter-process communication (IPC) and the handling of user input and system events within the Windows operating system. It allows applications to send messages to each other, receive notifications, and manage the flow of information between different parts of the system.
Core Concepts
- Messages: Messages are the fundamental unit of communication in Windows. They are typically 32-bit values, often structured as a
WORDfor the message type (e.g.,WM_PAINT,WM_KEYDOWN) and aDWORDfor the message parameter (wParamandlParam). - Message Loops: Every Windows application has a message loop that continuously retrieves messages from the application's message queue and dispatches them to the appropriate window procedure for processing.
- Window Procedures (
WndProc): These are callback functions that handle messages for a specific window. They receive messages from the message loop and perform actions based on the message type. - Message Queues: Each thread that creates a window has its own message queue where messages destined for that thread's windows are stored.
- Broadcasting vs. Sending: Messages can be sent directly to a specific window or thread (using functions like
SendMessage) or broadcast to all top-level windows (usingBroadcastSystemMessage).
Key Functions
SendMessage
Sends a message to one or more windows. The function waits until the window procedure has processed the message before returning.
LRESULT SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
hWnd: Handle to the window that will receive the message.Msg: The message to be sent.wParam: Additional message-specific information.lParam: Additional message-specific information.
PostMessage
Places a message in the message queue of the specified thread or window. The function returns immediately without waiting for the message to be processed.
BOOL PostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
- Parameters are similar to
SendMessage.
GetMessage
Retrieves a message from the message queue. If the message queue is empty, the function waits until a message is available.
BOOL GetMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax);
lpMsg: Pointer to anMSGstructure that receives the message information.hWnd: Handle to the window to retrieve messages for. IfNULL, messages for all windows created by the calling thread are retrieved.wMsgFilterMin/wMsgFilterMax: Range of message types to retrieve.
TranslateMessage
Translates virtual-key messages into character messages. These character messages are posted to the application's message queue, to be retrieved by the next call to GetMessage.
BOOL TranslateMessage(const MSG *lpMsg);
DispatchMessage
Dispatches a message to the appropriate window procedure (WndProc) for processing.
LRESULT DispatchMessage(const MSG *lpMsg);
Basic Message Loop Example
A typical Win32 application message loop:
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Common Message Types
WM_CREATE: Sent when a window is first created.WM_DESTROY: Sent when a window is being destroyed.WM_PAINT: Sent when the client area of a window needs to be redrawn.WM_SIZE: Sent when the size of a window has changed.WM_KEYDOWN,WM_KEYUP: Sent when a key is pressed or released.WM_LBUTTONDOWN,WM_MOUSEMOVE,WM_RBUTTONUP: Various mouse input messages.WM_COMMAND: Sent when a menu item is selected, a button is clicked, or an accelerator key is pressed.
Advanced Topics
- Message Hooks: Intercepting and processing messages before they reach their destination.
- Asynchronous vs. Synchronous Messaging: Understanding the difference between
PostMessageandSendMessage. - Thread Synchronization: Using messages for communication and synchronization between different threads.
- Custom Window Messages: Defining and using your own message types (typically in the range
WM_USERand above).