How to Format Text in Rich Edit Controls

An application can send messages to a rich edit control in order to format characters and paragraphs and retrieve formatting information. Paragraph formatting attributes include alignment, tabs, indents, numbering, and simple tables. For characters, you can specify font name, size, color, and effects such as bold, italic, and protected.

What you need to know

Technologies

Prerequisites

  • C/C++
  • Windows User Interface Programming

Instructions

Format Text in a Rich Edit Control

You can apply paragraph formatting by using the EM_SETPARAFORMAT message. To determine the current paragraph formatting for the selected text, use the EM_GETPARAFORMAT message. The PARAFORMAT or PARAFORMAT2 structure is used with both messages to specify paragraph formatting attributes.

You can apply character formatting by using the EM_SETCHARFORMAT message. To determine the current character formatting for the selected text, you can use the EM_GETCHARFORMAT message. The CHARFORMAT or CHARFORMAT2 structure is used with both messages to specify character attributes.

You can also use EM_SETCHARFORMAT and EM_GETCHARFORMAT messages to set and retrieve the character formatting of the insertion point, which is the formatting that is applied to any subsequently inserted characters. For example, if an application sets the default character formatting to bold and the user then types a character, that character is bold.

The character formatting of the insertion point is applied to newly inserted text only if the current selection is empty (if the current selection is an insertion point). Otherwise, the new text assumes the character formatting of the text it replaces. If the selection changes, the default character formatting changes to match the first character in the new selection.

The protected character effect is unique in that it does not change the appearance of text. If the user attempts to modify protected text, a rich edit control sends its parent window an EN_PROTECTED notification code, allowing the parent window to allow or prevent the change. To receive this notification code, you must enable it by using the EM_SETEVENTMASK message.

Foreground color is always a character attribute. In Microsoft Rich Edit 1.0, background color is only a property of the rich edit control. To set the default background color, use the EM_SETBKGNDCOLOR message. Note that Rich Edit does not support the WM_CTLCOLOREDIT message.

Using Rich Edit Controls

Windows common controls demo (CppWindowsCommonControls)