CMDIFrameWnd::MDISetMenu

Replaces the menu of an MDI frame window, the Window pop-up menu, or both.

CMenu* MDISetMenu( 
   CMenu* pFrameMenu, 
   CMenu* pWindowMenu  
);

Parameters

  • pFrameMenu
    Specifies the menu of the new frame-window menu. If NULL, the menu is not changed.

  • pWindowMenu
    Specifies the menu of the new Window pop-up menu. If NULL, the menu is not changed.

Return Value

A pointer to the frame-window menu replaced by this message. The pointer may be temporary and should not be stored for later use.

Remarks

After calling MDISetMenu, an application must call the DrawMenuBar member function of CWnd to update the menu bar.

If this call replaces the Window pop-up menu, MDI child-window menu items are removed from the previous Window menu and added to the new Window pop-up menu.

If an MDI child window is maximized and this call replaces the MDI frame-window menu, the Control menu and restore controls are removed from the previous frame-window menu and added to the new menu.

Do not call this member function if you use the framework to manage your MDI child windows.

Example

// CMdiView::OnReplaceMenu() is a menu command handler for CMdiView 
// class, which in turn is a CView-derived class. It loads a new 
// menu resource and replaces the main application window's menu 
// bar with this new menu. 
void CMdiView::OnReplaceMenu() 
{
   // Load a new menu resource named IDR_SHORT_MENU. m_hDefaultMenu is  
   // a member variable of CMdiDoc class (a CDocument-derived class).  
   // Its type is HMENU.
   CMdiDoc* pdoc = (CMdiDoc*)GetDocument();
   pdoc->m_hDefaultMenu = 
      ::LoadMenu(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_SHORT_MENU));
   if (pdoc->m_hDefaultMenu == NULL)
      return;

   // Get the parent window of this view window. The parent window is 
   // a CMDIChildWnd-derived class. We can then obtain the MDI parent  
   // frame window using the CMDIChildWnd*. Then, replace the current  
   // menu bar with the new loaded menu resource.
   CMDIFrameWnd* frame = ((CMDIChildWnd*)GetParent())->GetMDIFrame();
   frame->MDISetMenu(CMenu::FromHandle(pdoc->m_hDefaultMenu), NULL);
   frame->DrawMenuBar();
}
// GetDefaultMenu() is an undocumented virtual function for  
// CDocument class. It allows the document to determine which  
// menu to display. m_hDefaultMenu is of type HMENU. Its value 
// is initialized to NULL either in the constructor or  
// CDocument::OnNewDocument(). And the menu resource is destroyed 
// in the destructor to avoid having too many menus loaded at once.
HMENU CMdiDoc::GetDefaultMenu()
{
   if (m_hDefaultMenu)
      return m_hDefaultMenu;

   return COleServerDoc::GetDefaultMenu();
}

// Initialize member variable(s) in the constructor. CMdiDoc is 
// a CDocument-derived class.
CMdiDoc::CMdiDoc()
{
   // Use OLE compound files
   EnableCompoundFile();

   m_hDefaultMenu = NULL; // initialize to NULL
}

// Destroy menu resource in CMdiDoc's destructor. CMdiDoc is 
// a CDocument-derived class.
CMdiDoc::~CMdiDoc()
{
   if (m_hDefaultMenu)
      ::DestroyMenu(m_hDefaultMenu);
}

Requirements

Header: afxwin.h

See Also

Reference

CMDIFrameWnd Class

Hierarchy Chart

CWnd::DrawMenuBar

WM_MDISETMENU