Message Processing

Windows graphical applications (GUI) are based on message queues. Windows operating system has an event and graphical management server running as a background system process. This process acts as a message queue server and all windows applications act as message queue clients. Application calls GetMessage to receive a message from the event server. This is a blocking call and it waits for a message to arrive. The operating system receives keyboard inputs and mouse movements or clicks or other system events. All these are events are passed to the event and graphical management server. The server constructs a message object of type MSG and sends this to the message queue client which is the current application. There can be many applications running but the server decides to send the message to the application whose top-level window is focused on inputs. GetMessage blocking call in WinMain returns and this event MSG is further passed to DispatchMessage(). DispatchMessage() is an API that indirectly triggers a call back to window procedure which is registered by application with windows class. The application does not call this function directly, Win32 subsystem calls this function when the application dispatches the particular message. GetMessage and DispatchMessage are generally placed in an infinite loop. It keeps receiving new messages and further dispatches to wndproc till the application exits.

Windows Event processing and Window procedure wndproc


The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a place holder for the application-defined function name. Application processes only messages those are needed and for the rest, it passes to DefWindowProc() for default handling in Win32 subsystem.

  HWND hwnd,      /* handle to window */
  UINT uMsg,      /* message identifier */
  WPARAM wParam,  /* first message parameter */
  LPARAM lParam   /* second message parameter */


  • hwnd - Handle to the window.
  • uMsg - Specifies the message.
  • wParam - Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter.
  • lParam -Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter.

Return Values

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

Source Code

#include <windows.h>

                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
  MSG msg;
  HWND hWnd;
  ZeroMemory(&amp;wc, sizeof(WNDCLASS));           = 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";


   hWnd = CreateWindow("MyWin32Class", "Hello World!", WS_OVERLAPPEDWINDOW,

  ShowWindow(hWnd, nCmdShow);
  while (GetMessage(&msg, NULL, 0, 0)) 
  return 0;

  switch (message) 
    case WM_DESTROY:
      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 ...