Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 16: Text-Based Controls Visual C++ and MFC Fundamentals



  1. Add a Control variable (of type CRichEditCtrl) to the rich edit control and name it
    m_Richer


16.3.3..Rich Edit Properties............................................................................


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.
Free download pdf