mukesh
mukesh

Reputation: 726

Retrieve list of menu items in Windows in C++

I am trying to implement menu item search inside my desktop application . I want user to be able to type any menu item string inside a search box and invoke that menu item directly from the search result. This would be similar to the menu search inside mac. How can i retrieve the list of menu items for my application.

Upvotes: 0

Views: 1413

Answers (1)

Edward Clements
Edward Clements

Reputation: 5142

Here's a code snippet that you can modify according to your needs:

void InterateMenu(HMENU hMenu)
{   MENUITEMINFO mii;
    int i, nCount = GetMenuItemCount(hMenu);

    for (i = 0; i < nCount; i++)
    {   memset (&mii, 0, sizeof(mii));
        mii.cbSize = sizeof(mii);
        mii.fMask = MIIM_SUBMENU | MIIM_TYPE | MIIM_ID; // | MIIM_STATE
        if (!GetMenuItemInfo(hMenu, i, TRUE, &mii))
            continue;
        if ((mii.fType & MFT_STRING) != 0 && mii.cch > 0)
        {   mii.cch++;
            TCHAR *pString = (TCHAR *) malloc(mii.cch  * sizeof(TCHAR));
            if (pString != NULL)
            {   if (!GetMenuItemInfo(hMenu, i, TRUE, &mii))
                {   free(pString);
                    continue;
                }
                TRACE(_T("ID = %u, string = %s\n"), mii.wID, pString);
                free(pString);
            }
        }
        if (mii.hSubMenu != NULL)
            InterateMenu(mii.hSubMenu); // ** recursive **
    }
}

Call the function with the main menu handle.

Upvotes: 2

Related Questions