Click here to Skip to main content
15,918,976 members
Articles / Desktop Programming / MFC

A Simple Way to Switch from One Menu Set to Another

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
5 Jun 20021 min read 95.7K   21   15
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

C++
private:
  void ReplaceMenu(UINT n_IDResource);
C++
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();
}
C++
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.


Written By
Web Developer
United States United States
hrm.. been into computers since my tandy 8088.. im 28 years old..

Comments and Discussions

 
GeneralBetter way Pin
ericlamiot29-Aug-06 2:19
ericlamiot29-Aug-06 2:19 
GeneralRe: Better way Pin
Yogesh Dhakad8-May-07 23:10
Yogesh Dhakad8-May-07 23:10 
GeneralRe: Better way Pin
Yogesh Dhakad8-May-07 23:37
Yogesh Dhakad8-May-07 23:37 
Generalstore CMenu as a member of CFrameWnd derived class Pin
piide18-Jul-05 2:30
piide18-Jul-05 2:30 
GeneralAccess violation when called outside of the mainfrm Pin
dejavusoft23-May-05 16:12
dejavusoft23-May-05 16:12 
GeneralMaximized document Pin
simonpp18-Sep-03 15:24
simonpp18-Sep-03 15:24 
GeneralRe: Maximized document Pin
rmacapobre3-Feb-19 16:05
rmacapobre3-Feb-19 16:05 
GeneralHelp me, please Pin
shva21-Jul-03 21:12
shva21-Jul-03 21:12 
QuestionHow to get the menu of childframe? Pin
haiyingxu11-Mar-03 19:24
haiyingxu11-Mar-03 19:24 
Generalpossible bug Pin
Warren Stevens4-Jun-02 8:34
Warren Stevens4-Jun-02 8:34 
GeneralRe: possible bug Pin
dazinith4-Jun-02 9:19
dazinith4-Jun-02 9:19 
GeneralRe: possible bug Pin
Marc Clifton7-Jun-02 1:21
mvaMarc Clifton7-Jun-02 1:21 
GeneralRe: possible bug Pin
msheasby9-Jan-16 15:09
msheasby9-Jan-16 15:09 
Generalcrud.. i think this should have gone in another section Pin
dazinith4-Jun-02 5:06
dazinith4-Jun-02 5:06 
GeneralRe: crud.. i think this should have gone in another section Pin
Chris Maunder5-Jun-02 21:49
cofounderChris Maunder5-Jun-02 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.