共用方式為


CDocument::OnNewDocument

Called by the framework as part of the File New command.

virtual BOOL OnNewDocument( );

Return Value

Nonzero if the document was successfully initialized; otherwise 0.

Remarks

The default implementation of this function calls the DeleteContents member function to ensure that the document is empty and then marks the new document as clean. Override this function to initialize the data structure for a new document. You should call the base class version of this function from your override.

If the user chooses the File New command in an SDI application, the framework uses this function to reinitialize the existing document, rather than creating a new one. If the user chooses File New in a multiple document interface (MDI) application, the framework creates a new document each time and then calls this function to initialize it. You must place your initialization code in this function instead of in the constructor for the File New command to be effective in SDI applications.

Note that there are cases where OnNewDocument is called twice. This occurs when the document is embedded as an ActiveX Document Server. The function is first called by the CreateInstance method (exposed by the COleObjectFactory-derived class) and a second time by the InitNew method (exposed by the COleServerDoc-derived class).

Example

The following examples illustrate alternative methods of initializing a document object.

// Method 1: In an MDI application, the simplest place to do 
// initialization is in the document constructor.  The framework 
// always creates a new document object for File New or File Open.
CExampleDoc::CExampleDoc()
{
   // Do initialization of MDI document here.
}
// Method 2: In an SDI or MDI application, do all initialization 
// in an override of OnNewDocument, if you are certain that
// the initialization is effectively saved upon File Save
// and fully restored upon File Open, via serialization.
BOOL CMyDoc::OnNewDocument()
{
   if (!CDocument::OnNewDocument())
      return FALSE;

   // Do initialization of new document here.

   return TRUE;
}
// Method 3: If the initialization of your document is not
// effectively saved and restored by serialization (during File Save
// and File Open), then implement the initialization in single
// function (named InitMyDocument in this example).  Call the
// shared initialization function from overrides of both
// OnNewDocument and OnOpenDocument.
BOOL CExampleDoc::OnNewDocument()
{
   if (!CDocument::OnNewDocument())
      return FALSE;

   InitMyDocument(); // call your shared initialization function

   // If your new document object requires additional initialization
   // not necessary when the document is deserialized via File Open,
   // then perform that additional initialization here.

   return TRUE;
}

Requirements

Header: afxwin.h

See Also

Reference

CDocument Class

Hierarchy Chart

CDocument::CDocument

CDocument::DeleteContents

CDocument::OnCloseDocument

CDocument::OnOpenDocument

CDocument::OnSaveDocument

Other Resources

CDocument Members