Message Processing

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

Windows Event processing and Window procedure wndproc

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.


LRESULT CALLBACK WindowProc(
  HWND hwnd,      /* handle to window */
  UINT uMsg,      /* message identifier */
  WPARAM wParam,  /* first message parameter */
  LPARAM lParam   /* second message parameter */
);

Parameters

  • 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>

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", "Hello World!", 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:
      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 ...

#