The Notes Application

Introduction

The CEditView class is used to create a simple text editor type of application, like Notepad. Its class is equipped all the primary functionality you would need from it. This is because all the necessary behavior is already implemented in the class, with just a few minor exceptions.

Creating the Application

If you use AppWizard to create an MFC application based on CEditView, the interface presented is mostly the same shared by other CView derived applications. Therefore, if you want, you add the other features once you finish with the wizard. Fortunately, to implement some of these features, you don't need to write a single line of code. For some others, because the features may be based on your own needs, you may have to write code accordingly.

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++
  2. To create a new project, display the New dialog box
  3. In the Projects property page, select MFC AppWizard (exe)
  4. Set the name of the project to Notes and click OK
  5. In MFC AppWizard - Step 1, select Single Document and click Next five times
  6. In MFC AppWizard - Step 6 of 6, in the Base Class combo box, select CEditView and click Finish then click OK 
  7. From the ResourceView tab of the Workspace, expand the Notes Resources and expand the Menu node
  8. Double-click IDR_MAINFRAME
  9. Right-click the first empty field under the Edit menu and click Properties
  10. Make it a Separator
  11. Click the first empty field under the new separator
  12. In the ID combo box, select ID_EDIT_FIND
     
  13. Change its Caption to &Find...\tCtrl+F
  14. In the same way, set the menu as follows:
     
    ID Caption Separator
      Undo  
      Cut  
      Paste  
        Separator
    ID_EDIT_FIND &Find...\tCtrl+F  
    ID_EDIT_REPLACE &Replace...\tCtrl+H  
        Separator
    ID_EDIT_SELECTALL Select &All\tCtrl+A  
    ID_EDIT_CLEAR &Delete\tDel  
  15. In the ResourceView tab of the Workspace, expand the Accelerator tab and double-click IDR_MAINFRAME
  16. Double-click the first empty field in the list. In the Accel Properties window, in the ID combo box, select ID_EDIT_FIND and press Tab. In the Key combo box, type F and press Enter
  17. In the same way, add an accelerator for the newly added menu item. For the ID_EDIT_CLEAR, in the Key combo box, select VK_DELETE and remove the check mark on the Ctrl check box before pressing Enter
  18. Test the application. Notice that, without having written code, you can use the Find or the Replace dialog boxes
  19. To change the initial font of the editor, press Ctrl + W
  20. In the MFC ClassWizard dialog box and in the Message Maps property page, select CNotesView in the Class Name combo box
  21. In the Messages combo box, double-click OnInitialUpdate and click Edit Code
  22. Implement the event as follows:
     
    void CNotesView::OnInitialUpdate() 
    {
    	CEditView::OnInitialUpdate();
    	
    	// TODO: Add your specialized code here and/or call the base class
    	CEdit &curEditor = GetEditCtrl();
    	CFont *SimpleFont = new CFont;
    
    	SimpleFont->CreatePointFont(120, "Times New Roman");
    	curEditor.SetFont(SimpleFont);
    }
  23. Display the IDR_MAINFRAME menu again
  24. To allow the user to select a different font, click the empty box on the right side of Help
  25. Type F&ormat and press Enter
  26. In the first box under Format, click and type &Font...
  27. Change its Prompt to Select a different font\nFont
  28. Move the Format menu item to position it between View and Help
  29. Press Ctrl + W
  30. In the Class Name combo box, select CNotesView. In the Object IDs list, select ID_FORMAT_FONT
  31. In the Messages list, double-click COMMAND
  32. Accept the suggested name of the function and click OK then click Edit Code
  33. Implement the event as follows:
     
    void CNotesView::OnFormatFont() 
    {
        // TODO: Add your command handler code here
        CEdit &curEditor = GetEditCtrl();
        CFont *SimpleFont = new CFont;
        CFontDialog dlgFont;
    
        dlgFont.m_cf.Flags &= ~CF_EFFECTS;
    
        if( dlgFont.DoModal() )
        {
    	SimpleFont->CreatePointFont(dlgFont.GetSize(), dlgFont.GetFaceName());
    	curEditor.SetFont(SimpleFont);	
        }
    }
  34. Test the application
  35. After using it, close it and return to your programming environment.
 
 

Home Copyright © 2004-2014 FunctionX, Inc.