DispatchMessage

Header: winuser.h

Library: User32.lib

Syntax

LRESULT DispatchMessage(
    const MSG *lpmsg
);

Parameters

lpmsgPointer to an MSG structure that contains the message to be dispatched.

Return value

If the function succeeds, the return value is the value returned by the window procedure that processes the message. If the function fails, the return value is zero. A zero value does not necessarily indicate an error; it may be a legitimate return value from the window procedure.

Remarks

DispatchMessage calls the window procedure for the window that receives the message. It should be used after retrieving a message with GetMessage or PeekMessage.

If the message is a WM_QUIT message, DispatchMessage does not call the window procedure.

Example

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int nCmdShow)
{
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";

    WNDCLASS wc = { };
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInst;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"DispatchMessage Demo",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
        NULL, NULL, hInst, NULL);

    if (hwnd == NULL) return 0;

    ShowWindow(hwnd, nCmdShow);

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

See also