怎样变换菜单??

cqw4101206 2004-05-04 09:46:57
在MDI程序中,有两个菜单IDR_MAINFRAME和IDR_XXXXTYPE,在有无子窗口时分别显示,请问是怎样实现的?最好能附上一点代码?不甚感谢!!
...全文
85 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗黑帝国 2004-05-04
  • 打赏
  • 举报
回复
mar
UDX协议 2004-05-04
  • 打赏
  • 举报
回复
在没有子窗口时自动会显示IDR_MAINFRAME菜单。有子窗口就会显示IDR_XXXXTYPE

------------------我拿来一些mfc源码帮你----------
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_TESTMDTYPE,
RUNTIME_CLASS(CTestmdiDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CTestmdiView));
AddDocTemplate(pDocTemplate);
---------------end initinstance--------------
这里会
if (!CDocManager::bStaticInit)
LoadTemplate();
-------------insite loadtemplate
void CMultiDocTemplate::LoadTemplate()
{
CDocTemplate::LoadTemplate();

if (m_nIDResource != 0 && m_hMenuShared == NULL)
{
HINSTANCE hInst = AfxFindResourceHandle(
MAKEINTRESOURCE(m_nIDResource), RT_MENU);
m_hMenuShared = ::LoadMenu(hInst, MAKEINTRESOURCE(m_nIDResource));
m_hAccelTable =
::LoadAccelerators(hInst, MAKEINTRESOURCE(m_nIDResource));
}

#ifdef _DEBUG
// warnings about missing components (don't bother with accelerators)
if (m_hMenuShared == NULL)
TRACE1("Warning: no shared menu for document template #%d.\n",
m_nIDResource);
#endif //_DEBUG
}

---------------->这里其他模板载入menu,供以后,他们相弧切换--------
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
--------->
BOOL CFrameWnd::Create(LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
LPCTSTR lpszMenuName,
DWORD dwExStyle,
CCreateContext* pContext)
{
HMENU hMenu = NULL;
if (lpszMenuName != NULL)
{
// load in a menu that will get destroyed when window gets destroyed
HINSTANCE hInst = AfxFindResourceHandle(lpszMenuName, RT_MENU);
if ((hMenu = ::LoadMenu(hInst, lpszMenuName)) == NULL)
{
TRACE0("Warning: failed to load menu for CFrameWnd.\n");
PostNcDestroy(); // perhaps delete the C++ object
return FALSE;
}
}

m_strTitle = lpszWindowName; // save title for later

if (!CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
pParentWnd->GetSafeHwnd(), hMenu, (LPVOID)pContext))
{
TRACE0("Warning: failed to create CFrameWnd.\n");
if (hMenu != NULL)
DestroyMenu(hMenu);
return FALSE;
}

return TRUE;
}
这里主框架载入menu

BOOL CMDIFrameWnd::CreateClient(LPCREATESTRUCT lpCreateStruct,
CMenu* pWindowMenu)
{
ASSERT(m_hWnd != NULL);
ASSERT(m_hWndMDIClient == NULL);
DWORD dwStyle = WS_VISIBLE | WS_CHILD | WS_BORDER |
WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
MDIS_ALLCHILDSTYLES; // allow children to be created invisible
DWORD dwExStyle = 0;
// will be inset by the frame

if (afxData.bWin4)
{
// special styles for 3d effect on Win4
dwStyle &= ~WS_BORDER;
dwExStyle = WS_EX_CLIENTEDGE;
}

CLIENTCREATESTRUCT ccs;
ccs.hWindowMenu = pWindowMenu->GetSafeHmenu();
// set hWindowMenu for MFC V1 backward compatibility
// for MFC V2, window menu will be set in OnMDIActivate
ccs.idFirstChild = AFX_IDM_FIRST_MDICHILD;

if (lpCreateStruct->style & (WS_HSCROLL|WS_VSCROLL))
{
// parent MDIFrame's scroll styles move to the MDICLIENT
dwStyle |= (lpCreateStruct->style & (WS_HSCROLL|WS_VSCROLL));

// fast way to turn off the scrollbar bits (without a resize)
ModifyStyle(WS_HSCROLL|WS_VSCROLL, 0, SWP_NOREDRAW|SWP_FRAMECHANGED);
}

// Create MDICLIENT control with special IDC
if ((m_hWndMDIClient = ::CreateWindowEx(dwExStyle, _T("mdiclient"), NULL,
dwStyle, 0, 0, 0, 0, m_hWnd, (HMENU)AFX_IDW_PANE_FIRST,
AfxGetInstanceHandle(), (LPVOID)&ccs)) == NULL)
{
TRACE(_T("Warning: CMDIFrameWnd::OnCreateClient: failed to create MDICLIENT.")
_T(" GetLastError returns 0x%8.8X\n"), ::GetLastError());
return FALSE;
}
// Move it to the top of z-order
::BringWindowToTop(m_hWndMDIClient);

return TRUE;
}
这里,通过CreateClient传入IDR_XXXXTYPE对应的Menu.
当没有子窗口时,就切换到原来IDR_MAINFRAME的frame窗口。


zbl101 2004-05-04
  • 打赏
  • 举报
回复
mm

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧