【原先的做法】
MFC有很多的办法能够在主框架中产生子框架、视和文档。当我们在产生一个工程的时候,
常见的办法是在一个配置对话框中配置好对应的属性,然后接下去就是打开一个文档记录
数据,打开该文档的一个视,和相应的菜单、工具栏。而且复杂点的工程中有几个不同的
文档类,最起码的也是有一个文档对应几种视的情况,所以在编写MFC的时候,动态打开
子框架、视和文档的是种常用的技术。
当我们用AppWizard这种垃圾生成一个App的框架的时候,通常会在App的InitInstance()里
面new一个DocTemplate,然后用AddDocTemplate()的方式初始化一个模板,然后在
ProcessShellCommand()的方式执行子框架的打开。
我采用的办法是在ParseCommandLine()后用cmdInfo.m_nShellCommand = cmdInfo.FileNothing
禁止打开新的文件,而是在这里打开个配置对话框。接着就是在SetDlg.DoModal()后用如下的
代码打开文档,视和子框架:
// Creat new ChileFrame
theApp.m_pCurrentFrame = new CChildFrame;
// Creat new view and new doc
theApp.m_pCurrentView = new CEcmcView(VIEW_WAVE);
theApp.AddView(theApp.m_pCurrentView);
theApp.m_pCurrentDoc = new CEcmcDoc(VIEW_WAVE);
theApp.AddDoc(theApp.m_pCurrentDoc);
// Attach them
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CEcmcView);
context.m_pCurrentDoc = theApp.m_pCurrentDoc;
theApp.m_pCurrentFrame->LoadFrame(
IDR_EcmprojTYPE,
WS_OVERLAPPEDWINDOW,
this,
&context);
UINT ID_WAVEVIEW = AFX_IDS_PREVIEW_CLOSE +1;
CRect rect;
theApp.m_pCurrentView->Create(
NULL,
"New Wave Project",
WS_CHILD,
rect,
this,
ID_WAVEVIEW,
&context);
theApp.m_pCurrentFrame->ShowWindow(SW_SHOW);
theApp.m_pCurrentFrame->InitialUpdateFrame(NULL,TRUE);
这种办法可以在任何的地方采用,只要预备好菜单资源和定义好视类、文档类就好了
大家有什么好的方法,也说说吧
按照上面的代码,菜单并没有更新成新的菜单,所以要添加下面代码:
CMenu currentMenu;
currentMenu.LoadMenu(你要添加菜单的ID);
SetMenu(¤tMenu);
【修正】
上次我写的这个主题的文章有些问题,在调试的时候,发现在MainFrame中改变产生的视的一个状态
的时候,在视的成员函数处理的时候,并没有体现出来。具体来讲,我在CEcmcView::OnDraw()中根
据View中的一个成员变量来画不同的图的,我在MainFrame::OnProjNew()按钮响应函数结束的时候改
变view的此成员变量,但是在CEcmcView::OnDraw()中并没有反应出来,所以后来参照了MFC中CWinApp
::OnFileNew()的写方法,实现了该类功能,不禁感慨,MFC毕竟写的很好。
// Creat new doc, view, ChildFrame and attach them
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_EcmprojTYPE,
RUNTIME_CLASS(CEcmcDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CEcmcWaveView));
theApp.AddDocTemplate(pDocTemplate);
CEcmcDoc* pDoc = (CEcmcDoc*) pDocTemplate->OpenDocumentFile(NULL);
theApp.m_pCurrentDoc = pDoc;
POSITION posView = pDoc->GetFirstViewPosition();
CEcmcWaveView* pView = (CEcmcWaveView*)pDoc->GetNextView(posView);
theApp.m_pCurrentView = pView;
theApp.m_pCurrentFrame = (CChildFrame*)pView->GetParentFrame();
// link the doc and view into app's list
theApp.AddDoc(theApp.m_pCurrentDoc);
theApp.AddView(theApp.m_pCurrentView);
TRACE("-->Mainfrm.OnProjNew:the status of current view is %d/n",theApp.m_pCurrentView->m_currentStatus);
theApp.m_pCurrentFrame->ShowWindow(SW_SHOW);
theApp.m_pCurrentFrame->InitialUpdateFrame(NULL,TRUE);