Share via


How to: Display a Shortcut Menu from an Existing Resource

Send Feedback

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 WM_LBUTTONDOWN:
        {
            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 (SHRecognizeGesture(&shrg) == GN_CONTEXTMENU) 
            {
                hmenu = GetSubMenu(g_hMainMenu, 0);
                TrackPopupMenuEx(hmenu, TPM_LEFTALIGN, LOWORD(lParam), 
                                 HIWORD(lParam), hWnd, NULL);
            }
            break;
        }
    }
    return 0;
}

See Also

Menus | How to: Extend Shortcut Menus | How to: Register a File System Shortcut Menu | Shortcut Menu Overview

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.