Click here to Skip to main content
15,612,595 members
Articles / Desktop Programming / MFC
Posted 15 Nov 2001


21 bookmarked

Merging Two Menus

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


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 ( for the modifications. The function calls itself recursively and does not care about consecutive separators.

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 )
    // 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
                TRACE( "MergeMenu: AppendMenu failed!\n" );
                return false;
            // 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;

                    // 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

            // 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,
                (UINT)hNewMenu, sMenuAddString ))
                // don't forget to correct the item count
                TRACE( "MergeMenu: InsertMenu failed!\n" );
                return false;

            // don't destroy the new menu       
    return true;


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 
I needed to merge popup menus - function works just fine! My five Smile | :)
GeneralNo name put in the menu bar Pin
IntelMacDeveloper17-Jun-02 20:34
IntelMacDeveloper17-Jun-02 20:34 
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.