<< Return to Main Index

< Return to Class Index

class CGXRichEditCtrl: public CGXStatic

The CGXRichEditCtrl lets you display and edit Rich Text in cells. Rich Text allows you to format individual characters or paragraphs with different fonts, colors or horizontal alignment.

CGXRichEditCtrl is only supported for MFC version 4.0 or higher.

The rich edit control determines attributes like font, horizontal alignment and text color information from the RTF string which is stored in the style value (see CGXStyle::GetValue).

In your application, you should implement toolbar buttons for outlining fonts (e.g., bold, italic, underline) or provide a menu option which lets the user format cells with the CGXStyleSheet dialog (as in RTFGrid). When the user formats cells with the Format|Cells dialog (or by pressing the “B”, “I”, “U” or other toolbar buttons), you should call CGXGridCore::SetStyleRange.

SetStyleRange calls CGXControl::StoreStyle for each cell. CGXRichEditCtrl provides its own version of StoreStyle. This method extracts font and text color information from the style object, inserts these attributes into the RTF string, and writes the RTF string back to the style object (with CGXStyle::SetValue).

This technique makes it possible for the user to format RTF cells in any way. StoreStyle even knows that only the selected text in the active RTF cell should be formatted. For example, if the user selects some text and presses the “Bold” toolbar button, only the selected text will become Bold.

The new CGXControl::LoadStyle method lets you extract style information from the current RTF cell. You can use this method to enable/disable the toolbar buttons for outlining the font. See the RTFGrid sample.

Additional features (and limitations) of the CGXRichEditCtrl are:

Style attributes which specify the appearance of the cell are:

The default id for CGXRichEditCtrl controls is GX_IDS_CTRL_RICHEDIT.

Use the CGXControl::GetControlText API if you want to determine the plain text without RTF statements from the cell and CGXControl::SetControlText if you want to specify the text for the cell without RTF statements. For example, if the RTF value is “{ Hello World! }”, GetControlText will return “Hello world!”.

RTF Statements

The easiest way to produce an RTF string is to format the text with WordPad and then open the RTF text with a text editor. You can then copy/paste the RTF string to your C++ code. Take care that you replace all ‘\’ characters with ‘\\’. Otherwise, Visual C++ will give you plenty of warnings.

Here is some information we have collected from Microsoft Developer Network on how to use RTF statements. See also “Creating Help Files” in MSDN.

RTF code always starts with

{\rtf1\ansi

Next, you must declare the names of the fonts you use in the file by using a \fonttbl statement. The \fonttbl statement, enclosed in braces, contains a list of font and family names and specifies a unique number for each font. You use these numbers with \f statements later to set specific fonts. The following \fonttbl statement assigns font numbers 0, 1, and 2 to the TrueType fonts Times New Roman, Courier New, and Arial, respectively:

{\fonttbl
\f0\froman Times New Roman;
\f1\fdecor Courier New;
\f2\fswiss Arial;}

You should also use the \deff statement to set the default font for the file. Windows Help uses this default font if no other font is specified. The following example sets the default font number to zero, corresponding to the Times New Roman font specified in the previous \fonttbl statement:

\deff0 

 If you use specific text colors or choose not to rely on the default text colors set by Windows, you must define your colors by using a \colortbl statement. The \colortbl statement, enclosed in braces, defines each color by specifying the amount of each primary color (red, green, and blue) used in it. The statement implicitly numbers the colors consecutively starting from zero. You use these color numbers with \cf statements later to set the color. The following example creates four colors (black, red, green, and blue):

{\colortbl
\red0\green0\blue0;
\red255\green0\blue0;
\red0\green128\blue0;
\red0\green0\blue255;}

Although it is not shown here, you can put a semicolon immediately after the \colortbl statement to define the default color as 0.

You can set the font name and size by using the \f and \fs statements. The name is set by using a font number specified in the \fonttbl statement. The size of the font is specified in half-points. The following example sets the text to 10-point Times New Roman (if the \fonttbl statement matches the example in Declaring Character Set, Fonts, and Colors):

\f0\fs20

\b is for bold font. \i italic font. \ul underlined font.

Once you set the font name and size, the settings apply to all subsequent text up to the next \plain statement or until you change the name or size by using the \f or \fs statement again. The \plain statement resets the name and font to the defaults. The default font name is as set by the \deff statement; the default font size is 12 points.

Horizontal alignment can be specified with \qc (centered), \qr (right aligned) and \ql (left aligned). Each paragraph is separated with \par.

Don’t forget the closing bracket at the end of the RTF string.

Example:

We want to display the following text:

10 point, Times New Roman, red, centered
14 point, Courier New, blue, left aligned
18 point, Arial, black, right aligned
Default font and color, Bold, Underline, Italic

Here is the RTF string

{\rtf1\ansi
{\fonttbl
\f0\froman Times New Roman;
\f1\fdecor Courier New;
\f2\fswiss Arial;}
\deff0
{\colortbl
\red0\green0\blue0;
\red255\green0\blue0;
\red0\green128\blue0;
\red0\green0\blue255;}
\qc\cf1\f0\fs20 10 point, Times New Roman, red, centered\plain\par
\ql\cf3\f1\fs28 14 point, Courier New, blue, left aligned\plain\par
\qr\cf0\f2\fs36 18 point, Arial, black, right aligned\plain\par
\qc Default font and color, \b Bold\plain , \ul Underline\plain , \i Italic \plain\par
}

See the example below for how to call SetValueRange.


#include <gxall.h>

Example

This example initializes a rich edit cell:

SetStyleRange(CGXRange(nRow, nCol),
   CGXStyle()
      .SetControl(GX_IDS_CTRL_RICHEDIT)
      .SetAutoSize(TRUE)
      .SetWrapText(TRUE)
      .SetAllowEnter(TRUE)
   );

SetValueRange(CGXRange(nRow, nCol),
   _T("{\\rtf1\\ansi")
   // font table
   _T("{\\fonttbl")
   _T("\\f0\\froman Times New Roman; ")
   _T("\\f1\\fdecor Courier New; ")
   _T("\\f2\\fswiss Arial;} ")
   _T("\\deff0 ")
   // color table
   _T("{\\colortbl")
   _T("\\red0\\green0\\blue0; ")
   _T("\\red255\\green0\\blue0; ")
   _T("\\red0\\green128\\blue0; ")
   _T("\\red0\\green0\\blue255;} ")
   // first line
   _T("\\qc\\cf1\\f0\\fs20 10 point, Times New Roman, ")
   _T("red, centered\\plain\\par")
   // second line
   _T("\\ql\\cf3\\f1\\fs28 14 point, Courier New, blue, ")
   _T("left aligned\\plain\\par")
   // third line
   _T("\\qr\\cf0\\f2\\fs36 18 point, Arial, black, ")
   _T("right aligned\\plain\\par")
   // fourth line
   _T("\\qc Default font and color, \\b Bold\\plain , \\ul Underline\\plain , \\i Italic \\plain\\par")
   // closing bracket
   _T("}")
);

See Also

CGXStyle::GetValue CGXGridCore::SetStyleRange CGXControl::StoreStyle CGXControl::LoadStyle CGXControl::GetControlText CGXControl::SetControlText

CGXRichEditCtrl

Class Members