Next I wanted to add a context menu. This requires handling the WM_CONTEXTMENU message, which is sent when the user clicks the right mouse button in the client window. For instance, when the user right-clicks a list view item (the wParam parameter is filled in with the handle of the window that received the right click), a context menu for that item appears.
This task is straightforward. Basically, you load the menu and call TrackPopupMenu, as you would in a 16-bit Windows-based application. When the user chooses an item from the context menu, the appropriate command is generated and sent to the window procedure as a WM_COMMAND message. The following code, from the main window procedure, demonstrates how to handle the WM_CONTEXTMENU message:
case WM_CONTEXTMENU:
// The right mouse button has been clicked.
if ((HWND)wParam == g_Listing.hWndListView)
{
// Get the context menu from the resource file.
hMenu = LoadMenu (g_Listing.hInst, "HousePopupMenu");
if (! hMenu)
break;;
// Get the first item in the menu for TrackPopupMenu().
hMenuTrackPopup = GetSubMenu (hMenu, 0);
// Draw and track the "floating" menu.
TrackPopupMenu (hMenuTrackPopup,
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
LOWORD(lParam), HIWORD(lParam),
0, g_Listing.hWndListView, NULL);
// Destroy the menu.
DestroyMenu (hMenu);
}
break;