How to Display a Shortcut Menu from an Existing Resource

Windows Mobile 6.5
4/19/2010

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

Bb158545.note(en-us,MSDN.10).gifNote:
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;
}

Community Additions

Show: