CSingleDocTemplate 類別

定義實作單一文件介面 (SDI) 的文件範本。

語法

class CSingleDocTemplate : public CDocTemplate

成員

公用建構函式

名稱 描述
CSingleDocTemplate::CSingleDocTemplate 建構 CSingleDocTemplate 物件。

備註

SDI 應用程式會使用主框架視窗來顯示檔;一次只能開啟一份檔。

檔案樣本會定義三種類別類型之間的關聯性:

  • 檔案類別,衍生自 CDocument

  • 檢視類別,顯示上面所列檔案類別的數據。 您可以從 、、 CScrollViewCFormViewCEditView衍生這個類別CView。 (您也可以直接使用 CEditView

  • 包含檢視的框架視窗類別。 針對 SDI 檔範本,您可以從 衍生此類別 CFrameWnd;如果您不需要自定義主框架視窗的行為,則可以直接使用 CFrameWnd 而不衍生您自己的類別。

SDI 應用程式通常支援一種檔案類型,因此它只有一個 CSingleDocTemplate 物件。 一次只能開啟一份檔。

除了建構函式之外,您不需要呼叫 的任何成員函 CSingleDocTemplate 式。 架構會在內部處理 CSingleDocTemplate 物件。

如需使用 CSingleDocTemplate的詳細資訊,請參閱 文件範本和檔/檢視建立程式

繼承階層架構

CObject

CCmdTarget

CDocTemplate

CSingleDocTemplate

需求

標題: afxwin.h

CSingleDocTemplate::CSingleDocTemplate

建構 CSingleDocTemplate 物件。

CSingleDocTemplate(
    UINT nIDResource,
    CRuntimeClass* pDocClass,
    CRuntimeClass* pFrameClass,
    CRuntimeClass* pViewClass);

參數

nIDResource
指定搭配檔類型使用之資源的識別碼。 這可能包括功能表、圖示、快速鍵數據表和字串資源。

字串資源包含最多七個以 '\n' 字元分隔的子字元串(如果未包含子字串,則需要 '\n' 字元做為佔位元;不過,不需要尾端 '\n' 字元:這些子字串會描述檔類型。 如需子字串的相關信息,請參閱 CDocTemplate::GetDocString。 此字串資源位於應用程式的資源檔中。 例如:

// MYCALC.RC
STRINGTABLE PRELOAD DISCARDABLE
BEGIN
  IDR_MAINFRAME "MyCalc Windows Application\nSheet\nWorksheet\n Worksheets (*.myc)\n.myc\nMyCalcSheet\n MyCalc Worksheet"
END

您可以使用字串編輯器來編輯此字串;整個字串會顯示為字串編輯器中的單一專案,而不是七個不同的專案。

如需這些資源類型的詳細資訊,請參閱 字串編輯器

pDocClass
CRuntimeClass指向檔案類別的物件。 這個類別是您 CDocument定義來代表檔的衍生類別。

pFrameClass
CRuntimeClass指向框架視窗類別的物件。 這個類別可以是 CFrameWnd衍生類別,或者如果您想要主框架視窗的默認行為, CFrameWnd 也可以是本身。

pViewClass
指向 CRuntimeClass 檢視類別的物件。 這個類別是 CView用來顯示檔的衍生類別。

備註

動態設定 CSingleDocTemplate 物件,並將它從InitInstance應用程式類別的成員函式傳遞至 CWinApp::AddDocTemplate

範例

// The following code fragment is from CMyWinApp::InitInstance.
// CMyWinApp is derived from CWinApp.

// Establish the document type
// supported by the application
AddDocTemplate(new CSingleDocTemplate(IDR_MAINFRAME,
   RUNTIME_CLASS(CMyDoc),
   RUNTIME_CLASS(CMainFrame),       // main SDI frame window
   RUNTIME_CLASS(CMyView)));

 

// The following code fragment is from CMyWinApp::InitInstance.
// CMyWinApp is derived from CWinApp.

// Normally, an application creates a document
// template and registers it with MFC as a part
// of its initialization.

// IDR_SAMPLERESOURCE is a resource ID string;
// see the CDocTemplate class overview documentation
// for more information on its format.

// The next three parameters use the RUNTIME_CLASS()
// macro to get runtime type information for the doc,
// frame, and view classes that will be associated by
// the template.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
   RUNTIME_CLASS(CMyDoc),
   RUNTIME_CLASS(CMainFrame),       // main SDI frame window
   RUNTIME_CLASS(CMyView));
if (!pDocTemplate)
return FALSE;

// After the following call, MFC is aware of the doc
// template and will free it when the application is
// shut down. The doc templates known to MFC will
// automatically be used when CWinApp:OnFileOpen() or 
// CWinApp::OnFileNew() are called.
AddDocTemplate(pDocTemplate);

另請參閱

MFC 範例 DOCKTOOL
CDocTemplate 類別
階層架構圖表
CDocTemplate 類別
CDocument 類別
CFrameWnd 類別
CMultiDocTemplate 類別
CView 類別
CWinApp 類別