Solved

Reloading menus

Posted on 1998-07-14
7
513 Views
Last Modified: 2013-11-20
I'd like to be able to programmatically restore my applications menu to the state it was in when the application first started.   However, since MFC keeps some of the original HMENU handles I can't simply DestroyMenu the current one and LoadMenu the new one.  Any suggestions on how to do this?
0
Comment
Question by:JohnWeidner
7 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 1319156
John..
        Do u want to remove the default menu used by the MFC app and show u'r custom menu?? or do  u mean something else??? please make this point clear..
0
 

Author Comment

by:JohnWeidner
ID: 1319157
Edited text of question
0
 

Author Comment

by:JohnWeidner
ID: 1319158
The program allows the user to customize the menus to the point of even allowing "standard" menu items to be deleted.   I'd like to be able to restore the menu to it's previous "un-customized" state.   Since, the description of the un-customized menu is already in the resource file, I'd like to be able to LoadMenu the original menu.   I'd really rather not have to keep track of what menu items were deleted and InsertMenu them back in.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:duneram
ID: 1319159
Why not just reload the menu from the resource file?   YOu could do a LoadMenu("...") and then get the handle to the old one and destroy it, and then put this one in its place or

Just make sure you use the SetMenu and Detach functions when you are ready to relate the new menu with the window.
0
 

Author Comment

by:JohnWeidner
ID: 1319160
The problem is that MFC keeps copies of the HMENU of the original menu.   So if you use DeleteMenu on the original menu, MFC will start ASSERTing because the handles it saved are no longer valid.
0
 

Author Comment

by:JohnWeidner
ID: 1319161
I solved this problem.   Basically, I deleted all the submenus of the main menu, and then insterted the subMenus from a new copy of the menu loaded from the resource file.   Here's the code:

void CMainFrame::OnRestoreMenu()
{
      CMenu * mainMenu = GetMenu() ;
      CMenu newMenu ;
      CMenu * subMenu ;
      CString menuText ;


      if (  newMenu.LoadMenu( IDR_MFCSCRTYPE ) ) {
            // new menu loaded successfully,

            // so delete all of the main menu's current menus.
            while ( mainMenu->DeleteMenu( 0, MF_BYPOSITION ) != 0 ) {
            }

            // now copy the new menu to the main menu
            int position=0;
            while ( subMenu = newMenu.GetSubMenu( position ) ) {
                  newMenu.GetMenuString( position, menuText, MF_BYPOSITION ) ;
                  mainMenu->AppendMenu( MF_POPUP, (UINT) subMenu->GetSafeHmenu(), LPCTSTR( menuText ) );
                  subMenu->Detach();
                  position++;
            }
      }
      newMenu.Detach();
      DrawMenuBar();
}
0
 
LVL 1

Accepted Solution

by:
sudhirbrat earned 300 total points
ID: 1319162
You can do this in the following way

CMenu *pMenu = GetMenu();

CMenu *newMenu = new CMenu;
newMenu->LoadMenu(IDR_MYMENU);

if(SetMenu(newMenu))
{
   //Menu is set properly
  pMenu->DestroyMenu();
}
]
}
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 114
Expand data scrubbing tool 13 30
Expand to include initial dialog with two choices. 9 59
sum13 challenge 24 77
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now