Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reloading menus

Posted on 1998-07-14
7
Medium Priority
?
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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