Microsoft KB Archive/113257

= How to create a CMultiDocTemplate based window in Visual C++ =

Article ID: 113257

Article Last Modified on 11/21/2006

-

APPLIES TO

 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</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q113257

<div class="summary_section">

SUMMARY
It is sometimes desirable to create a CMultiDocTemplate based window (in other words, a CFrame/CDocument/CView combination) without using the mechanism provided by CWinApp::OnFileNew.

For example, if the program has multiple document templates, CWinApp::OnFileNew will prompt the user with a dialog box asking which type of document to open. The programmer may already know which type of CMultiDocTemplate to use, and therefore may not want to prompt the user because it would be inappropriate in the given context of the application.

<div class="moreinformation_section">

MORE INFORMATION
Assuming the application was originally created with AppWizard, the undocumented CMultiDocTemplate::OpenDocumentFile function can be used to create a new CMultiDocTemplate based window. There are several steps involved:

<ol> <li> Add a CMultiDocTemplate pointer to your CWinApp derived class: class CMyApp : public CWinApp {       ...

public: CMultiDocTemplate* m_pDocTemplate;

...

} NOTE: If you plan to use multiple document types, you must create a CMultiDocTemplate pointer member variable for each document type. </li> <li> In the call to CWinApp::InitInstance, remove the creation of the CMultiDocTemplate from the call to AddDocTemplate. Set the pointer to point to the new CMultiDocTemplate. Use the pointer to call AddDocTemplate: BOOL CMyApp::InitInstance {       ...

m_pDocTemplate = new CMultiDocTemplate(IDR_TEXTTYPE,                                     RUNTIME_CLASS(CMyDoc),                                      RUNTIME_CLASS(CMDIChildWnd),                                      RUNTIME_CLASS(CMyView));

AddDocTemplate(m_pDocTemplate);

...

}                       </li> <li> Use the pointer to call CMultiDocTemplate::OpenDocumentFile with a NULL parameter to create the new window. For this example, assume there is a button in a CView window. In the BN_CLICKED handler for the button, we want to create a window based on m_pDocTemplate: void CMyView::OnNewWindowButtonClicked {         CMyApp* pApp = (CMyApp*)AfxGetApp; pApp->m_pDocTemplate->OpenDocumentFile(NULL); } This same technique could be used to create a CSingleDocTemplate based window in a Single Document Interface (SDI) application. However, it is not necessary. Because there is only one document template for the application, calling OnFileNew will create the new window without prompting the user for the type of document. </li></ol>

Keywords: kbhowto kbinfo kbdocview KB113257

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.