Click here to Skip to main content
15,887,746 members
Articles / Desktop Programming / MFC

Merging Two Menus

Rate me:
Please Sign up or sign in to vote.
4.25/5 (10 votes)
15 Nov 2001 75.1K   21   5
This function will merge two menus

Introduction

This function will merge two menus. This can be used for context menus as well as for top level menus. At top level known popup menus will be appended or new popups will be created before Window or Help. Thanks to M. Knaup (mknaup@bigfoot.de) for the modifications. The function calls itself recursively and does not care about consecutive separators.

C++
bool 
MergeMenu(CMenu * pMenuDestination, const CMenu * pMenuAdd, bool bTopLevel /*=false*/)
{
    // Abstract:
    //      Merges two menus.
    //
    // Parameters:
    //      pMenuDestination    - [in, retval] destination menu handle
    //      pMenuAdd            - [in] menu to merge
    //      bTopLevel           - [in] indicator for special top level behavior
    //
    // Return value:
    //      <false> in case of error.
    //
    // Comments:
    //      This function calles itself recursivley. If bTopLevel is set to true,
    //      we append popups at top level or we insert before <Window> or <Help>.

    // get the number menu items in the menus
    int iMenuAddItemCount = pMenuAdd->GetMenuItemCount();
    int iMenuDestItemCount = pMenuDestination->GetMenuItemCount();
    
    // if there are no items return
    if( iMenuAddItemCount == 0 )
        return true;
    
    // if we are not at top level and the destination menu is not empty
    // -> we append a seperator
    if( !bTopLevel && iMenuDestItemCount > 0 )
        pMenuDestination->AppendMenu(MF_SEPARATOR);
    
    // iterate through the top level of <pmenuadd>
    for( int iLoop = 0; iLoop < iMenuAddItemCount; iLoop++ )
    {
        // get the menu string from the add menu
        CString sMenuAddString;
        pMenuAdd->GetMenuString( iLoop, sMenuAddString, MF_BYPOSITION );
        
        // try to get the submenu of the current menu item
        CMenu* pSubMenu = pMenuAdd->GetSubMenu(iLoop);
        
        // check if we have a sub menu
        if (!pSubMenu)
        {
            // normal menu item
            // read the source and append at the destination
            UINT nState = pMenuAdd->GetMenuState( iLoop, MF_BYPOSITION );
            UINT nItemID = pMenuAdd->GetMenuItemID( iLoop );
            
            if( pMenuDestination->AppendMenu( nState, nItemID, sMenuAddString ))
            {
                // menu item added, don't forget to correct the item count
                iMenuDestItemCount++;
            }
            else
            {
                TRACE( "MergeMenu: AppendMenu failed!\n" );
                return false;
            }
        }
        else
        {
            // create or insert a new popup menu item
            
            // default insert pos is like ap
            int iInsertPosDefault = -1;
            
            // if we are at top level merge into existing popups rather than
            // creating new ones
            if( bTopLevel )
            {
                ASSERT( sMenuAddString != "&?" && sMenuAddString != "?" );
                CString sAdd( sMenuAddString );
                sAdd.Remove('&');  // for comparison of menu items supress '&'
                bool bAdded = false;

                // try to find existing popup
                for( int iLoop1 = 0; iLoop1 < iMenuDestItemCount; iLoop1++ )
                {
                    // get the menu string from the destination menu
                    CString sDest;
                    pMenuDestination->GetMenuString( iLoop1, sDest, MF_BYPOSITION );
                    sDest.Remove( '&' ); // for a better compare (s.a.)
                    
                    if( sAdd == sDest )
                    {
                        // we got a hit -> merge the two popups
                        // try to get the submenu of the desired destination menu item
                        CMenu* pSubMenuDest = pMenuDestination->GetSubMenu( iLoop1 );
                        
                        if( pSubMenuDest )
                        {
                            // merge the popup recursivly and continue with outer for loop
                            if( !MergeMenu( pSubMenuDest, pSubMenu ))
                                return false;
                            
                            bAdded = true;
                            break;
                        }
                    }

                    // alternativ insert before <Window> or <Help>
                    if( iInsertPosDefault == -1 && 
                          ( sDest == "Window" || sDest == "?" || sDest == "Help" ))
                        iInsertPosDefault = iLoop1;
                }
                
                if( bAdded )
                {
                    // menu added, so go on with loop over pMenuAdd's top level
                    continue;
                }
            }

            // if the top level search did not find a position append the menu
            if( iInsertPosDefault == -1 )
                iInsertPosDefault = pMenuDestination->GetMenuItemCount();
            
            // create a new popup and insert before <Window> or <Help>
            CMenu NewPopupMenu;
            if( !NewPopupMenu.CreatePopupMenu() )
            {
                TRACE( "MergeMenu: CreatePopupMenu failed!\n" );
                return false;
            }
            
            // merge the new popup recursivly
            if( !MergeMenu( &NewPopupMenu, pSubMenu ))
                return false;
            
            // insert the new popup menu into the destination menu
            HMENU hNewMenu = NewPopupMenu.GetSafeHmenu();

            if( pMenuDestination->InsertMenu( iInsertPosDefault,
                MF_BYPOSITION | MF_POPUP | MF_ENABLED, 
                (UINT)hNewMenu, sMenuAddString ))
            {
                // don't forget to correct the item count
                iMenuDestItemCount++;
            }
            else
            {
                TRACE( "MergeMenu: InsertMenu failed!\n" );
                return false;
            }

            // don't destroy the new menu       
            NewPopupMenu.Detach();
        } 
    } 
    
    return true;
}</pmenuadd>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWorked great! Needs a small fix for 64bit. Pin
Iain Clarke, Warrior Programmer15-Oct-19 22:17
Iain Clarke, Warrior Programmer15-Oct-19 22:17 
Questionlicense Pin
digicompass2-Jan-13 21:44
digicompass2-Jan-13 21:44 
GeneralSimply, great job! Pin
PeterMB16-May-06 22:23
professionalPeterMB16-May-06 22:23 
GeneralNo name put in the menu bar Pin
IntelMacDeveloper17-Jun-02 20:34
IntelMacDeveloper17-Jun-02 20:34 
Do you have any idea why when I use this function my menu is created at the Help menu position? For example, I have a menu resource and I'm using MergeMenus to load it onto the mainframe menu. All of my items that are in the single menu resource replace the Help menu items and the Help menu items are lost. My menu that's being inserted does not get its name (Tools) in the menu bar.

Any ideas?

Thanks,
Fred
GeneralRe: No name put in the menu bar Pin
MichaelDecker2-Dec-02 13:59
MichaelDecker2-Dec-02 13:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.