Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS General Discussion » suggested way to remove menu item from command update handler Collapse All
Subject Author Date
Chris Thomas Jul 1, 2005 - 8:33 AM

Hello, I’m in the final stages of porting an existing MFC app to use Prof-UIS, and I ran into this sort of code in command update UI handlers:

pCmdUI->m_pMenu->RemoveMenu(nID, MF_BYCOMMAND);

But with Prof-UIS m_pMenu seems to be NULL for top level menus. I really don’t want to have to navigate through the menu bar’s CMenu, is there a better way?

Technical Support Jul 1, 2005 - 10:03 AM

Prof-UIS menus are not based on HMENU handles. They are based on CExtPopupMenuWnd windows. This class sends the CExtPopupMenuWnd::g_nMsgPrepareMenu registered windows message which allows you to modify pop-up menu on-the-fly immediately before it appears on the screen. Here is the declaration, message map’s entry and method’s body for handling this message:

afx_msg LRESULT OnExtMenuPrepare( WPARAM wParam, LPARAM lParam );

ON_REGISTERED_MESSAGE(
      CExtPopupMenuWnd::g_nMsgPrepareMenu,
      OnExtMenuPrepare
      )

LRESULT CMainFrame::OnExtMenuPrepare( WPARAM wParam, LPARAM lParam )
{
      lParam;
CExtPopupMenuWnd::MsgPrepareMenuData_t * pData =
            reinterpret_cast
                  < CExtPopupMenuWnd::MsgPrepareMenuData_t * >
                  ( wParam ) 
      ASSERT( pData != NULL );
CExtPopupMenuWnd * pPopup = pData->m_pPopup;
      ASSERT( pPopup != NULL );
      . . . 
      // modify the pPopup menu
      . . . 
      return TRUE;
}

If the pop-up menu has been modified, then the pData->m_bMenuChanged property should be set to true by the OnExtMenuPrepare() method. To cancel pop-menu tracking, set pData->m_bMenuCanceled to true.

The CExtPopupMenuWnd::g_nMsgPrepareOneMenuLevel registered windows message is also sent by the pop-up menu before it appears on the screen. This message has the same parameters but it is sent separately for each pop-up menu level. Please note that the CExtPopupMenuWnd::g_nMsgPrepareOneMenuLevel registered windows message is available in Prof-UIS version 2.33 and can be downloaded by registered users. It will be available soon in Prof-UIS 2.40 for all users.

You need to handle one of this messages to get what you want. By handling these registered windows messages, you can implement dynamically generated command lists similar to the most recently used file list (MRU) used in MFC. Just create a pre-defined command item in the menu, find and remove it in the OnExtMenuPrepare() method, and insert new menu items at the position of the removed item. To find a menu item by its command identifier, you can use the CExtPopupMenuWnd::ItemFindPosForCmdID() method. The menu modification code should allow for more than one command identifiers that are replaced:

INT nReplacePos =
            pPopup->ItemFindPosForCmdID( ID_OF_ITEM_TO_REMOVE );
      for( ;      nReplacePos >= 0;
            nReplacePos =
                  pPopup->ItemFindPosForCmdID( ID_OF_ITEM_TO_REMOVE )
            )
      {
            VERIFY( pPopup->ItemRemove(nReplacePos)  );
            VERIFY(
                  pPopup->ItemInsert(
                        ID_OF_NEW_COMMAND_ITEM,
                        nReplacePos
                        )
                  ); 
            . . . 
      }