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;
NewMenu.LoadMenu(n_IDResource);
ASSERT(NewMenu);
SetMenu(NULL);
::DestroyMenu(m_hMenuDefault);
SetMenu(&NewMenu);
m_hMenuDefault = NewMenu.GetSafeHmenu();
}
ReplaceMenu(IDR_OTHERMENU);
- Create multiple Menu resources in the resource view. The two I will use are
IDR_MAINFRAME
and IDR_OTHERMENU
. - Declare the function in your MainFrame.h (or some other
CFrameWnd
derived class). - Add the following to your MainFrame.cpp.
- 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.