In my MFC dialog based application I have main menu created with Visual Studio resource editor, and in one place there is dynamic menu, created at run time. All parts of menu that are created with Visual studio have modern look, and my dynamically created sub-menu has old look, as shown in image below.

My code for creating dynamic sub-menu is something like this (not the real code, but real code is not all that important):
CMenu subMenu;
subMenu.CreateMenu();
for (...)
{
subMenu.AppendMenu(
MF_STRING | (isChecked ? MF_CHECKED : MF_UNCHECKED),
<some menu ID>,
<some menu text>);
}
Inserting this sub-menu to where it belongs is done like this (pretty much the actual code):
TCHAR szMenuString[256];
MENUITEMINFO mii;
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS |
MIIM_DATA | MIIM_STRING | MIIM_BITMAP | MIIM_FTYPE;
mii.dwTypeData = szMenuString;
mii.cch = sizeof(szMenuString) / sizeof(szMenuString[0]);
GetMenu()->GetMenuItemInfo(ID_SUBMENU, &mii);
mii.fMask |= MIIM_SUBMENU;
mii.hSubMenu = subMenu.GetSafeHmenu();
GetMenu()->SetMenuItemInfo(ID_SUBMENU, &mii);
How can I create my sub-menu so that it appears the same in style as the rest of the main menu?
My code is written in MFC, but your answer does not have to be in MFC (and probably cannot be).
I discovered the solution, but don’t really understand what was happening. The solution is to prevent calling
DestroyMenuonsubMenu‘s destructor at the end of the function. This is done by either callingsubMenu.Detach(), or makingsubMenua pointer toCMenu.What I don’t understand is why is
DestroyMenuturning new style menu to old style. I would expect that the menu is either destroyed and not shown, or copied inSetMenuItemInfoand so its style preserved. Whoever provides an answer to this one gets my vote 🙂Also, I would like to know if I’m producing a resource leak by calling
Detachhere, or is my dynamic sub-menu destroyed along with the main menu. Points await the one who provides an answer.