CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005
    Posts
    104

    Child frame menubar

    Hi,
    When you define a document template in an MDI app, you specify the menu resource that gets used for the main frame.

    I have a bunch (~ 5) different document types that my application can load. For each of these, there is a unique set of commands. There is also a global set of commands that applies no matter which document type is currently active.

    Maintaining 5 different menus, that contain the unique commands for that document type, *plus* all the global commands, is a maintenance nightmare. Every time I add a global command, I have to add it to all 5 menus.

    Is there some way to make this easier? Like a way to merge document-specific menus with a global menu?

    thanks!

  2. #2
    Join Date
    Jul 2005
    Location
    Russian Federation. Moscow
    Posts
    152

    Lightbulb Re: Child frame menubar

    hi, i've written and tested this for you (see attachment)...
    is that what you want?

    Code:
    BOOL CChildFrame::SwitchDocMenu(UINT nID)
    {
    	if (nID <= 0)
    		return FALSE;
    	
    	BOOL bResult = FALSE;
    
    	CMenu mnu;
    	CString strCaption;
    
    	CWnd *pMainFrame = AfxGetMainWnd();
    	CMenu *pMainMenu = pMainFrame->GetMenu();
    	ASSERT(::IsMenu(pMainMenu->m_hMenu));
    	
    	m_menuDoc.DestroyMenu();
    	{
    		if (m_menuDoc.LoadMenu(IDR_MAINFRAME))
    		{
    			pMainFrame->SetMenu(0);
    			pMainMenu->DestroyMenu();
    						
    			if (mnu.LoadMenu(nID))
    			{
    		 	if (mnu.GetMenuString(0, strCaption, MF_BYPOSITION) > 0)
    				{
    		 		if (m_menuDoc.InsertMenu(1, MF_BYPOSITION|MF_POPUP,
    		 		 (UINT_PTR)(HMENU)mnu, strCaption))
    					{
    						mnu.Detach();
    		 			bResult = TRUE;
    		 		 pMainFrame->SetMenu(&m_menuDoc);
    		 		 pMainFrame->DrawMenuBar();
    					}
    				}
    			}
    		}
    	}
    	return bResult;
    }
    
    // CChildFrame message handlers
    
    void CChildFrame::OnUpdateFrameMenu(BOOL bActive, CWnd* pActiveWnd, HMENU hMenuAlt)
    {
    	//CMDIChildWnd::OnUpdateFrameMenu(bActive, pActiveWnd, hMenuAlt);
    
    	UINT nMenuID = 0;
    	CDocument *pActiveDoc;
    	LPCSTR pszClass;
    
    	if (bActive)
    	{
    		pActiveDoc = GetActiveView()->GetDocument();
    		ASSERT(pActiveDoc != NULL);
    
    		pszClass = pActiveDoc->GetRuntimeClass()->m_lpszClassName;
    		
    
    		if (_tcsicmp(pszClass, _T("CDoc1")) == 0)
    		{
    			nMenuID = IDR_DOC1MENU;
    		}
    		else
    			if (_tcsicmp(pszClass, _T("CDoc2")) == 0)
    			{
    				nMenuID = IDR_DOC2MENU;
    			}
    			else
    				if (_tcsicmp(pszClass, _T("CDoc3")) == 0)
    				{
    					nMenuID = IDR_DOC3MENU;
    				}
    		SwitchDocMenu(nMenuID);
    	}
    }
    Attached Files Attached Files
    Best regards, Alex Dronov
    Let the poster of the answers know when they helped you: Click "Rate this post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured