Introduction to the Rich Edit Control
|
|
|
A rich edit control is a Windows object that resembles
an edit box but can handle text that is formatted. This mean that it can
display text with various characters formats and can show paragraphs with
different alignments. A rich edit control can also allow a user to change
the formatting on characters and control the alignment of paragraphs.
|
Practical
Learning: Introducing the Rich Edit Controls
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click MFC Application
- Set the Name to RichFormatter
- Click OK
- In the first page of the MFC Application Wizard, click Next
- In the second page of the wizard, click Dialog Box
- Click Next
- In the third page of the wizard, click About Box to remove the
check mark on it
- Change the Dialog Title to Rich Editor
- Click Next twice
- Click Finish
- On the dialog box, click TODO: and press Delete
Creating a Rich Edit Control
|
|
To create a rich edit control, you ca use the Rich
Edit 2.0 Control
from the Toolbox. You can also programmatically create this control using
the CRichEditCtrl class. To do this, use its (default) constructor:
CRichEditCtrl RichEditor;
After declaring this variable, you can define the
characteristics of the control using the CRichEditCtrl::Create()
method. Its syntax is:
BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
The dwStyle argument specifies the window style to
apply to the control. Since the rich edit control is not a container, it
is usually positioned on another control such as a form or dialog box.
Therefore, the primary style you must apply is WS_CHILD. To display the
control to the user, also add the WS_VISIBLE style. This primary
style can be defined as follows:
DWORD RichStyle = WS_CHILD | WS_VISIBLE;
Although a rich edit control can be used as a
single-line text object, to make it more efficient, you should make it use
various lines. This can be taken care of by setting the Multiline property
to True or adding the ES_MULTILINE style. An example would be:
DWORD RichStyle = WS_CHILD | WS_VISIBLE | ES_MULTILNE;
Once a rich edit can display multiple lines of text,
if the text is longer than the control can display, you should equip it
with scroll bars. The vertical scroll bar is made available by adding
checking the Vertical Scroll check box or setting it to True. This can be
done programmatically by adding the WS_VSCROLL window style. Here
is an example:
DWORD RichStyle = WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILNE;
The horizontal scroll bar is made possible by setting
the Horizontal Scroll property to True. To do this programmatically, add
the WS_HSCROLL window style.
If you do not want the user to change the text in the
rich edit control, you can set the Read-Only property to True. This can
also be done by adding the ES_READONLY style.
The rect argument specifies the location and
dimensions of the control.
The pParentWnd argument is the control that is
hosting the rich edit control. It is usually a form or a dialog box.
The nID is an identifier for the rich edit
control.
Practical
Learning: Creating a Rich Edit Application
|
|
- In the Solution Explorer, in the Source Files node, double-click
RichFormatter.cpp
- In the InitInstance method, type ::AfxInitRichEdit2();
BOOL CRichFormatterApp::InitInstance()
{
//TODO: call AfxInitRichEdit2() to initialize richedit2 library.
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
::AfxInitRichEdit2();
CWinApp::InitInstance();
. . . No Change
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
- On the Toolbox, click the Rich Edit 2.0 Control
and draw a rectangle from the left border to the left of the right
border
- On the Properties window, change its ID to IDC_EDITOR
- Set the following properties to True: Multiline, Want Return,
Vertical Scroll
- Right-click the rich edit control and click Add Variable
- Make sure the Variable Type is set to CRichEditCtrl. Set the Name
to m_RichEditor
- Click OK
Characteristics of a Rich Edit Control
|
|
The primary role of a rich edit control is to hold
text. When its content is long or large, it can be equipped with scroll
bars, either or both horizontal and vertical. The control allows you to
specify these characteristics or you can control them at design time if
you want.
Practical
Learning: Controlling the Scroll Bars
|
|
- On the dialog box, click the rich edit control
- In the Properties window, change the values of the following
characteristics
Auto HScroll: False
Auto VScroll: True
Vertical Scroll: True
Formatting the Selected Text
|
|
At first glance, a rich edit appears like a regular
edit control. Its ability to format text and paragraph sets them apart. To
change the appearance of a letter, a word or a paragraph, you can change
its size, height, or weight. This can be done by calling the
CRichEditCtrl::SetSelectionCharFormat() method. Its syntax is:
BOOL SetSelectionCharFormat(CHARFORMAT& cf);
This simply means that the rich edit control relies on
the Win32 API's CHARFORMAT structure to format text. This structure
is defined as follows:
typedef struct _charformat {
UINT cbSize;
DWORD dwMask;
DWORD dwEffects;
LONG yHeight;
LONG yOffset;
COLORREF crTextColor;
BYTE bCharSet;
BYTE bPitchAndFamily;
TCHAR szFaceName[LF_FACESIZE];
} CHARFORMAT;
To format the characters, declare a variable of this
structure and take its size. Then initialize the necessary member
variables, ignoring those you do not need. To start, initialize dwMask
with the type of formatting you want to apply or the type of operation you
want to perform. The possible values are:
Value |
Used to |
CFM_BOLD |
Make the character(s) bold |
CFM_ITALIC |
Italicize the character(s) |
CFM_UNDERLINE |
Underline the character(s) |
CFM_STRIKEOUT |
Strike out the character(s) |
CFM_SIZE |
Change the size the character(s) |
CFM_CHARSET |
Access character set |
CFM_COLOR |
Change the color of the text |
CFM_FACE |
Set the font name |
CFM_OFFSET |
Offset the character(s) |
CFM_PROTECTED |
Protect the character(s) |
You can apply the actual text formatting using the
dwEffects member variable. Its possible values are: CFE_AUTOCOLOR,
CFE_BOLD, CFE_ITALIC, CFE_STRIKEOUT, CFE_UNDERLINE,
and CFE_PROTECTED. These effects can be combined as needed using
the bitwise OR operator. For example, you can combine CFE_BOLD and
CFE_ITALIC as CFE_BOLD | CFE_ITALIC to have text that is
both in bold and italic.
The yHeight variable is used to set the new
height of the text.
The yOffset variable is used to create a
superscript or subscript effect.
The crTextColor variable is used to set the
color for the text.
The bCharSet variable is used to the character
set value as defined for the Win32's LOGFONT structure.
The bPitchAndFamily member variable is the same
as set for the LOGFONT structure.
The szFaceName variable is used to specify the
name of the font to apply to the text.
If you want to find out what formatting is applied on
a character or text, call the CRichEditCtrl::GetSelectionCharFormat()
method. Its syntax is:
DWORD GetSelectionCharFormat(CHARFORMAT& cf) const;
This method returns the type of dwMask of the
CHARFORMAT structure applied on the character or text.