WindowProc ( WPARAM and LPARAM)

WindowProc: The WindowProc function is an application-defined function that processes messages sent to a window. WPARAM and LPARAM are third and forth parameter in WindowProc() callback. Both WPARAM and LPARAM are dependent on message identifier. For some message identifiers these has values and for some others these are either NULL or 0.

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.

What do the prefix letter W and L stand for in WPARAM and LPARAM?

The W in WPARAM stands for WORD and the L in LPARAM stands for LONG. These two parameters are the two parameters for the uMsg message code.

WM_MOUSEMOVE (WPARAM LPARAM)

WM_MOUSEMOVE uses all the parameters. This is one good example to describe how WPARAM and LPARAMs are used. Mouse move or WM_MOUSEMOVE message has the following meaning for WPARAM and LPARAM.

Message: WM_MOUSEMOVE 
Mouse+Ctrl Kyes: fwKeys = wParam;
Mouse horizontal position: xPos = LOWORD(lParam);
Mouse vertical position: yPos = HIWORD(lParam); 

WM_MOUSEMOVE (WPARAM LPARAM) demo

#include <windows.h>
#include <stdio.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(&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)"WPARAMLPARAMClass";


  RegisterClass(&wc);

  hWnd = CreateWindow("WPARAMLPARAMClass",
                      "WPARAM LPARAM 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;
}

WORD ButtonValues[] =
{
  MK_CONTROL, /* 0x0008 The CTRL key is down. */
  MK_LBUTTON, /* 0x0001 The left mouse button is down.*/
  MK_MBUTTON, /* 0x0010 The middle mouse button is down.*/
  MK_RBUTTON, /* 0x0002 The right mouse button is down.*/
  MK_SHIFT,   /* 0x0004 The SHIFT key is down. */

};

char *ButtonStrings[] =
{
  "Ctrl ",   /* 0x0008 The CTRL key is down. */
  "Mouse-L ",/* 0x0001 The left mouse button is down. */
  "Mouse-M ",/* 0x0010 The middle mouse button is down. */
  "Mouse-R ",/* 0x0002 The right mouse button is down. */
  "Shift ",  /* 0x0004 The SHIFT key is down.*/
};

char * WPARAM2STRING(WPARAM wParam, CHAR *key)
{
  strcpy(key, "");
  for (int i =0; i < sizeof(ButtonValues)/sizeof(ButtonValues[0]); i++)
  {
  if ((ButtonValues[i] & wParam) == ButtonValues[i])
    strcat(key, ButtonStrings[i]);
  }
  return key;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  CHAR text[100];
  CHAR key[50];

  switch (message) 
  {
    case WM_MOUSEMOVE:
  {
      sprintf(text, "Window 0x%X Message:%X Button: %s Location (%d:%d)",
              hWnd,
              message,
              WPARAM2STRING(wParam, key),
              LOWORD(lParam),
              HIWORD(lParam)
              );
    SetWindowText(hWnd, text);
    break;
  }
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

Output

We have pressed control key, shift key and left mouse key along with moved the mouse on the window.

wm_mousemove wparam lparam

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

#