首先理解文档、视图、模板之间的关系,理解文档/视图框架。
1.添加CDocument类、CView(或者CFromView)类、CMDIChildWndEx类。
2.修改自定义的子框架类中的宏,头文件和源文件都为 DECLARE_DYNCREATE 。
3.添加全局变量CMultiDocTemplate 模板类指针,在App源文件中添加 文档、视图、子框架关联。
例如:
pUserFormDocTemplate = new CMultiDocTemplate( IDR_USERVIEW_TMPL, RUNTIME_CLASS(CUserDoc), RUNTIME_CLASS(CUserFrm), RUNTIME_CLASS(CUserView)); AddDocTemplate(pUserFormDocTemplate);
4.添加按钮响应,在响应函数中添加如下代码:
5.在子框架类中添加Onclose函数,当窗口关闭时,恢复创建标记flog,这样就可以确保始终只有一个同样的子窗口创建。if (theApp.flog[0])
{//创建用户管理子窗口
if (theApp.pUserFormDocTemplate)
{
theApp.pUserFormDocTemplate->OpenDocumentFile(NULL);
theApp.flog[0] = 0; //创建成功标记
POSITION pos = theApp.pUserFormDocTemplate->GetFirstDocPosition();
CDocument *pDoc = theApp.pUserFormDocTemplate->GetNextDoc(pos);
pDoc->SetTitle("用户管理");
}
}