DefWindowProc Function

Winuser.h

The DefWindowProc function passes a message that the calling application has not processed to the default window procedure. This function ensures that every message is processed. The default window procedure handles messages for windows that do not belong to any of the registered classes.

LRESULT DefWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

Parameters

hWnd

A handle to the window procedure that is to receive the message.

Msg

The message to be posted.

wParam

Additional message information. The exact meaning depends on the message being passed.

lParam

Additional message information. The exact meaning depends on the message being passed.

Return Value

The return value is the result of the message processing and depends on the message sent.

Remarks

The DefWindowProc function is used by an application's window procedure to process messages that the application does not handle explicitly. If your application's window procedure handles all the messages it receives, you can return zero from the procedure for any unhandled messages. However, it is generally recommended to call DefWindowProc for all unhandled messages to ensure proper system behavior.

Commonly, the DefWindowProc function is called for messages that the application does not process. For example, if your window procedure receives a message for which it does not have a specific handler, you would pass that message to DefWindowProc:


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_COMMAND:
            // Handle command messages
            break;
        case WM_PAINT:
            // Handle paint messages
            break;
        default:
            // Pass all unhandled messages to the default window procedure
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0; // For handled messages
}
            

Requirements

Header: Winuser.h (include Windows.h)

Library: Use User32.lib

See Also