Processing the WM_CONTEXTMENU Message

The WM_CONTEXTMENU message is generated when an application's window procedure passes the WM_RBUTTONUP or WM_NCRBUTTONUP message to the DefWindowProc function. The application can process this message to display a shortcut menu appropriate to a specific portion of its screen. If the application does not display a shortcut menu, it should pass the message to DefWindowProc for default handling.

Following is an example of WM_CONTEXTMENU message processing as it might appear in an application's window procedure. The low-order and high-order words of the lParam parameter specify the screen coordinates of the mouse when the right mouse button is released. The application-defined OnContextMenu function returns TRUE if it displays a context menu, or FALSE if it does not.


if (!OnContextMenu(hwnd, LOWORD(lParam), HIWORD(lParam)))

return DefWindowProc(hwnd, uMsg, wParam, lParam);


The following application-defined OnContextMenu function displays a shortcut menu if the specified mouse position is within the window's client area. A more sophisticated function might display one of several different menus, depending on which portion of the client area is specified. To actually display the shortcut menu, this example calls an application-defined function called DisplayContextMenu. For a description of this function, see Displaying a Shortcut Menu.

BOOL WINAPI OnContextMenu(HWND hwnd, int x, int y)


RECT rc; // client area of window

POINT pt = { x, y }; // location of mouse click

// Get the bounding rectangle of the client area.

GetClientRect(hwnd, &rc);

// Convert the mouse position to client coordinates.

ScreenToClient(hwnd, &pt);

// If the position is in the client area, display a

// shortcut menu.

if (PtInRect(&rc, pt)) {

ClientToScreen(hwnd, &pt);

DisplayContextMenu(hwnd, pt);

return TRUE;


// Return FALSE if no menu is displayed.

return FALSE;


Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database