次の方法で共有


CDocument::OnNewDocument

ファイルの新しいコマンドの一部として、フレームワークによって呼び出されます。

virtual BOOL OnNewDocument( );

戻り値

ドキュメントが正常に初期化された場合、; それ以外の場合は 0。

解説

このの既定の実装では、関数呼び出し、ドキュメントが空でない場合は、次に示すことを確認する DeleteContents のメンバー関数を新しいドキュメント。新しいドキュメントのデータ構造体を初期化するには、この関数をオーバーライドします。は、オーバーライド関数からこの関数の基本クラスのバージョンを呼び出す必要があります。

ユーザーが SDI アプリケーション ファイルの新しいコマンドを選択すると、フレームワークは使用して新しいを作成するのではなく、既存の図面、初期化をやり直すには、この関数が。ユーザーがマルチ ドキュメント インターフェイスの (MDI) アプリケーションで新しいファイルを選択すると、フレームワークは常に新しいドキュメントを作成し、それを初期化するには、この関数を呼び出します。SDI アプリケーションで効果的なファイルの新しいコマンドのコンストラクターではなく、この関数に初期化コードを記述する必要があります。

OnNewDocument が重複して呼び出される場合があることに注意してください。これはドキュメントが ActiveX ドキュメント サーバーとして埋め込まれている場合に発生します。関数は CreateInstance のメソッド ( COleObjectFactory派生クラス) と InitNew のメソッドにより、もう一度までに最初に呼び出されます ( COleServerDocで公開される-派生クラスで公開されます)。

使用例

次の例は、ドキュメントのオブジェクトを初期化する方法を説明します。

// 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;
}

必要条件

ヘッダー: afxwin.h

参照

関連項目

CDocument クラス

階層図

CDocument::CDocument

CDocument::DeleteContents

CDocument::OnCloseDocument

CDocument::OnOpenDocument

CDocument::OnSaveDocument