DefWindowProc() Overview

DefWindowProc: DefWindowProc() does the default processing of messages defined in win32 sub-system. Application calls this function to those messages which does not have any processing inside the application. This ensures that application is processing every single message. Application calls the DefWindowProc() with the same parameters received by the window procedure. Win32 subsystem performs default action for the message when this is called.

DefWindowProc() prototype

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

Parameters

  • hWnd - Handle to the window procedure that received the message.
  • Msg - Specifies the message.
  • wParam - Specifies additional message information. The content of this parameter depends on the value of the Msg parameter.
  • lParam - Specifies additional message information. The content of this parameter depends on the value of the Msg parameter.

Return Values

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

Windows message processing

Windows Event processing and DefWindowProc

Windows GUI subsystem is a server for all application runnding on the frontend desktop. Every application running acts as a client and process an infinite loop of message processing from windows GUI subsystem. It this Window proc is a single point callback. Application process events like keyboard / mouse events can handle individial functions. However each message should be processed and Window proc should return a value. So the remaining messages those are not handled should go to DefWindowProc(). DefWindowProc() is a call back to Win32 GUI server to handle default action for this message.

Source Code

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  WNDCLASS wc;
  MSG msg;
  HWND hWnd;
  ZeroMemory(&amp;wc, sizeof(WNDCLASS));

  wc.style           = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc     = (WNDPROC)WndProc;
  wc.hInstance       = hInstance;
  wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
  wc.lpszClassName   = (LPCTSTR)"MyWin32Class";


  RegisterClass(&wc);

   hWnd = CreateWindow("MyWin32Class", "DefWindowProc demo", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    DispatchMessage(&msg);
  }
  return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message) 
  {
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
    /* Call DefWindowProc() as default */
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#