Shown below is a code with which attempted to get Menu Data, especially menu string, at runtime, unfortunately nothing was returned. Debugging effort showed that the cbSize, fMask and fType data members of the MENUITEMINFO structure that were initally set were set. Absolutely nothing was returned. What is the right approach?
MENUITEMINFO MenuInfo{};
MenuInfo.cbSize = sizeof(MENUITEMINFO);
MenuInfo.fMask = MIIM_STRING | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
MenuInfo.fType = MFT_STRING;
MenuInfo.dwTypeData = NULL;
GetMenuItemInfo(hMenu, i, TRUE, &MenuInfo);
std::wstring wstMenuString(MenuInfo.cch + 1, L'\0');
MenuInfo.cch++;
MenuInfo.cbSize = sizeof(MENUITEMINFO);
MenuInfo.fMask = MIIM_STRING | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
MenuInfo.fType = MFT_STRING;
MenuInfo.dwTypeData = &wstMenuString[0];
GetMenuItemInfo(hMenu, i, TRUE, &MenuInfo);
Shown below is a complete version of the code.
void ModifyAppMenu(HWND hWnd)
{
hOwnerDrawnMenuFont = GetAppFont();
HMENU hMenu = GetMenu(hWnd);
int iMenuCount = GetMenuItemCount(hMenu);
for (int i = 0; i < iMenuCount; i++)
{
MENUITEMINFO MenuInfo{};
MenuInfo.cbSize = sizeof(MENUITEMINFO);
MenuInfo.fMask = MIIM_STRING | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
MenuInfo.fType = MFT_STRING;
MenuInfo.dwTypeData = NULL;
GetMenuItemInfo(hMenu, i, TRUE, &MenuInfo);
std::wstring wstMenuString(MenuInfo.cch + 1, L'\0');
MenuInfo.cch++;
MenuInfo.cbSize = sizeof(MENUITEMINFO);
MenuInfo.fMask = MIIM_STRING | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
MenuInfo.fType = MFT_STRING;
MenuInfo.dwTypeData = &wstMenuString[0];
GetMenuItemInfo(hMenu, i, TRUE, &MenuInfo);
vMenuText.push_back(utf8_encode(wstMenuString));
unsigned uSubMenuID{ 0 };
HMENU hSubMenu{ nullptr };
if (MenuInfo.hSubMenu == NULL) {
uSubMenuID = MenuInfo.wID; }
else {
hSubMenu = MenuInfo.hSubMenu; }
MenuInfo.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
MenuInfo.fType = MFT_OWNERDRAW;
MenuInfo.dwItemData = reinterpret_cast<ULONG_PTR>(&vMenuText[static_cast<int>(vMenuText.size()) - 1][0]);
if (!uSubMenuID)
{
MenuInfo.hSubMenu = hSubMenu;
}
else
{
MenuInfo.hSubMenu = nullptr;
}
MenuInfo.wID = uSubMenuID;
SetMenuItemInfo(hMenu, i, TRUE, &MenuInfo);
if (hSubMenu != NULL)
{
int iSubMenuCount = GetMenuItemCount(hSubMenu);
for (int j = 0; j < iSubMenuCount; i++)
{
MENUITEMINFO SubMenuInfo{};
SubMenuInfo.cbSize = sizeof(MENUITEMINFO);
SubMenuInfo.fMask = MIIM_STRING | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
SubMenuInfo.fType = MFT_STRING;
SubMenuInfo.dwTypeData = nullptr;
GetMenuItemInfo(hSubMenu, j, TRUE, &SubMenuInfo);
std::wstring wstSubMenuString(SubMenuInfo.cch + 1, L'\0');
MenuInfo.cch++;
SubMenuInfo.dwTypeData = &wstSubMenuString[0];
GetMenuItemInfo(hSubMenu, j, TRUE, &SubMenuInfo);
vMenuText.push_back(utf8_encode(wstSubMenuString));
unsigned uSubSubMenuID{ 0 };
HMENU hSubSubMenu{ nullptr };
if (MenuInfo.hSubMenu == NULL) {
uSubSubMenuID = MenuInfo.wID; }
else {
hSubSubMenu = MenuInfo.hSubMenu; }
SubMenuInfo.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
SubMenuInfo.fType = MFT_OWNERDRAW;
SubMenuInfo.dwItemData = reinterpret_cast<ULONG_PTR>(&vMenuText[static_cast<int>(vMenuText.size()) - 1][0]);
if (!uSubSubMenuID)
{
SubMenuInfo.hSubMenu = hSubSubMenu;
}
else
{
SubMenuInfo.hSubMenu = nullptr;
}
SubMenuInfo.wID = uSubSubMenuID;
SetMenuItemInfo(hSubMenu, j, TRUE, &SubMenuInfo);
if (hSubSubMenu != NULL)
{
int iSubSubMenuCount = GetMenuItemCount(hSubSubMenu);
for (int k = 0; k < iSubSubMenuCount; i++)
{
MENUITEMINFO SubSubMenuInfo{};
SubSubMenuInfo.cbSize = sizeof(MENUITEMINFO);
SubSubMenuInfo.fMask = MIIM_STRING | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
SubSubMenuInfo.fType = MFT_STRING;
SubSubMenuInfo.dwTypeData = nullptr;
GetMenuItemInfo(hSubSubMenu, k, TRUE, &SubSubMenuInfo);
std::wstring wstSubSubMenuString(SubSubMenuInfo.cch + 1, L'\0');
MenuInfo.cch++;
SubSubMenuInfo.dwTypeData = &wstSubSubMenuString[0];
GetMenuItemInfo(hSubSubMenu, k,TRUE, &SubSubMenuInfo);
vMenuText.push_back(utf8_encode(wstSubSubMenuString));
unsigned uSubSubSubMenuID{ 0 };
HMENU hSubSubSubMenu{ nullptr };
if (MenuInfo.hSubMenu == NULL) {
uSubSubSubMenuID = MenuInfo.wID; }
else {
hSubSubSubMenu = MenuInfo.hSubMenu; }
SubSubMenuInfo.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
SubSubMenuInfo.fType = MFT_OWNERDRAW;
SubSubMenuInfo.dwItemData = reinterpret_cast<ULONG_PTR>(&vMenuText[static_cast<int>(vMenuText.size()) - 1][0]);
if (!uSubSubSubMenuID)
{
SubSubMenuInfo.hSubMenu = hSubSubSubMenu;
}
else
{
SubSubMenuInfo.hSubMenu = nullptr;
}
SubSubMenuInfo.wID = uSubSubSubMenuID;
SetMenuItemInfo(hSubSubMenu, k, TRUE, &SubSubMenuInfo);
}
}
}
}
}
}
What I have tried:
I have spent considerable time debugging to figure out what was wrong.