Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Switch DocTemplates in SDI application

Posted on 1999-07-19
7
Medium Priority
?
238 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:wen_xiaolin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:VCGuru
ID: 1310470
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;
}
0
 
LVL 3

Expert Comment

by:VCGuru
ID: 1310471
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;
}

0
 
LVL 3

Expert Comment

by:VCGuru
ID: 1310472
remove the ASSEET_VALID and ASSERT_KINDOF lines in the functions. Doesn't seem to work in debug build. Rest works fine...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wen_xiaolin
ID: 1310473
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.
0
 
LVL 1

Accepted Solution

by:
MarkusLoibl earned 200 total points
ID: 1310474
* In your InitInstance, create or do not create a DocTemplate, as you like
* If you need another docTemplate, call from your App
  POSITION  pos;
  CDocTemplate *pTempl;
  pos = GetFirstDocTemplatePosition();
  while( pos != NULL )
  {
   pTempl = GetNextDocTemplate(pos);
   pTempl->CloseAllDocuments(FALSE);
  }
  delete m_pDocManager;
  m_pDocManager = NULL;
* Then after that you can add a new docTemplate with
  AddDocTemplate(), that code that everyone knows from the "classic" InitInstance()
0
 
LVL 3

Expert Comment

by:VCGuru
ID: 1310475
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));
0
 
LVL 1

Expert Comment

by:MarkusLoibl
ID: 1310476
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question