Link to home
Start Free TrialLog in
Avatar of wen_xiaolin
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.
Avatar of VCGuru
VCGuru

You need not add the templates before the processshellcommand. Just add one template that's enough. Make the template variables as a member function of your app class.
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*>(pDoc1);
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc1);
return pDoc1;
}
etc.

remember to delete the pointers not added using AddDocTemplate().

CMyApp::ExitInstance()
{
delete pt2;
delete pt3;
}
CMyDoc1* OpenDoc1()
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL);
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc1);
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc1);
return pDoc1;
}
should be
CMyDoc1* CMyApp::OpenDoc1()
{
CDocument* pDoc = pt1->OpenDocumentFile(NULL);
CMyDoc1* pDoc1 = static_cast<CMyDoc1*>(pDoc1);
ASSERT_VALID(pDoc1);
ASSERT_KINDOF(CMyDoc1,pDoc1);
return pDoc1;
}

remove the ASSEET_VALID and ASSERT_KINDOF lines in the functions. Doesn't seem to work in debug build. Rest works fine...
Avatar of wen_xiaolin

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.
ASKER CERTIFIED SOLUTION
Avatar of MarkusLoibl
MarkusLoibl
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(pDocTemplate);

      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.