Using 2 views for a single document

In order to use more than 1 views for a singel document, one should add another document template. If adding this snippet code to InitInstance() directly, one would encounter a dialog to select different documents although you only have one type of document. Commonly, there are 2 methods to avoid this dialog: first is to declaim different resource ID for the second document template, and blabla…; second is to add the second doucment template to other place other than the InitInstance(). See:
BOOL CPhotograph2App::OnDstTemplate()
 pDstTemplate = new CMultiDocTemplate(IDR_Photograph2TYPE,
  RUNTIME_CLASS(CChildFrame), // custom MDI child frame
 if (!pDstTemplate)
    return FALSE;
 return TRUE;
Then you can create the view when necessary. Remember to InitialUpdateFrame after CreateNewFrame.
// CMainFrame message handlers
BOOL CMainFrame::OnDstWindow()
   CMDIChildWnd* pActiveChild = MDIGetActive();
   CDocument* pDocument;
   if (pActiveChild == NULL ||
      (pDocument = pActiveChild->GetActiveDocument()) == NULL) {
      TRACE0("Warning: No active document for WindowNew command.\n");
      return FALSE;     // command failed
 CPhotograph2App*  pApp = (CPhotograph2App*)AfxGetApp();  
 ASSERT_POINTER(pApp, CPhotograph2App);
   //CDocTemplate* pTemplate = pDocument->GetDocTemplate();
   CMultiDocTemplate* pDstTemplate = pApp->GetDstTemplate();     
 CFrameWnd* pFrame = pDstTemplate->CreateNewFrame(pDocument, pActiveChild);
   if (pFrame == NULL)   {  
    TRACE0("Warning: failed to create new frame.\n");   
    return FALSE;     // command failed
   pDstTemplate->InitialUpdateFrame(pFrame, pDocument);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s