wen_xiaolin
asked on
Switch DocTemplates in SDI application
I use several sets of DocTemplates in SDI application,I need to use one set of DocTemplates at one time,and I can switch to another set of DocTemplates without quit the
application.But if I add all the templates in function
CWinApp :InitInstance(),all the views are constructed after
ProcessShellCommand(),so there are too many views kept in
memory,but if I do not add the templates in InitInstance(),
I can not tell the SDI application to open new document.
application.But if I add all the templates in function
CWinApp :InitInstance(),all the views are constructed after
ProcessShellCommand(),so there are too many views kept in
memory,but if I do not add the templates in InitInstance(),
I can not tell the SDI application to open new document.
CMyDoc1* OpenDoc1()
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL );
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc 1);
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc 1);
return pDoc1;
}
should be
CMyDoc1* CMyApp::OpenDoc1()
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL );
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc 1);
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc 1);
return pDoc1;
}
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc
return pDoc1;
}
should be
CMyDoc1* CMyApp::OpenDoc1()
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc
return pDoc1;
}
remove the ASSEET_VALID and ASSERT_KINDOF lines in the functions. Doesn't seem to work in debug build. Rest works fine...
ASKER
Thanks for answer so quickly.
But the answer from the VCGuru does not show how the other
templates are constructed and used.In this application,the main purpose of switching templates is to use different view class and according resource,not the different document class.And the view may have splitter ,and must take
SDI into consideration.
But the answer from the VCGuru does not show how the other
templates are constructed and used.In this application,the main purpose of switching templates is to use different view class and according resource,not the different document class.And the view may have splitter ,and must take
SDI into consideration.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In the below piece of code from initinstance. all the templates are created. ONLY ONE is added using AddDocTemplate(). Later using my above answer(rejected) you can open any view you want. You can see that CMyView and CMyTest are two different views, same document same mainframe.
You can switch between views. Have a couple of menu items that call OpenDoc1(), OpenDoc2() etc.
pt1 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSssDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CSssView));
AddDocTemplate(pDocTemplat e);
pt2 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSssDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CMyView));
pt3 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSssDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CMyTest));
You can switch between views. Have a couple of menu items that call OpenDoc1(), OpenDoc2() etc.
pt1 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSssDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CSssView));
AddDocTemplate(pDocTemplat
pt2 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSssDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CMyView));
pt3 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSssDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CMyTest));
I dont test VCGURU's code. If he is right, you may recect my (later) answer and wait for VCGURU's new answer, and give him the points.
If you are not satisfied with his answer, test my solution.
If you are not satisfied with his answer, test my solution.
CMyApp::public CWinApp
{
CSingleDocTemplate *pt1;
CSingleDocTemplate *pt2;
CSingleDocTemplate *pt3;
CMyDoc1* OpenDoc1();
CMyDoc1* OpenDoc2();
CMyDoc1* OpenDoc3();
}
then,
in the initinstance just use AddDocTemplate(pt1)
Later in your app class
CMyDoc1* OpenDoc1()
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc
return pDoc1;
}
etc.
remember to delete the pointers not added using AddDocTemplate().
CMyApp::ExitInstance()
{
delete pt2;
delete pt3;
}