Solved

Reloading menus

Posted on 1998-07-14
7
519 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

21 Experts available now in Live!

Get 1:1 Help Now