Topic : MFC - Views
Author : Unknown
Page : 1 Next >>
Go to page :


We are going to introduce various types of views in this chapter. In MFC, there are several types of standard views that are supported by MFC classes. These views include edit view, rich edit view, list view, form view and tree view. The classes that can be used to implement them are CEditView, CRichEditView, CListView, CFormView and CTreeView respectively. They can be used to display plain text, formatted text, tree, list, etc.

1 Edit View

Edit view is most suitable for implementing plain text editor. Remember in Chapter 9 when building one-line text editor, we had to write a lot of code in order to add some basic editing functions. Actually, if we use edit view to implement text editor, we can build a notepad-like application within just few minutes. With the edit view, the serialization can be implemented by the member function of class CEditView, so we don't even need to write code for handling data loading and storing. Also, this class supports commands such as string search & replace, cut, copy and paste.

Generating the Application

A plain text editor can be generated through using Application Wizard. We can let the editor support files with ".txt" extension as we go through the steps of generating application skeleton (In order to do this, we need to click "Advanced" button in step 4, and input appropriate file extension into the edit box labeled with "File extension"), or we can edit IDR_MAINFRAME string resource after the application is created. We need to select CEditView as the base class of the view in the last step. By doing so, after the application is first compiled, we will have a very simple notepad-like application.

Sample 1\NotePad is generated this way. We even don't need to customize function CDocument:: Serialize(...) in order to support file I/O. Lets take a look at the default implementation of serialization:

void CNotePadDoc::Serialize(CArchive& ar)



Everything is handled by function CEditView::SerializeRaw(...), which includes both data reading and writing.

Also, there is no need for us to write message handlers for Edit | Undo, Edit | Cut, Edit | Copy and Edit | Paste commands in order to enable them. If the mainframe menu contains the following IDs for these commands (which is the default feature for any project generated by Application Wizard), the application will automatically support the above commands:

(Table omitted)

The reason for this is that CEditView already maps commands with the above-mentioned IDs to its built-in member functions that handle undo, cut, copy and paste commands. The name of these functions are not documented in the current version of Visual C++, this means these function are not guaranteed to be supported in the future.

If we want to use other command IDs instead of the recommended ones, we need to implement command message mapping by ourselves. In order to do so, we need to look at the MFC source code that contains member functions of class CEditView, find out the function names that support these commands, and map the WM_COMMAND type messages to the appropriate functions.

Search Related Commands

In the sample, three other standard commands, Search | Find..., Search | Replace..., and Search | Find Next are implemented this way. The three commands are used for searching a specific string in the text contained in the view, replacing an old string with a new one, or repeating searching. The default IDs for these commands are listed in the following table:

(Table omitted)

Message UPDATE_COMMAND_UI is also handled for the above commands.

In the sample, we use following non-standard command IDs to implement find, replace, and repeat commands:

(Table omitted)

The message mapping is done in the implementation file "NPView.cpp":

(Code omitted)

With the above implementation, there is no need for us to declare and define new member functions to handle the above commands, everything will be handled automatically.

Other Commands

In the sample, some other commands that are not supported by class CEditVew are also implemented. These command include Edit | Delete, which can be used to delete the current selection; Edit | Select All, which can be used to select all the text contained in the window, and Edit | Time/Date, which can be used to insert a time/date stamp at the current caret position. For these commands, the message mapping and message handlers need to be implemented by ourselves.

Edit view is implemented by an embedded edit control, which can be accessed by calling function CEditView::GetEditCtrl(). Once this is done, we can call any member function of CEdit and make change to the text contained in the window. For example, if we want to replace the selected text with a new string or insert a string at the current caret position, we can call function CEdit::ReplaceSel(...) to do so. If we want to select all the text, we can call function CEdit::SetSel(...) and pass 0 and -1 to its first two parameters. If we want to delete the selected text, we just need to call function CEdit::Clear().

The following shows how command Edit | Time/Date is implemented in the sample:

(Code omitted)

First the current time is obtained by calling function CTime::GetCurrentTime(), which is stored in a CTime type variable. Then it is formatted and output to a CString type variable by calling function CTime:: Format(...). Finally, function CEdit::ReplaceSel(...) is called to insert the time stamp.

By now, our sample is almost the same with standard "Notepad" application. Obviously deriving class from standard MFC class saves us a lot of work. If we implement the file I/O and formatted text display by ourselves, we need to write a lot of source code.

2 Rich Edit View

Sample 2\Wordpad is an SDI application generated from Application Wizard, it will be implemented as a "Wordpad" application.

The edit view has very limited feature: it can store only text less than 64Kbytes, and it supports only one type of font. Furthermore, we can not edit graphics in the editor.

A more advanced editor can be implemented by using Rich edit view and Rich edit document. Two classes that support this type of view and document are CRichEditView and CRichEditDoc respectively. With the two classes, we can easily build a Wordpad-like application, which supports rich text format (one of the formats supported by Microsoft Word), OLE container and many default editing functions.

It seems that building a Wordpad-like application is very difficult, because it has too many features. However, with CRichEditView and CRichEditDoc classes, this procedure is almost equally simple with creating a "Notepad" application as we did in the previous section. To build a Wordpad-like application, we can start from Application Wizard to build a standard SDI application. To support ".rtf" file extension in our application (which is the default extension for rich edit format files), in step 4, we need to click "Advanced" button, input "rtf" in the edit box labeled with "File extension". In the final step, we also need to select CRichEditView as the base class for view implementation.

Generally, the base class for implementing application's document can not be customized. It will be derived from CDocument by default. However, if class CRichEditView is selected as base class for view implementation, the base class for document implementation will automatically be changed to CRichEditDoc. Also, some new commands will be added to the default mainframe menu IDR_MAINFRAME under "Edit" sub-menu (These new commands will be added by Application Wizard).

In order to include graphic objects (or other OLE server objects), we need to make the application an OLE container. By doing this, we can embed all OLE servers in the application. One example of such type of applications is "Paint". With this implementation, the application can be used to edit not only text, but also graphic objects. The OLE container selection can be set in the second step of Application Wizard. If we forgot this, we will be prompted to do so when button "Finish" is clicked in the final step.

By only working with Application Wizard, we are able to create a simple Wordpad. If we compile the application at this point, we will have a standard editor that allows us to insert various types of objects. The type of objects that can be embedded in the application depends on which OLE servers have registered in the system. To insert an object, we can execute command Edit | Insert New Object..., and select an object from the popped dialog box. If we select "Bitmap Image" object, the application will turn into a "Paint" application, with will let us edit bitmap in place.

Another feature of this editor is Edit | Paste Special... command. Because the editor supports not only text, but also graphic editing now, the paste command should support multiple-format data. If we execute command Edit | Paste Special..., a dialog box will pop up indicating the available data formats contained in the clipboard, from which we can make the selection. For example, if we paste a bitmap, we have the choice to paste it either as bitmap format, metafile format or DIB format.

The third feature of this editor is font selection. We may want to format different portion of the text using a different font. This

Page : 1 Next >>