We help IT Professionals succeed at work.

Changing Menu dynamically

FMayis
FMayis asked
on
Medium Priority
358 Views
Last Modified: 2013-11-20
Hi,
I have an application that runs in 2 modes:
  - designer
  - runtime

for each mode i have a menu define in the resources.
I want to display the correct menu for each mode.

The probleme is that the menu are not correctly refresh when i switch from a mode to another. The subitems are correct but the titles of the menu stay the same betwween 2 switch.
for instance:

in designer:       in runtime:
    AAA                BBB
      Cmd1               Cmd3
      Cmd2               Cmd4

and when i switch from designer to runtime i have:
    AAA
      Cmd3
      Cmd4

The code i have made to switch menu is:
void CMainFrame::SetMenuFromMode(t_AppMode mode){
     CMenu* pMenu = (CMenu*)GetMenu();
    if (pMenu)
          pMenu->DestroyMenu();
     HMENU hMenu = NewMenu(mode);
     pMenu = CMenu::FromHandle( hMenu );  
    SetMenu(pMenu);
     DrawMenuBar();
    m_hMenuDefault = hMenu;
}

HMENU CMainFrame::NewMenu(t_AppMode Mode)
{
     if ( Mode == Mode_designer )
          m_menu.LoadMenu(IDR_MAINFRAME);
     else
          m_menu.LoadMenu(IDR_MAINFRAMERUNTIME);
     return(m_menu.Detach());
}


Can you help please!
Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Author of the Year 2009
Commented:
It looks like you are doing too much work (although the technique appears viable).  This is from the online help for CMenu::LoadMenu.  It does excatly what you want to do

void CMainFrame::OnReplaceMenu()
{
   // Load the new menu.
   m_NewMenu.LoadMenu(IDR_SHORT_MENU);
   ASSERT(m_NewMenu);

   // Remove and destroy the old menu
   SetMenu(NULL);
   ::DestroyMenu(m_hMenuDefault);

   // Add the new menu
   SetMenu(&m_NewMenu);

   // Assign default menu
   m_hMenuDefault = m_NewMenu.GetSafeHmenu();  // or m_NewMenu.m_hMenu;
}

The only other problem could be that your menue are defined incorrectly.  No way to tell that unless you post that part of the RC file.

-- Dan

Explore More ContentExplore courses, solutions, and other research materials related to this topic.