Programming with Microsoft Visual C++.NET 6ed [Electronic resources]

George Shepherd, David Kruglinski

نسخه متنی -صفحه : 319/ 105
نمايش فراداده

MFC Text Editing Options

Windows itself supplies two text editing tools: the edit control and the rich edit common control. Both can be used as controls within dialog boxes, but both can also be made to look like view windows. The MFC library supports this versatility with the CEditView and CRichEditView classes.

The CEditView Class

This class is based on the Windows edit control. The MFC Application Wizard gives you the option of making CEditView the base class of your view class. When the framework gives you an edit view object, it has all the functionality of both CView and CEdit. There's no multiple inheritance here, just some magic that involves window subclassing. The CEditView class implements and maps the Clipboard cut, copy, and paste functions, so they appear active on the Edit menu. The default character limit for CEditView is 1,048,575. You can change the character limit by sending the EM_LIMITTEXT message to the underlying edit control. However, the limits are different depending on the operating system and the type of edit control (single or multi-line). See the MSDN Library for more information on these limits.

The CRichEditView Class

This class uses the rich edit control, so it supports mixed formats and large quantities of text. The CRichEditView class is designed to be used with the CRichEditDoc and CRichEditCntrItem classes to implement a complete ActiveX container application.

The CRichEditCtrl Class

This class wraps the rich edit control, and you can use it to make a fairly decent text editor. That's exactly what we'll do in the upcoming Ex12a example. We'll use an ordinary view class derived from CView, and we'll cover the view's client area with a big rich edit control that resizes itself when the view size changes. The CRichEditCtrl class has dozens of useful member functions, and it picks up other functions from its CWnd base class. The functions we'll use in this chapter are listed in Table 12-1.

Table 12-1: Commonly Used CRichEditCtrl Functions

Function

Description

Create

Creates the rich edit control window (which is called from the parent's WM_CREATE handler).

SetWindowPos

Sets the size and position of the edit window (sizes the control to cover the view's client area).

GetWindowText

Retrieves plain text from the control. (CRichEditCtrl includes other functions for retrieving the text using rich text formatting codes.)

SetWindowText

Stores plain text in the control.

GetModify

Gets a flag that is TRUE if the text has been modified (when the user types in the control or the program calls SetModify(TRUE)).

SetModify

Sets the modify flag to TRUE or FALSE.

GetSel

Gets a flag that indicates whether the user has selected text

SetDefaultCharFormat

Sets the control's default format characteristics.

SetSelectionCharFormat

Sets the format characteristics of the selected text.

Note

If you use the dialog editor to add a rich edit control to a dialog resource, your application class InitInstance member function must call the function AfxInitRichEdit.