Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


An application cannot exist without a frame. As we saw in previous lessons, to provide a
frame to an application, you can derive a class from CFrameWnd.

5.1.5 The Document/View Approach.........................................................


To create an application, you obviously should start by providing a frame. This can be
taken care of by deriving a class from CFrameWnd. Here is an example:

class CMainFrame : public CFrameWnd
{
DECLARE_DYNCREATE(CMainFrame)

DECLARE_MESSAGE_MAP()
};

To give "physical" presence to the frame of an application, you can declare an OnCreate()
method. Here is an example:

class CMainFrame : public CFrameWnd
{
DECLARE_DYNCREATE(CMainFrame)

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};

The easiest way you can implement this method is to call the parent class, CFrameWnd,
to create the window. As we have seen in the past, if this method returns 0, the frame has
been created. It returns -1, this indicates that the window has been destroyed. Therefore,
you can create a frame as follows:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// Call the base class to create the window
if( CFrameWnd::OnCreate(lpCreateStruct) == 0)
return 0;

// else is implied
return -1;
}

To allow users to interact with your application, you should provide a document. To do
this, you can derive a class from CDocument so you can take advantage of this class. If
you do not plan to do anything with the document, you can just make it an empty class.
Here is an example:

class CExerciseDoc : public CDocument
{
DECLARE_DYNCREATE(CExerciseDoc)

DECLARE_MESSAGE_MAP()
};

Besides the few things we have learned so far, your next big decision may consist on the
type of application you want to create. This is provided as a view. The most fundamental
class of the view implementations in the MFC is CView. Because CView is an abstract
Free download pdf