MFC 多文档动态菜单设置

菜单的响应函数事先已经设置好了,这个动态响应菜单暂时还没搞明白

 

// 注册应用程序的文档模板。  文档模板
	// 将用作文档、框架窗口和视图之间的连接
	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(IDR_SmartReadingTYPE,
		RUNTIME_CLASS(CSmartReadingDoc),
		RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架
		RUNTIME_CLASS(CSmartReadingView));
	if (!pDocTemplate)
		return FALSE;
	m_pDocTemplate = pDocTemplate;
	AddDocTemplate(pDocTemplate);

	//关联新的视图
	m_pNewDocTemplate = new CMultiDocTemplate(IDR_SmartReadingTYPE,
		RUNTIME_CLASS(CSmartReadingDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CBaiduPropertiesView));
	AddDocTemplate(m_pNewDocTemplate);
	// 创建主 MDI 框架窗口
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
	{
		delete pMainFrame;
		return FALSE;
	}
	m_pMainWnd = pMainFrame;

	//设置默认显示的菜单
	m_pNewDocTemplate->m_hMenuShared = pMainFrame->GetSharedMenu();
	m_pDocTemplate->m_hMenuShared = m_pNewDocTemplate->m_hMenuShared;

//设置默认菜单(文档打开前显示的主菜单)
HMENU CMainFrame::SetDefaultMenu()
{
	m_menu.LoadMenu(IDR_MAINFRAME);
	m_hMenuDefault = m_menu.Detach();
	OnUpdateFrameMenu(m_hMenuDefault); 
	return m_hMenuDefault;
}

//获取编辑菜单(文档打开后显示的主菜单)
HMENU CMainFrame::GetSharedMenu()
{
	CMenu menu;
	m_baiduMenu.CreateMenu();
	menu.CreateMenu();
	CString strItem;
	strItem.LoadString(IDS_BAIDU);

	UINT_OCRINFO_MAP& ocrInfoMap = CBaiDuOcr::GetOcrInfo();
	UINT_OCRINFO_MAP::iterator it = ocrInfoMap.begin();
	for (; it != ocrInfoMap.end(); it++)
	{
		menu.AppendMenu(MF_STRING, it->second._uMenuId, it->second._strMenuName);
	}
	//添加子菜单,注意第一个参数选项,这个地方折腾了快一个小时第一个参数设置错了,搞的二级子菜单一直没添加成功
	m_baiduMenu.AppendMenu(MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)menu.m_hMenu, strItem);
	strItem.LoadString(IDS_ALIGENIE);
	m_baiduMenu.AppendMenu(MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)IDS_ALIGENIE, strItem);
	strItem.LoadString(IDS_TENCENT);
	m_baiduMenu.AppendMenu(MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT_PTR)IDS_TENCENT, strItem);

	menu.Detach();

	m_default.LoadMenu(IDR_SmartReadingTYPE);
	strItem.LoadString(IDS_AI_OPEN_PLATFORM);
	//m_default.AppendMenu(MF_POPUP, (UINT)m_baiduMenu.m_hMenu, strItem);//增加子菜单 
	m_default.InsertMenu(m_default.GetMenuItemCount() - 2, MF_POPUP | MF_BYPOSITION, (UINT)m_baiduMenu.m_hMenu, strItem);

	return 	m_default.Detach();
}

 

你可能感兴趣的:(MFC学习心得,C++)