Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 5: The Document/View Architecture


return TRUE;
}

The above code allows only creating a window frame for the parent window. To allow
the user to interact with the computer through your MDI, you must provide a template for
a document. The child document must have its own frame, distinct from that of the
parent. To provide this frame, you have two main alternatives. You can directly use the
CMDIChildWnd class or you can derive your own class from CMDIChildWnd:

class CChildFrame : public CMDIChildWnd
{
DECLARE_DYNCREATE(CChildFrame)

DECLARE_MESSAGE_MAP()
};

As opposed to a Single Document Interface application, to create a Multiple Document
Interface (MDI) application, you use the CMultiDocTemplate class which, like the
CSingleDocTemplate class, is derived from CDocTemplate. Therefore, you must declare
a pointer variable to CMultiDocTemplate using the new operator, then use its constructor
to initialize the template. The syntax of the CMultiDocTemplate constructor is:

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

To make a document different from the parent, you must create an additional series of
resources that share a common name other than IDR_MAINFRAME. The most basic
resources you should create are a menu and an icon. There are two main types of MDI
applications.

One kind of application may use only one particular category of documents such as only
text-based. Because text-based documents can include ASCII text files, rich text
documents (RTF), HTML files, script-based documents (JavaScript, VCScript, Perl,
PHP, etc), etc, such an application may be configured to open only that type of document.
To create such an MDI application, in the constructor of the CMultiDocTemplate object
that you are using, specify a CView derived class (CEditView, CListView, etc) or your
own class you derived from CView or one of its children. Here is an example:

BOOL CMultiEdit1App::InitInstance()
{
CMultiDocTemplate* pDocEdit;
pDocEdit = new CMultiDocTemplate(
IDR_EDITTYPE,
RUNTIME_CLASS(CMultiEdit1Doc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CEditView));
AddDocTemplate(pDocEdit);

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

CCommandLineInfo cmdInfo;
Free download pdf