Knowledge Base

How To Prevent Creation of Initial Macintosh Document

Article ID: 152029

Article Last Modified on 7/1/2004


APPLIES TO


This article was previously published under Q152029

SUMMARY

Sometimes it is preferable not to initially open a new document in an MFC application. To implement this behavior on Windows, you can remove the call to OnFileNew() or ParseCommandLine() from InitInstance. On the Macintosh, you need to override CWinApp::CreateInitialDocument(). This article describes how to accomplish these behaviors.

MORE INFORMATION

In a Windows MFC application, a new document is initially created if the Command Line is empty. Since Macintosh applications do not have Command Lines, the open application Apple Event is sent to the application and is handled by _AfxOpenAppHandler(), which calls CreateInitialDocument() to open a new document:
  // From Visual C++ 4.0
   OSErr PASCAL _AfxOpenAppHandler(AppleEvent* pae,
                                   AppleEvent* paeReply,
                                   long lRefcon)
   {
     CWinApp* pApp;
     OSErr err = HandlerCommon(pae, lRefcon, pApp);
     if (err == noErr)
     {
       if (pApp->CreateInitialDocument())
         err = noErr;
       else
         err = errAEEventNotHandled;
     }
     AfxOleSetUserCtrl(TRUE);
     return err;
   }

   // From Visual C++ 4.0
   BOOL CWinApp::CreateInitialDocument()
   {
     if (m_pMainWnd != NULL)
       m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_NEW);
     else if (m_pDocManager != NULL)
     {
       POSITION pos = m_pDocManager->GetFirstDocTemplatePosition();
       if (pos != NULL)
       {
         CDocTemplate* pTemplate = m_pDocManager->GetNextDocTemplate(pos);

         // if MDI, or SDI but we haven't opened any documents yet,
         // open a new one
         if (pTemplate != NULL &&
           (pTemplate->IsKindOf(RUNTIME_CLASS(CMultiDocTemplate)) ||
           m_pDocManager->GetOpenDocumentCount() == 0))
         {
           OnFileNew();
         }
       }
     }
     return TRUE;
   }
				
When a new document is not needed, you can override CreateInitialDocument() and have it return TRUE. This is desirable to prevent the file type dialog from appearing in applications supporting multiple document types:
   BOOL CMyWinApp::CreateInitialDocument()
   {
     return TRUE;
   }
				

REFERENCES

See "Creating Initial Documents" in the Macintosh Porting Guide within the Online Documentation.

Additional query words: WM_MACINTOSH WLM_MACEVENT

Keywords: kbhowto kbnoupdate kbdocview KB152029