Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
4/14/2010

Code Example

The following code example demonstrates how to display a shortcut menu based on an existing resource.

Note:
To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.
Copy Code
// Global for handle to menu resource.
HMENU g_hMainMenu;

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   ...
	// Load the menu resource to use later.
	g_hMainMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDM_MENU));
	...
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
						 WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		...
		case :
		{
			SHRGINFO  shrg;
			HMENU	hmenu;

			shrg.cbSize = sizeof(shrg);
			shrg.hwndClient = hWnd;
			shrg.ptDown.x = LOWORD(lParam);
			shrg.ptDown.y = HIWORD(lParam);
			shrg.dwFlags = SHRG_RETURNCMD ;

			if ((&shrg) == GN_CONTEXTMENU) 
			{
				hmenu = GetSubMenu(g_hMainMenu, 0);
				TrackPopupMenuEx(hmenu, TPM_LEFTALIGN,
LOWORD(lParam), 
								 HIWORD(lParam), hWnd, NULL);
		}
			break;
	}
}
	return 0;
}

See Also