• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

update command UI in a popup menu

I have a number of popup menus in my app which I show by calling CMenu::TrackPopupMenu().  Unfortunately, the popup menu does not send update-command-UI messages for the commands in the menu until after the menu command is chosen.  So several menu commands which should be disabled show as enabled but function as if they are disabled.

How can I change the following function to cause all the menu items to process update command UI messages before the popup menu is shown (or immediately after, or during the TrackPopupMenu() call)

void StandardPopupMenu(UINT nPopupMenu, CWnd* pCallbackWindow, CPoint TrackPoint)
    CMenu PopupMenu;
    //NOTE: every popup menu is one menu deep in the menu resource
    CMenu* pPopupMenu = PopupMenu.GetSubMenu(0);
    //TODONOW: make sure all of the commands in the menu process
    //  update command UI messages before showing the menu
        TrackPoint.x, TrackPoint.y, pCallbackWindow));
} //end StandardPopupMenu()
  • 2
  • 2
1 Solution
I want to know more about pCallbackWindow, what is it when you call StandardPopupMenu?

Generally, when you pass pCallbackWindow to StandardPopupMenu, you should add update-command-ui handlers to that window.

For example: you add those handlers to your Main Window, and pass the pointer to StandardPoppMenu.

Hope this help,
risAuthor Commented:
CWnd* pCallbackWindow is the window that calls StandardPopupMenu(), and it already has update-command-ui handlers for the applicable menu commands, and those handlers are called.  However, those update-command-ui handlers aren't called before the menu is displayed, but they are called after a menu item is clicked on.  So the behavior is something like this:

1) I right click on the window and that window class's MyWindow::OnContextMenu() handler is called.

2) MyWindow::OnContextMenu() calls StandardPopupMenu(IDR_POPUP_MENU, this, Point)

3) StandardPopupMenu() shows the menu.  Menu items which should be disabled appear enabled

4) I click on a menu item which should be disabled but appears enabled

5) the framework sends an update-command-ui message for that menu item command that I clicked

6) MyWindow::OnUpdateCommandUIMyMenuItem() handles the update-command-ui message for the should-be-disabled menu item that I clicked, and disables the menu item after I clicked it

7) since the menu item is now disabled (after I clicked it) the actual command message for the menu item I clicked is never sent, just as if the menu item had been disabled before I clicked it.

So how can I fix this problem?  Do I have to iterate through the items in pPopupMenu, get the command IDs, and manually set update-command-UI messages to pCallbackWindow before I call pPopupMenu->TrackPopupMenu()?  Will that work?  Is there an easier way?
The problem here is the framework does not support update-command-ui like in Doc-View architecture.
So you must manually handle WM_INITMENUPOPUP in the pCallbackWindow. In that handler, create an instance of CCmdUI, then set approxiate parameters for this, finally call DoUpdate to update command ui
here is the code to do that:
(I just copied from CFrameWnd::OnInitMenuPopup, change something to suit the need of Dialog-based app)
If you like, give me your email addr, then i will send a demo-app.

void CDialogTestDlg::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu)
     CDialog::OnInitMenuPopup(pMenu, nIndex, bSysMenu);

     if (bSysMenu)
          return;     // don't support system menu

     ASSERT(pMenu != NULL);
     // check the enabled state of various menu items

     CCmdUI state;
     state.m_pMenu = pMenu;
     ASSERT(state.m_pOther == NULL);
     ASSERT(state.m_pParentMenu == NULL);

     // determine if menu is popup in top-level menu and set m_pOther to
     //  it if so (m_pParentMenu == NULL indicates that it is secondary popup)
     HMENU hParentMenu;
     if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu)
          state.m_pParentMenu = pMenu;    // parent == child for tracking popup
     else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
          CWnd* pParent = GetTopLevelParent();
          // child windows don't have menus -- need to go to the top!
          if (pParent != NULL &&
               (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
               int nIndexMax = ::GetMenuItemCount(hParentMenu);
               for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
                    if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu)
                         // when popup is found, m_pParentMenu is containing menu
                         state.m_pParentMenu = CMenu::FromHandle(hParentMenu);

     state.m_nIndexMax = pMenu->GetMenuItemCount();
     for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
          state.m_nID = pMenu->GetMenuItemID(state.m_nIndex);
          if (state.m_nID == 0)
               continue; // menu separator or invalid cmd - ignore it

          ASSERT(state.m_pOther == NULL);
          ASSERT(state.m_pMenu != NULL);
          if (state.m_nID == (UINT)-1)
               // possibly a popup menu, route to first item of that popup
               state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex);
               if (state.m_pSubMenu == NULL ||
                    (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
                    state.m_nID == (UINT)-1)
                    continue;       // first item of popup can't be routed to
               state.DoUpdate(this, FALSE);    // popups are never auto disabled
               // normal menu item
               // Auto enable/disable if frame window has 'm_bAutoMenuEnable'
               //    set and command is _not_ a system command.
               state.m_pSubMenu = NULL;
               state.DoUpdate(this, state.m_nID < 0xF000);

          // adjust for menu deletions and additions
          UINT nCount = pMenu->GetMenuItemCount();
          if (nCount < state.m_nIndexMax)
               state.m_nIndex -= (state.m_nIndexMax - nCount);
               while (state.m_nIndex < nCount &&
                    pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
          state.m_nIndexMax = nCount;
risAuthor Commented:
Wow, code and everything!  Thank you so much!  That is exactly what I needed and it works very nicely!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now