桌面开发
现在阅读
A Simple way to switch from one menu set to another
0

A Simple way to switch from one menu set to another

由 ultracpy2018年1月27日

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

  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. private:
      void ReplaceMenu(UINT n_IDResource);
  4. Add the following to your MainFrame.cpp
  5. 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();
    }
  6. Use the function wherever you need just by calling the function passing
    it the menu resource to use
  7. ReplaceMenu(IDR_OTHERMENU);

Conclusion

You still implement your message map the same way. Although I would suggest
prefixing your menu-set’s menu functions differently (ex. OnView1TestMe(),
OnView2TestMe()) so you can easily tell which functions are for which menu set.
Please keep in mind this is my first article, feed back 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.

-dz [www.dzgraphics.com]

出处:https://www.codeproject.com/Articles/2378/A-Simple-way-to-switch-from-one-menu-set-to-anothe

关于作者
ultracpy
评论

你必须 登录 提交评论