Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Way to Switch from One Menu Set to Another

0.00/5 (No votes)
5 Jun 2002 1  
Replace your current menus with another set of menus with this function

Basic Purpose

I spent a couple hours today looking for a simple way to change from one set of menus (file, edit, etc.) to another menu set. I came across this code in MSDN, but I didn't see anything like it here on CodeProject, so this is my first article post. I hope someone might find it useful.

How to Use

private:
  void ReplaceMenu(UINT n_IDResource);
void CMainFrame::ReplaceMenu(UINT n_IDResource)
{
   CMenu NewMenu; // create the new CMenu variable
   NewMenu.LoadMenu(n_IDResource); 
      // Load the menu from the resource passed
   ASSERT(NewMenu);

   // Remove and destroy the old menu
   SetMenu(NULL);
   ::DestroyMenu(m_hMenuDefault); 
      // m_hMenuDefault is the menu member variable of CFrameWnd

   // Set the menu to the new menu we created
   SetMenu(&NewMenu);

   // Set the default menu handler to the handle of our new menu
   m_hMenuDefault = NewMenu.GetSafeHmenu();
}
ReplaceMenu(IDR_OTHERMENU);
  1. Create multiple Menu resources in the resource view. The two I will use are IDR_MAINFRAME and IDR_OTHERMENU.
  2. Declare the function in your MainFrame.h (or some other CFrameWnd derived class).
  3. Add the following to your MainFrame.cpp.
  4. Use the function wherever you need just by calling the function passing it the menu resource to use.

Conclusion

You still implement your message map the same way. Although I would suggest prefixing your menu-set's menu functions differently (example: OnView1TestMe(), OnView2TestMe()) so you can easily tell which functions are for which menu set. Please keep in mind this is my first article, feedback will be read but just take it easy on me. I've done a lot of work with split views and replacing views and printing different views and such in split views. I hope to write another article on that stuff in a few months.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here