The Ex16c Example: An MTI Application
previous section. To create this example, in the MFC Application Wizard select Multiple Top-Level Documents on the Application Type page and deselect Printing And Print Preview on the Advanced Features page. On the Generated Classes page, change the view's base class to CFormView.Ex16c uses the same document and view class code and the same resources (except the resource name). The application code and main frame class code are different, however. You can examine all the new code in the Ex16c application on the companion CD. A list of files and classes in the Ex16c example are shown in Table 16-4.
Header File | Source Code File | Class | Description |
---|---|---|---|
![]() | ![]() | CEx16cApp | Application class (from the MFC Application Wizard) |
CAboutDlg | About dialog box | ||
![]() | ![]() | CMainFrame | MTI main frame |
C ![]() | C ![]() | CEx16cDoc | Student document (borrowed from Ex16a) |
C ![]() | ![]() | CEx16cView | Student form view (borrowed from Ex16a) |
![]() | ![]() | CStudent | Student record (from Ex16a) |
![]() | ![]() | Precompiled headers (with afxtempl.h included) |
Unlike the MDI and SDI applications, the MTI application includes a New Frame command on the File menu. This command tells the application to open a new top-level window. The following listing illustrates handling the New Frame command:
void CEx16cApp::OnFileNewFrame()
{
ASSERT(m_pDocTemplate != NULL);
CDocument* pDoc = NULL;
CFrameWnd* pFrame = NULL;
// Create a new instance of the document referenced
// by the m_pDocTemplate member.
pDoc = m_pDocTemplate->CreateNewDocument();
if (pDoc != NULL)
{
// If creation worked, use create a new frame for
// that document.
pFrame = m_pDocTemplate->CreateNewFrame(pDoc, NULL);
if (pFrame != NULL)
{
// Set the title, and initialize the document.
// If document initialization fails, clean-up
// the frame window and document.
m_pDocTemplate->SetDefaultTitle(pDoc);
if (!pDoc->OnNewDocument())
{
pFrame->DestroyWindow();
pFrame = NULL;
}
else
{
// Otherwise, update the frame
m_pDocTemplate->InitialUpdateFrame(pFrame, pDoc, TRUE);
}
}
}
// If we failed, clean up the document and show a
// message to the user.
if (pFrame == NULL || pDoc == NULL)
{
delete pDoc;
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
}
}
MTI applications use the CMultiDocTemplate class to manage the document, the frame, and the view. Notice that OnFileNewFrame creates a new document and then a new top-level frame window instead of depending on the framework to create the document, frame, and view classes. Otherwise, MTI applications manage their documents and views in the same way that SDI and MDI applications do.
Testing the Ex16c Application
To test the Ex16c application, run the application and choose New Frame from the File menu. Notice that a new frame opens up near the existing frame. The new top-level frame includes a new instance of the document, but the document is associated with the new frame (rather than with a new MDI child frame, as in Ex16b).