Saturday, October 10, 2009

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 class, you cannot directly use it in your application. You have two main alternatives. You can derive your own class based on CView (the CView class itself is based on CWnd) or you can use one of the many view classes that the MFC provides. The classes that are readily available to you are:

Class Description
CEditView Used for a basic text editing application
CRichEditView Allows creating rich documents that perform text and paragraph formatting
CScrollView Provides the ability to scroll a view horizontally and vertically
CListView Provides a view that can display a list of items
CTreeView Allows displaying a list of items arranged as a tree
CFormView Used to create a view that resembles a dialog box but provides the document/view features
CDaoRecordView Provides a view that resembles a dialog box but used for database controls
CCtrlView Provides parental guidance to the CEditView, CListView, CTreeView, and CRichEditView

As we move on, we will study these classes as needed.

Once you have a frame, a document, and a view, you can create an application, which, as we have learned so far, is done by deriving a class from CWinApp and overriding the InitInstance() method. In the InitInstance() implementation, you must let the compiler know how a document is created in your application. To do this, you must provide a sample document, called a template, that defines the parts that constitute a document for your particular type of application. This is done using a pointer variable declared from CDocTemplate or one of its derived classes.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.