How to Display a Shortcut Menu from an Existing Resource

4/19/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.

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

Tasks

How to Extend Shortcut Menus

Concepts

How to Register a File System Shortcut Menu
Menu Overview

Other Resources

Developing Menu Components