Search code examples
c++mfcwin32com

How to get the zero based indexes all sub-menus in a CMenu object


I have a CMenu instance. I would like to dynamically traverse all menu and sub-menu items in the menu.

The API for getting a sub-menu is called CMenu::GetSubMenu. Its lone parameter is the sub-menu's position (zero based index) within the parent menu.

How do I get the positions of all sub-menus?


Solution

  • Call CMenu::GetMenuItemCount() to determine the number of menu items. Iterate over the menu items, calling CMenu::GetSubMenu(nPos) for each to determine whether that menu item has a submenu (i.e. the call does not return NULL).