简体   繁体   English

在C ++中检索Windows中的菜单项列表

[英]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. 这类似于mac内的菜单搜索。 How can i retrieve the list of menu items for my application. 如何检索应用程序的菜单项列表。

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. 使用主菜单句柄调用该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM