How to use a personnal New Document Method

This is a simple way how to use a personnal dialog which appears when your application uses more than one document template and you click the 'new' button, or during the start of the appliacation. With this You can open any document directly or by a dialog of your choice.

First time : Create variables for all the DocTemplates in the initialization of the application. The variables are used to store the instance of the pDocTemplate and used after during the FileNew process.

CWinApp header file : Add the declaration of the variables

class CCTestNoDlgApp : public CWinApp
{
     public:
          CCTestNoDlgApp();
        
        // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CCTestNoDlgApp)
     public:
        virtual BOOL InitInstance();
        //}}AFX_VIRTUAL

        // Implementation
        //{{AFX_MSG(CCTestNoDlgApp)
        afx_msg void OnAppAbout();
        afx_msg void OnFileNew();
        //}}AFX_MSG

        DECLARE_MESSAGE_MAP()

     protected:
        CMultiDocTemplate* pDocTemplateFirst;
        CMultiDocTemplate* pDocTemplateSecond;
};

In the CWinApp source file : Initialize all the variables and disable the new document dialog by setting the CcommandLineInfo to CCommandLineInfo::FileNothing. By default, the value of the CcommandLineInfo is CCommandLineInfo::FileNew and generate the document selecting dialog.

BOOL CCTestNoDlgApp::InitInstance()
{
   AfxEnableControlContainer();

   // Standard initialization
   // If you are not using these features and wish to reduce
   // the size of your final executable, you should remove 
   // from the following the specific initialization routines 
   // you do not need.
   #ifdef _AFXDLL
     Enable3dControls(); // Call this when using MFC in a
                         // shared DLL
   #else
     Enable3dControlsStatic(); // Call this when linking to 
                               // MFC statically
   #endif

   // Change the registry key under which our settings are stored.
   // TODO: You should modify this string to be something 
   // appropriate such as the name of your company or organization.
   SetRegistryKey(_T("Local AppWizard-Generated Applications"));
   LoadStdProfileSettings();  // Load standard INI file 
                              // options (including  MRU)
   // Register the application's document templates. Document 
   // templates serve as the connection between documents, 
   // frame windows and views.

   pDocTemplateFirst = new CMultiDocTemplate( 
                IDR_CTESTNTYPE,
                RUNTIME_CLASS(CCTestNoDlgDoc),
                RUNTIME_CLASS(CChildFrame), // custom MDI child frame
                RUNTIME_CLASS(CCTestNoDlgView));

   AddDocTemplate(pDocTemplateFirst);

   pDocTemplateSecond = new CMultiDocTemplate(
               IDR_SECOND_DOC,
               RUNTIME_CLASS(CSecondDoc),
               RUNTIME_CLASS(CChildFrame), // custom MDI child frame
               RUNTIME_CLASS(CSecondView));

   AddDocTemplate(pDocTemplateSecond);

   // create main MDI Frame window
   CMainFrame* pMainFrame = new CMainFrame;
   if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
      return FALSE;

   m_pMainWnd = pMainFrame;

   // Parse command line for standard shell commands, DDE, file open
   CCommandLineInfo cmdInfo;
   ParseCommandLine(cmdInfo);

   // Disable this line to reactivate  the dialog selection 
   // document on start also the dialog new document choice 
   // appears.
   cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

   // Dispatch commands specified on the command line
   if (!ProcessShellCommand(cmdInfo))
      return FALSE;

   // The main window has been initialized, so show and update it.
   pMainFrame->ShowWindow(m_nCmdShow);
   pMainFrame->UpdateWindow();
   return TRUE;
}

Now to create a new document an display the view wanted, it's necessary to intercept the OnFileNew function, or use a fucntion by document for exemple. In the new fucntion, we can display a new personnal dialog selector, or directly select a document. In the example I've chosen to use the second method. The FileNew button in the toolbar is just present to show the difference between the two methods. If you don't want to use it, just remove it.

void CCTestNoDlgApp::OnFileNewFirst()
{
   ((CDocTemplate*)pDocTemplateFirst)->OpenDocumentFile(NULL);
}

void CCTestNoDlgApp::OnFileNewSecond()
{
   ((CDocTemplate*)pDocTemplateSecond)->OpenDocumentFile(NULL);
}

Downloads

Download demo project - 8 Kb
Download source - 36 Kb


Comments

  • Very Good !!

    Posted by Legacy on 02/03/2004 12:00am

    Originally posted by: Ivan Tan

    Awesome Code... Solved my problem for opening and closing of MDI Documents / Views

    Reply
  • How about SDI

    Posted by Legacy on 10/30/2001 12:00am

    Originally posted by: huanghuang


    It is very good for MDI .But when the SDI programm starts,how can I prevent it to open automatically a untitled document?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Webinar June 2, 2015 - 11 am PST / 2 pm EST Your organization and brand are increasingly digital today, proliferating across websites, media outlets, social networks, search engines, mobile apps, online marketplaces and more. Each digital touch point not only enhances the value of your brand, but also represents far greater risk for your company. They increase opportunities for brand defamation and data leakage; act as discreet places to conspire and collude; and open the door to new security …

  • IBM SPSS Modeler is an extensive predictive analytics platform that is designed to bring predictive intelligence to decisions made by individuals, groups, systems, and the enterprise. By providing a range of advanced algorithms and techniques that include text analytics, entity analytics, decision management, and optimization, SPSS Modeler can help you consistently make the right decisions -- from the desktop or within operational systems. Check out this easy-to-absorb infographic to get a picture of what …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date