CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Multiple Document Interface with Visual C++ 6.0



Creating a Text Editor



1) Overview


These directions apply to Visual C++ 6.0 specifically. The task is to write a Windows app (application) that is a simple text editor. We will use the AppWizard's multiple document interface. The editor itself is similar to Notepad and has some of the same limitations. Since it is based on CEditView, which stores in a single CString object a copy of the file being edited, the size of file that can be handled is limited. We add to the editor a File Size menu item under the View pull-down menu. This reports in a dialog box the number of characters currently in the file.

2) Using AppWizard


Begin by making a new project of type "MFC AppWizard (exe)". Name the project Ted (short for tiny editor) and use the default values on all of the dialog screens with this exception: On the sixth screen, click on CTedView and then select CEditView as the base class. (This step is very important as it automatically supplies most of the code for our editor.) Do be sure that Multiple Document Interface was selected on the first screen, of course, though that should be the default.)

3) Change the About Box and Copyright


Click on the Resource View tab and expand the Dialog folder for the Ted project. Double click on IDD_ABOUTBOX and then click on the Copyright line. Right click and select Properties. Change the caption to one word: Freeware. Expand the Menu folder and double click on IDR_TEDTYPE. In the menu editor, click on Window and then on New Window. Press the Delete key to remove this menu option. (We are using CEditView to create our editor. It cannot handle multiple views of the same document.) Expand the Version folder and double click on VS_VERSION_INFO. Double click on Copyright (C) followed by the year. This should let you modify this item to also say Freeware. Do File, Save All.

4) Easy Edit Menu Additions


Now we will add more options to the Edit menu. These are the ones that work automatically just by adding the items to the menu. Expand the Menu folder in Resource View if necessary. Double click on IDR_TEDTYPE. In the menu editor click on Edit. Then click on the blank item at the bottom of the pull-down menu. Type &Find and press Enter. (Note that the ampersand indicates what letter gets underlined for those selecting a menu item by keypress.) Click on the blank item now at the bottom of the pull-down. Type &Repeat and press Enter. In the same way add menu items with Rep&lace and &Select All as the text.

Next double click on the Select All menu item. In the "Menu Item Properties" box, change the ID to ID_EDIT_SELECT_ALL. Make sure you have that underscore before ALL. Then close this box and do File, Save All. Do Build, Build Ted.exe. Then run the program and see what you get.

5) File Size Menu Item


Let's add the menu item for displaying the current file size. In Resource View find the Menu folder. Double click on IDR_TEDTYPE within it. In the menu editor, click on View. Click on the blank item at the bottom of the pull-down menu and then enter the text &File Size. Then use View, ClassWizard. Check that CTedView is the class name selected. Under Object IDs select ID_VIEW_FILESIZE, the ID for the menu item you just created. Under Messages click on COMMAND. Check this screen shot to see what you should have at this point. Click on Add Function, OK, OK. Do File, Save All.

6) File Size Dialog Box


Now we need to add a dialog box in which to show the file size. Use Insert, Resource, Dialog, New. This should show you a form for the new dialog box. We don't need the Cancel button, so click on it and then press the Delete key. Do the same with the OK button, as it is not needed either. (People can close the box with the x in the corner.) Right click on the title bar of your new dialog box. Select Properties. Change the caption to File Size. Also change the ID to IDD_DIALOG_FILESIZE. Here is a picture of the properties box at this point. Then close this properties box.

On the Controls toolbar, click on the edit box button and then click in the middle of your dialog box. This puts a new edit box down. Drag the edge of the edit box to resize it, move it around, and resize the entire dialog box until things look good. Right click on the edit box, select Properties, and change the ID to IDC_EDIT_FILESIZE. Click the Styles tab under this same Properties dialog and check Read-only. It would not make sense to allow the user to try to change the file size number. Close this properties box and do File, Save All.

7) Adding a Class


With the new dialog box on the screen, select View, ClassWizard. It should ask you if you want to create a new class. Click OK. (We want to create a new class as our interface with the dialog.) Name the new class CSizeDlg. The base class should be CDialog. Be sure that the Dialog ID box contains IDD_DIALOG_FILESIZE. Here is a picture of what this should look like. Click OK and OK.

Find the place in your TedView.cpp file to insert the code for the CTedView::OnViewFilesize function. (If need be, find this .cpp file in the Source Files section under File View.) Put the following code into this function:


CSizeDlg dlg;
dlg.m_FileSize = GetWindowTextLength();
dlg.DoModal();   // display the dialog box

This looks up the length of the text in the view window and places this number into the m_FileSize variable. So, we had better add an m_FileSize variable associated with the edit box in our new dialog. (That way, the file size will get displayed in the edit box.) Use View, ClassWizard. Click on the Member Variables tab. Under Class name select CSizeDlg. The ID shown should be IDC_EDIT_FILESIZE. Click on Add Variable. Name the variable m_FileSize. Use the Value category and long for the type. Click OK and OK.

Add a #include of SizeDlg.h at the top of your TedView.cpp file, after any #include lines that are already present. Do a File, Save All. Then try Build, Build (or Rebuild All if Build doesn't behave properly). Run your program and try out the File Size menu item both when a document is open and when no document is open. You now have a nice little text editor.

8) For Further Practice (Optional)


If you are brave, you might want to try the following. Note that complete directions are not given, so that you will have to figure out on your own how to deal with a number of complications. The suggestion is to place under the Edit pull-down a Spell Check menu item. It should bring up a Spell Check dialog box that shows in an edit box each word (one by one, in succession) that the program thinks may be misspelled. You will have to find a file of common words. (If you have access to Linux, see if you have the linux.words file.) To get fast lookup to see if a particular word is present, you may want to store the words in a file-based hash table. The Spell Check dialog box could show in a separate edit box the words immediately surrounding the suspected misspelled word. This is to provide the user with the context. You could provide a Continue button and an Ignore All button. If the user has not changed the displayed word, the Continue button should just let the spell checker go on. If the word was modified by the user, clicking Continue should cause the new spelling to replace the old. The Ignore All button should tell the spell checker to ignore all instances of the current word. This can be handled by placing each word to be ignored on a linked list that can be checked every time you reach a word that is not in the file of common words (dictionary). The STL list container is suggested for this.

9) Reference:


This example is partially based on an article by Marshall Brain entitled Understanding the AppWizard and ClassWizard in Visual C++ Version 6.x that appeared at DevCentral. The article might no longer be there by the time that you read this.

Related Item


Multiple Document Interface with Visual C++ .NET

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: August 27, 2009
Disclaimer