How to Create Additional Views with CreateNewFrame() Function (100993)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0
    • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article was previously published under Q100993
Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

SUMMARY

The CDocTemplate::CreateNewFrame() function creates additional views of a document in a multiple document interface (MDI) application written using the Microsoft Foundation Class Library. The prototype of the CreateNewFrame() function is as follows:
      CFrameWnd * CDocTemplate::CreateNewFrame(CDocument *, CFrameWnd *)
				
To call this function, specify a pointer to a CDocument object (the document for which the function will create a view) and a pointer to a frame window that has properties to duplicate. Typically, the second parameter of the function is NULL.

When an application calls CreateNewFrame(), the function creates a new frame window and a view in the frame window. The frame window type and view type depend on the document template (CDocTemplate) associated with the document specified in the CreateNewFrame() call.

MORE INFORMATION

To better understand how to use CreateNewFrame(), it might be useful to review two examples.

The first example is the WINMDI.CPP file in the Microsoft Foundation Class Library source code. WINMDI.CPP defines the function CMIDFrameWnd::OnWindowNew() that calls CreateNewFrame() to add an additional frame and view for a specified document. The application calls OnWindowNew() each time the user selects New from the MDI application's Windows menu.

The OnWindowNew() function contains two significant lines of code, as follows:
      CFrameWnd * pFrame =
            pTemplate->CreateNewFrame(pDocument, pActiveChild);
      pTemplate->InitialUpdateFrame(pFrame, pDocument);
				
This code creates and displays the new frame window and document view.

The other example is the DOCMULTI.CPP file, also in the Microsoft Foundation Class Library source code. The CMultiDocTemplate::OpenDocumentFile() function includes the following code:
      CFrameWnd * pFrame = CreateNewFrame(pDocument, NULL);
				
NOTE: The second parameter is NULL because the design of the OpenDocumentFile() function assumes that the programmer is not interested in duplicating any of the other frames that contain views of this document.

The CHKBOOK sample also demonstrates creating additional frames and views for documents. In CHKBOOK.CPP, the CChkBookApp::OpenDocumentFile() function includes the following code:
      CChkBookDoc * pDoc =
            (CChkBookDoc*)CWinApp::OpenDocumentFile(lpszFileName);

      if (pDoc == NULL)
             return NULL;
<?xm-insertion_mark_start author="sastryg" time="20030506T120406+05-30"?>The below line is not required.<?xm-insertion_mark_end?>
      CMDIChildWnd * pframe =
            ((CMDIFrameWnd *)AfxGetApp()->m_pMainWnd)->MDIGetActive();

      CFrameWnd * pNewFrame =
            m_pCheckViewTemplate->CreateNewFrame(pDoc, NULL);
      if (pNewFrame == NULL)
         return pDoc;
      m_pCheckViewTemplate->InitialUpdateFrame(pNewFrame, pDoc);
				
Here are two points to consider when you use the CreateNewFrame():

  • The source code for CDocTemplate::CreateNewFrame() is in DOCTEMPL.CPP. It includes the following code:
          if (!pFrame->LoadFrame(m_nIDResource,
               WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, // default frame styles
               NULL, &context)
    						
    Because this code creates the frame window with a NULL parent window, the Microsoft Foundation Class Library uses the application's main window as the parent window.
  • CreateNewFrame() creates both a frame and a view; not only a view.
If, for some reason, CreateNewFrame() does not quite address your situation, the source code for CreateNewFrame() is quite useful to demonstrate the steps required to create frames and views.

Modification Type:MinorLast Reviewed:2/8/2005
Keywords:kbDocView kbhowto kbMDI KbUIDesign KB100993 kbAudDeveloper