Saturday, 9 March 2013

Initializing the Rich Edit Control


Initializing the Rich Edit Control

The rich edit control is perfectly usable as soon as it is created. Member functions
manipulate the control extensively, formatting and selecting text, enabling and
disabling many control features, and more. As always, check your online documentation
for all the details on these member functions.
Manipulating the Rich Edit Control
This sample application shows you the basics of using the rich edit control by setting
character attributes and paragraph formats. When you include a rich edit control in an
application, you will probably want to give the user some control over its contents. For
this reason, you usually create menu and toolbar commands for selecting the various
options that you want to support in the application. In Common, the user can click four

buttons to control the rich edit control.
You've already added the code to create these buttons. Add lines to the message map in
the header file to declare the handlers:
afx_msg void OnULine();
afx_msg void OnLeft();
afx_msg void OnCenter();
afx_msg void OnRight();
Similarly, add these lines to the message map in the source file:
ON_COMMAND(IDC_RICHEDIT_ULINE, OnULine)
ON_COMMAND(IDC_RICHEDIT_LEFT, OnLeft)
ON_COMMAND(IDC_RICHEDIT_CENTER, OnCenter)
ON_COMMAND(IDC_RICHEDIT_RIGHT, OnRight)
Each of these functions is simple. Add them each to CommonView.cpp. OnULine() looks
like this:
void CCommonView::OnULine()
{
CHARFORMAT charFormat;
charFormat.cbSize = sizeof(CHARFORMAT);
charFormat.dwMask = CFM_UNDERLINE;
m_richEdit.GetSelectionCharFormat(charFormat);
if (charFormat.dwEffects & CFM_UNDERLINE)
charFormat.dwEffects = 0;
else
charFormat.dwEffects = CFE_UNDERLINE;
m_richEdit.SetSelectionCharFormat(charFormat);
m_richEdit.SetFocus();
}
OnULine() creates and initializes a CHARFORMAT structure, which holds information
about character formatting and is declared in Listing 10.11.
Listing 10.11 The CHARFORMAT Structure, Defined by MFC
typedef struct _charformat
{
UINT cbSize;
_WPAD _wPad1;
DWORD dwMask;
DWORD dwEffects;
LONG yHeight;
LONG yOffset;
COLORREF crTextColor;
BYTE bCharSet;

BYTE bPitchAndFamily;
TCHAR szFaceName[LF_FACESIZE];
_WPAD _wPad2;
} CHARFORMAT;
In a CHARFORMAT structure, cbSize is the size of the structure. dwMask indicates
which members of the structure are valid (can be a combination of CFM_BOLD,
CFM_CHARSET, CFM_COLOR, CFM_FACE, CFM_ITALIC, CFM_OFFSET,
CFM_PROTECTED, CFM_SIZE, CFM_STRIKEOUT, and CFM_UNDERLINE). dwEffects is
the character effects (can be a combination of CFE_AUTOCOLOR, CFE_BOLD,
CFE_ITALIC, CFE_STRIKEOUT, CFE_UNDERLINE, and CFE_PROTECTED). yHeight is the
character height, and yOffset is the character baseline offset (for super- and subscript
characters). crTextColor is the text color. bCharSet is the character set value (see the
ifCharSet member of the LOGFONT structure). bPitchAndFamily is the font pitch and
family, and szFaceName is the font name.
After initializing the CHARFORMAT structure, as needed, to toggle underlining,
OnULine() calls the control's GetSelectionCharFormat() member function. This
function, whose single argument is a reference to the CHARFORMAT structure, fills
the character format structure. OnULine() checks the dwEffects member of the
structure to determine whether to turn underlining on or off. The bitwise and
operator, &, is used to test a single bit of the variable.
Finally, after setting the character format, OnULine() returns the focus to the rich
edit control. By clicking a button, the user has removed the focus from the rich edit
control. You don't want to force the user to keep switching back manually to the
control after every button click, so you do it by calling the control's SetFocus()
member function.
Common also enables the user to switch between the three types of paragraph
alignment. This is accomplished similarly to toggling character formats. Listing 10.12
shows the three functions - OnLeft(), OnRight(), and OnCenter()--that handle the
alignment commands. Add the code for these functions to CommonView.cpp. As you can
see, the main difference is the use of the PARAFORMAT structure instead of
CHARFORMAT and the call to SetParaFormat() instead of SetSelectionCharFormat().
Listing 10.12 CommonView.cpp - Changing Paragraph Formats
void CCommonView::OnLeft()
{
PARAFORMAT paraFormat;
paraFormat.cbSize = sizeof(PARAFORMAT);
paraFormat.dwMask = PFM_ALIGNMENT;
paraFormat.wAlignment = PFA_LEFT;
m_richEdit.SetParaFormat(paraFormat);
m_richEdit.SetFocus();

}
void CCommonView::OnCenter()
{
PARAFORMAT paraFormat;
paraFormat.cbSize = sizeof(PARAFORMAT);
paraFormat.dwMask = PFM_ALIGNMENT;
paraFormat.wAlignment = PFA_CENTER;
m_richEdit.SetParaFormat(paraFormat);
m_richEdit.SetFocus();
}
void CCommonView::OnRight()
{
PARAFORMAT paraFormat;
paraFormat.cbSize = sizeof(PARAFORMAT);
paraFormat.dwMask = PFM_ALIGNMENT;
paraFormat.wAlignment = PFA_RIGHT;
m_richEdit.SetParaFormat(paraFormat);
m_richEdit.SetFocus();
}
After adding all that code, it's time to build and test again. First, click in the text box
to give it the focus. Then, start typing. Want to try out character attributes? Click the
ULine button to add underlining to either selected text or the next text you type. To
try out paragraph formatting, click the Left, Center, or Right button to specify
paragraph alignment. (Again, if you're using large text, adjust the button size if the
labels don't fit.) Figure 10.9 shows the rich edit control with some different character
and paragraph styles used.
FIG. 10.9 A rich edit control is almost a complete word processor.



No comments:

Post a Comment