Solved

Reloading menus

Posted on 1998-07-14
7
537 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 134
substring method in java 1 136
building a service to accessed via telnet client, want colors ? 4 96
Increment column based of a FK 8 50
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 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