Microsoft KB Archive/100993

From BetaArchive Wiki
Knowledge Base

How to Create Additional Views with CreateNewFrame() Function

Article ID: 100993

Article Last Modified on 1/29/2007


  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.51
    • Microsoft Visual C++ 1.52 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 4.1 Subscription
    • Microsoft Visual C++ 5.0 Standard Edition
    • Microsoft Visual C++ 6.0 Service Pack 5
    • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition

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.


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.


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 =

      if (pDoc == NULL)
             return NULL;
//The line below is not required.
      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.

Additional query words: change view

Keywords: kbhowto kbuidesign kbdocview kbmdi KB100993