Tried to make cascading dynamic popup menus in MFC.
Mind you, there is a dynamic menu example on MSDN
. But all that does is dynamically deleting given menu items from a menu. What if you want a cascading hierarchy of popup menus depending on your
(tree-structured) data, not known at compile time, only at runtime, generated only as the menu tree is actually expanded?
Here is how it should work:
Somewhere, you have the top-level popup menu coming up, calling
, you pass a window,
If the window is a frame window, a
you can set
on that window to FALSE.
That will prevent automatic graying out of menu items for which the window doesn't
find a handler. In that case, you should properly handle all your menu items,
or there will be non-grayed-out, selectable, but non-functional menu items.
BOOL bOldAutoMenuEnable = ((CFrameWnd*)pWndPopupOwner)->m_bAutoMenuEnable;
((CFrameWnd*)pWndPopupOwner)->m_bAutoMenuEnable = FALSE;
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
((CFrameWnd*)pWndPopupOwner)->m_bAutoMenuEnable = bOldAutoMenuEnable;
ok, so then, the class of your popup owner window should have overridden
, like so:
public MyPopupOwnerWindow: public CWindow (or CFrameWnd)
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
... because OnCmdMsg is the one that's called when a popup menu item is either being updated or is being clicked.
gets passed to it a pointer to a command handler function. For dynamically added menu items, there are no handler functions in the message map, obviously, so the handler info will be
for those menu items.
We want cascading, dynamically created popup menus, so there are certain menu items which, as we find out at the time of expanding the menu, should instead be replaced with a popup menu.
The only place where we can do this is in OnCmdMsg, the virtual method that gets called when the menu item is updated for dislplay.
is called, this is done a few call stack levels down from
inside a loop that iterates through the menu items by index. Therefore we have to make sure that when we replace menu items with popup menus, we won't shuffle around the sequence of menu items.
So our OnCmdMsg will look like this:
BOOL MyPopupOwnerWindow::OnCmdMsg(UINT nID, int nCode, void* pExtra,
if (pHandlerInfo == NULL)
if ( )
if (nCode == CN_COMMAND)
else if (nCode == CN_UPDATE_COMMAND_UI)
if ( )
pExtra->m_pMenu->DeleteMenu( pExtra->m_nIndex, MF_BYPOSITION ) ;
CMenu MyPopupSubMenu ;
int nIDSubItem1 = GetOneOfMyOwnNewUniqueMenuIDs() ;
MyPopupSubMenu.AppendMenu( MF_STRING, nIDSubItem1, GetNewSubMenuItemTitle(nID,nIDSubItem1) ) ;
int nIDSubItem2 = GetOneOfMyOwnNewUniqueMenuIDs() ;
MyPopupSubMenu.AppendMenu( MF_STRING, nIDSubItem2, GetNewSubMenuItemTitle(nID,nIDSubItem2) ) ;
int nIDSubItem3 = GetOneOfMyOwnNewUniqueMenuIDs() ;
MyPopupSubMenu.AppendMenu( MF_STRING, nIDSubItem3, GetNewSubMenuItemTitle(nID,nIDSubItem3) ) ;
pExtra->m_pMenu->InsertMenu( pExtra->m_nIndex, MF_POPUP | MF_BYPOSITION, MyPopupSubMenu.GetSafeHmenu(), GetPopupMenuTitle( nID ) ) ;
CMenu *pMenu = pExtra->m_pMenu->GetSubmenu( pExtra->m_nIndex ) ;
if ( NULL == pMenu )
TRACE(_T("Added submenu not retrieved at position %d\n"), pExtra->m_nIndex ) ;
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
Now, this should work. In particular, it should work recursively: If
, one of the menu item IDs added dynamically, in turn must be replaced by a popup menu, this would have to be done when
is called with
nID == nIDSubItem1
nCode == CN_UPDATE_COMMAND_UI
But: Even though the inserted menu point is replaced by a submenu, this is not shown in the UI. Insert does work, but
, as if no popup menu was inserted, and in the UI, those menu items don't have a little "arrow" next to their labels and don't pop up the menu that I have so painstakingly added.
So does anyone have a better idea how to do this?