Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


Figure 50: Notepad as an SDI...................................................................................................


Notepad can be used to open only one document such as an HTML file, to view another
file, the user would either replace the current document or launch another copy of
Notepad.

To create an SDI, Microsoft Visual C++ provides the MFC wizard which provides all the
basic functionality that an application needs, including the resources and classes.

5.2.2 Creating a Single Document Interface.............................................


As mentioned earlier, after creating a frame, a document, and a view, you can create an
application by deriving a class from CWinApp and overriding the virtual InitInstance()
member function. In InitInstance(), you must provide a template for your type of
application. This is done using a CDocTemplate type of object.

To create an SDI, the CDocTemplate class provides the CSingleDocTemplate class
used to create an application that provides only one view. Therefore, you can declare a
pointer variable to CSingleDocTemplate. Using this pointer and the new operator, use
the CSingleDocTemplate constructor to provide the template. The syntax of the
CSingleDocTemplate constructor is:

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

The CSingleDocTemplate constructor needs the common identifier for the resources of
your application. We saw in Lesson 3 that this can be done by using IDR_MAINFRAME
as the common name of most or all main resources of an application. This is provided as
the nIDResource argument.

The second argument, pDocClass, is the name of the class you derived from
CDocument, as mentioned earlier.
Free download pdf