A "rich edit control" is a window in which the user can enter and edit text. The text can be assigned character and paragraph formatting, and can include embedded OLE objects. Rich edit controls provide a programming interface for formatting text. However, an application must implement any user interface components necessary to make formatting operations available to the user.

This Windows Common control (and therefore the CRichEditCtrl class) is available only to programs running under Windows 95/98 and Windows NT versions 3.51 and later. The CRichEditCtrl class supports versions 2.0 and 3.0 of the Windows SDK rich edit control.

Caution

If you are using a rich edit control in a dialog box (regardless whether your application is SDI, MDI, or dialog-based), you must call [AfxInitRichEdit]--brokenlink--(../Topic/not%20found.md#afxinitrichedit) once before the dialog box is displayed. A typical place to call this function is in your program's InitInstance member function. You do not need to call it for each time you display the dialog box, only the first time. You do not have to call AfxInitRichEdit if you are working with CRichEditView.

Parameters

Return Value

The zero-based character index of the character nearest the specified point. If the specified point is beyond the last character in the control, the return value indicates the last character in the control.

Remarks

This member function works with a rich edit control. To get the information for an edit control, call CEdit::CharFromPos.

Parameters

dwStyle
Specifies the edit control's style. Apply a combination of the window styles listed in the Remarks section below, and edit control styles, described in the Windows SDK.

rect
Specifies the edit control's size and position. Can be a CRect object or RECT structure.

pParentWnd
Specifies the edit control's parent window (often a CDialog). It must not be NULL.

nID
Specifies the edit control's ID.

Return Value

Nonzero if initialization is successful; otherwise, 0.

Remarks

You construct a CRichEditCtrl object in two steps. First, call the CRichEditCtrl constructor, then call Create, which creates the Windows edit control and attaches it to the CRichEditCtrl object.

When you create a rich edit control with this function, first you must load the necessary common controls library. To load the libary, call the global function [AfxInitRichEdit]--brokenlink--(../Topic/not%20found.md#afxinitrichedit), which in turn initializes the common controls library. You need to call AfxInitRichEdit only once in your process.

When Create executes, Windows sends the WM_NCCREATE, WM_NCCALCSIZE, WM_CREATE, and WM_GETMINMAXINFO messages to the edit control.

These messages are handled by default by the OnNcCreate, OnNcCalcSize, OnCreate, and OnGetMinMaxInfo member functions in the CWnd base class. To extend the default message handling, derive a class from CRichEditCtrl, add a message map to the new class, and override the above message-handler member functions. Override OnCreate, for example, to perform needed initialization for the new class.

Example

// Set the selection to be the first occurrence of the // string lpszmyString, if it is found.
FINDTEXTEX ft;
ft.chrg.cpMin = 0;
ft.chrg.cpMax = 50;
ft.lpstrText = _T("wallaby");
long n = m_myRichEditCtrl.FindText(FR_MATCHCASE|FR_WHOLEWORD, &ft);
if (n != -1)
m_myRichEditCtrl.SetSel(ft.chrgText);

Parameters

lChar
Zero-based index of the character.

Return Value

The location of the top-left corner of the character specified by lChar.

Remarks

The character is specified by giving its zero-based index value. If lChar is greater than the index of the last character in this CRichEditCtrl object, the return value specifies the coordinates of the character position just past the last character in this CRichEditCtrl object.

DWORD GetDefaultCharFormat(CHARFORMAT& cf) const; DWORD GetDefaultCharFormat(CHARFORMAT2& cf) const; ```
### Parameters
`cf`
In the first version, a pointer to a **CHARFORMAT** structure holding the default character formatting attributes.
In the second version, a pointer to a **CHARFORMAT2** structure, which is a Rich Edit 2.0 extension to the **CHARFORMAT** structure, holding the default character formatting attributes.
### Return Value
The **dwMask** data member of `cf`. It specified the default character formatting attributes.
### Remarks
For more information, see the **EM_GETCHARFORMAT** message and the **CHARFORMAT** and **CHARFORMAT2** structures in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
See the example for [SetDefaultCharFormat](#cricheditctrl__setdefaultcharformat).
## <a name="cricheditctrl__geteventmask"></a> CRichEditCtrl::GetEventMask
Gets the event mask for this `CRichEditCtrl` object.

### Return Value
The event mask for this `CRichEditCtrl` object.
### Remarks
The event mask specifies which notification messages the `CRichEditCtrl` object sends to its parent window.
For more information, see [EM_GETEVENTMASK](http://msdn.microsoft.com/library/windows/desktop/bb788032) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
See the example for [CRichEditCtrl::SetEventMask](#cricheditctrl__seteventmask).
## <a name="cricheditctrl__getfirstvisibleline"></a> CRichEditCtrl::GetFirstVisibleLine
Determines the topmost visible line in this `CRichEditCtrl` object.

### Return Value
Zero-based index of the uppermost visible line in this `CRichEditCtrl` object.
### Remarks
For more information, see [EM_GETFIRSTVISIBLELINE](http://msdn.microsoft.com/library/windows/desktop/bb761574) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#11](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#11)]
## <a name="cricheditctrl__getiricheditole"></a> CRichEditCtrl::GetIRichEditOle
Accesses the **IRichEditOle** interface for this `CRichEditCtrl` object.

### Return Value
Pointer to the [IRichEditOle](http://msdn.microsoft.com/library/windows/desktop/bb774306) interface that can be used to access this `CRichEditCtrl` object's OLE functionality; **NULL** if the interface is not accessible.
### Remarks
Use this interface to access this `CRichEditCtrl` object's OLE functionality.
For more information, see [EM_GETOLEINTERFACE](http://msdn.microsoft.com/library/windows/desktop/bb788041) message and [IRichEditOle](http://msdn.microsoft.com/library/windows/desktop/bb774306) interface in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
## <a name="cricheditctrl__getlimittext"></a> CRichEditCtrl::GetLimitText
Gets the text limit for this `CRichEditCtrl` object.

### Return Value
The current text limit, in bytes, for this `CRichEditCtrl` object.
### Remarks
The text limit is the maximum amount of text, in bytes, the rich edit control can accept.
For more information, see [EM_GETLIMITTEXT](http://msdn.microsoft.com/library/windows/desktop/bb761582) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#12](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#12)]
## <a name="cricheditctrl__getline"></a> CRichEditCtrl::GetLine
Retrieves a line of text from this `CRichEditCtrl` object.

### Parameters
`nIndex`
Zero-based index of the line to retrieve.
`lpszBuffer`
Points to the buffer to receive the text. The first word of the buffer must specify the maximum number of bytes that can be copied into the buffer.
`nMaxLength`
Maximum number of characters that can be copied into `lpszBuffer`. The second form of `GetLine` places this value into the first word of the buffer specified by `lpszBuffer`.
### Return Value
The number of characters copied into `lpszBuffer`.
### Remarks
The copied line does not contain a terminating null character.
> [!NOTE]
> Because the first word of the buffer stores the number of characters to be copied, make sure that your buffer is at least 4 bytes long.
For more information, see [EM_GETLINE](http://msdn.microsoft.com/library/windows/desktop/bb761584) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
See the example for [GetLineCount](#cricheditctrl__getlinecount).
## <a name="cricheditctrl__getlinecount"></a> CRichEditCtrl::GetLineCount
Retrieves the number of lines in the `CRichEditCtrl` object.

### Return Value
The number of lines in this `CRichEditCtrl` object.
### Remarks
For more information, see [EM_GETLINECOUNT](http://msdn.microsoft.com/library/windows/desktop/bb761586) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#13](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#13)]
## <a name="cricheditctrl__getmodify"></a> CRichEditCtrl::GetModify
Determines if the contents of this `CRichEditCtrl` object have been modified.

### Return Value
Nonzero if the text in this `CRichEditCtrl` object has been modified; otherwise 0.
### Remarks
Windows maintains an internal flag indicating whether the contents of the rich edit control have been changed. This flag is cleared when the edit control is first created and can also be cleared by calling the [SetModify](#cricheditctrl__setmodify) member function.
For more information, see [EM_GETMODIFY](http://msdn.microsoft.com/library/windows/desktop/bb761592) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#14](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#14)]
## <a name="cricheditctrl__getoptions"></a> CRichEditCtrl::GetOptions
Retrieves the options currently set for the rich edit control.

### Return Value
A combination of the current option flag values. For a list of these values, see the *fOptions* parameter in the [EM_SETOPTIONS](http://msdn.microsoft.com/library/windows/desktop/bb774254) message, as described in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
## <a name="cricheditctrl__getparaformat"></a> CRichEditCtrl::GetParaFormat
Gets the paragraph formatting attributes of the current selection.

Example

Return Value

If successful, GetRedoName returns the UNDONAMEID enumeration type indicating the type of the next action in the control's redo queue. If the redo queue is empty, or if the redo action in the queue is of an unknown type, GetRedoName returns 0.

Remarks

The types of actions that can be undone or redone include typing, delete, drag-drop, cut, and paste operations. This information can be useful for applications that provide an extended user interface for Undo and Redo operations, such as a drop-down list box of redoable actions.

DWORD GetSelectionCharFormat(CHARFORMAT& cf) const; DWORD GetSelectionCharFormat(CHARFORMAT2& cf) const; ```
### Parameters
`cf`
In the first version, a pointer to a [CHARFORMAT](http://msdn.microsoft.com/library/windows/desktop/bb787881) structure to receive the character formatting attributes of the current selection.
In the second version, a pointer to a [CHARFORMAT2](http://msdn.microsoft.com/library/windows/desktop/bb787883) structure, which is a Rich Edit 2.0 extension to the **CHARFORMAT** structure to receive the character formatting attributes of the current selection.
### Return Value
The **dwMask** data member of `cf`. It specifies the character formatting attributes that are consistent throughout the current selection.
### Remarks
The `cf` parameter receives the attributes of the first character in the current selection. The return value specifies which attributes are consistent throughout the selection.
For more information, see the [EM_GETCHARFORMAT](http://msdn.microsoft.com/library/windows/desktop/bb788026) message and the **CHARFORMAT** and **CHARFORMAT2** structures in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
See the example for [SetSelectionCharFormat](#cricheditctrl__setselectioncharformat).
## <a name="cricheditctrl__getselectiontype"></a> CRichEditCtrl::GetSelectionType
Determines the selection type in this `CRichEditCtrl` object.

### Return Value
Flags indicating the contents of the current selection. A combination of the following flags:
- `SEL_EMPTY` Indicates that there is no current selection.
- `SEL_TEXT` Indicates that the current selection contains text.
- `SEL_OBJECT` Indicates that the current selection contains at least one OLE item.
- `SEL_MULTICHAR` Indicates that the current selection contains more than one character of text.
- `SEL_MULTIOBJECT` Indicates that the current selection contains more than one OLE object.
### Remarks
For more information, see [EM_SELECTIONTYPE](http://msdn.microsoft.com/library/windows/desktop/bb774223) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#16](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#16)]
## <a name="cricheditctrl__getseltext"></a> CRichEditCtrl::GetSelText
Retrieves the text from the current selection in this `CRichEditCtrl` object.

long GetSelText(LPSTR lpBuf) const; CString GetSelText() const; ```

Parameters

lpBuf
Pointer to the buffer to receive the text in the current selection.

Return Value

Depends on the form:

GetSelText(lpBuf) The number of characters copied into lpBuf, not including the null termination.

GetSelText( ) The string containing the current selection.

Remarks

If you use the first form, GetSelText(lpBuf), you must ensure that the buffer is large enough for the text it will receive. Call GetSel to determine the number of characters in the current selection.

Return Value

If an undo action is in the control's undo queue, GetUndoName returns the UNDONAMEID enumeration type indicating the type of the next action in the queue. If the undo queue is empty, or if the undo action in the queue is of an unknown type, GetUndoName returns 0.

Remarks

The types of actions that can be undone or redone include typing, delete, drag-drop, cut, and paste operations. This information can be useful for applications that provide an extended user interface for Undo and Redo operations, such as a drop-down list box of actions that can be undone.

Parameters

bHide
Indicates if the selection should be shown or hidden, TRUE to hide the selection.

bPerm
Indicates if this change in visibility for the selection should be permanent.

Remarks

When bPerm is TRUE, it changes the ECO_NOHIDESEL option for this CRichEditCtrl object. For a brief description of this option, see SetOptions. You can use this function to set all the options for this CRichEditCtrl object.

Parameters

nChars
Specifies the length (in bytes) of the text that the user can enter. If this parameter is 0 (the default value), the text length is set to 64K bytes.

Remarks

Changing the text limit restricts only the text the user can enter. It has no effect on any text already in the edit control, nor does it affect the length of the text copied to the edit control by the SetWindowText member function in CWnd. If an application uses the SetWindowText function to place more text into an edit control than is specified in the call to LimitText, the user can delete any of the text within the edit control. However, the text limit will prevent the user from replacing the existing text with new text, unless deleting the current selection causes the text to fall below the text limit.

Example

// Limit the number of characters to be the maximum number visible.// Get the text metrics for the edit; needed for the// average character width.
TEXTMETRIC tm;
CDC* pDC = m_myRichEditCtrl.GetDC();
pDC->GetTextMetrics(&tm);
m_myRichEditCtrl.ReleaseDC(pDC);
CRect r;
m_myRichEditCtrl.GetRect(&r);
m_myRichEditCtrl.LimitText(r.Width()/tm.tmAveCharWidth);

Parameters

nIndex
Contains the zero-based index value for the desired character in the text of the edit control, or contains –1. If nIndex is –1, it specifies the current line, that is, the line that contains the caret.

Return Value

The zero-based line number of the line containing the character index specified by nIndex. If nIndex is –1, the number of the line that contains the first character of the selection is returned. If there is no selection, the current line number is returned.

Remarks

A character index is the number of characters from the beginning of the rich edit control. For character counting, an OLE item is counted as a single character.

Parameters

nLine
Specifies the character index of a character in the line whose length is to be retrieved. If this parameter is –1, the length of the current line (the line that contains the caret) is returned, not including the length of any selected text within the line. When LineLength is called for a single-line edit control, this parameter is ignored.

Return Value

When LineLength is called for a multiple-line edit control, the return value is the length (in bytes) of the line specified by nLine. When LineLength is called for a single-line edit control, the return value is the length (in bytes) of the text in the edit control.

Remarks

Use the LineIndex member function to retrieve a character index for a given line number within this CRichEditCtrl object.

Example

Parameters

nLines
Specifies the number of lines to scroll vertically.

nChars
Specifies the number of character positions to scroll horizontally. This value is ignored if the rich edit control has either the ES_RIGHT or ES_CENTER style. Edit styles are specified in Create.

Remarks

The edit control does not scroll vertically past the last line of text in the edit control. If the current line plus the number of lines specified by nLines exceeds the total number of lines in the edit control, the value is adjusted so that the last line of the edit control is scrolled to the top of the edit-control window.

LineScroll can be used to scroll horizontally past the last character of any line.

Parameters

lpszNewText
Pointer to a null-terminated string containing the replacement text.

bCanUndo
To specify that this function can be undone, set the value of this parameter to TRUE. The default value is FALSE.

Remarks

To replace all the text in this CRichEditCtrl object, use CWnd::SetWindowText.

If there is no current selection, the replacement text is inserted at the insertion point, that is, the current caret location.

This function will format the inserted text with the existing character formatting. When replacing the entire range of text (by calling SetSel(0,-1) before calling ReplaceSel), there is an end of paragraph character that retains the previous paragraph's formatting, which in inherited by the newly inserted text.

Parameters

bEnable
Specifies whether the control is set to automatically detect a URL. If TRUE, it is enabled. If FALSE, it is disabled.

Return Value

Zero if successful, otherwise nonzero. For example, the message may fail due to insufficient memory.

Remarks

If enabled, the rich edit control will scan the text to determine if it matches a standard URL format. For a list of these URL formats, see EM_AUTOURLDETECT in the Windows SDK.

Note

Do not set SetAutoURLDetect to TRUE if your edit control uses the CFE_LINK effect for text other than URLs. SetAutoURLDetect enables this effect for URLs and disables it for all other text. See EN_LINK for more information about the CFE_LINK effect.

BOOL SetDefaultCharFormat(CHARFORMAT& cf);
BOOL SetDefaultCharFormat(CHARFORMAT2& cf);
```
### Parameters
`cf`
In the first version, a pointer to a [CHARFORMAT](http://msdn.microsoft.com/library/windows/desktop/bb787881) structure containing the new default character formatting attributes.
In the second version, a pointer to a [CHARFORMAT2](http://msdn.microsoft.com/library/windows/desktop/bb787883) structure, which is a Rich Edit 2.0 extension to the **CHARFORMAT** structure, containing the default character formatting attributes.
### Return Value
Nonzero if successful; otherwise, 0.
### Remarks
Only the attributes specified by the **dwMask** member of `cf` are changed by this function.
For more information, see the [EM_SETCHARFORMAT](http://msdn.microsoft.com/library/windows/desktop/bb774230) message and the **CHARFORMAT** and **CHARFORMAT2** structures in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#25](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#25)]
## <a name="cricheditctrl__seteventmask"></a> CRichEditCtrl::SetEventMask
Sets the event mask for this `CRichEditCtrl` object.

### Parameters
`bModified`
A value of **TRUE** indicates that the text has been modified, and a value of **FALSE** indicates it is unmodified. By default, the modified flag is set.
### Remarks
The modified flag indicates whether or not the text within the edit control has been modified. It is automatically set whenever the user changes the text. Its value can be retrieved with the [GetModify](#cricheditctrl__getmodify) member function.
For more information, see [EM_SETMODIFY](http://msdn.microsoft.com/library/windows/desktop/bb761651) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
See the example for [GetModify](#cricheditctrl__getmodify).
## <a name="cricheditctrl__setolecallback"></a> CRichEditCtrl::SetOLECallback
Gives this `CRichEditCtrl` object an **IRichEditOleCallback** object to use to access OLE-related resources and information.

### Parameters
`pCallback`
Pointer to an [IRichEditOleCallback](http://msdn.microsoft.com/library/windows/desktop/bb774308) object that this `CRichEditCtrl` object will use to get OLE-related resources and information.
### Return Value
Nonzero if successful; otherwise, 0.
### Remarks
This `CRichEditCtrl` object will call [IUnknown::AddRef](http://msdn.microsoft.com/library/windows/desktop/ms691379) to increment the usage count for the COM object specified by `pCallback`.
For more information, see [EM_SETOLECALLBACK](http://msdn.microsoft.com/library/windows/desktop/bb774252) message and [IRichEditOleCallback](http://msdn.microsoft.com/library/windows/desktop/bb774308) interface in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
## <a name="cricheditctrl__setoptions"></a> CRichEditCtrl::SetOptions
Sets the options for this `CRichEditCtrl` object.

### Parameters
*wOp*
Indicates the type of operation. One of the following values:
- `ECOOP_SET` Set the options to those specified by `dwFlags`.
- `ECOOP_OR` Combine the current options with those specified by `dwFlags`.
- `ECOOP_AND` Retain only those current options that are also specified by `dwFlags`.
- `ECOOP_XOR` Retain only those current options that are *not* specified by `dwFlags`.
`dwFlags`
Rich edit options. The flag values are listed in the Remarks section.
### Remarks
The options can be a combination of the following values:
- `ECO_AUTOWORDSELECTION` Automatic word selection on double-click.
- `ECO_AUTOVSCROLL` Automatically scrolls text to the right by 10 characters when the user types a character at the end of the line. When the user presses the ENTER key, the control scrolls all text back to position zero.
- `ECO_AUTOHSCROLL` Automatically scrolls text up one page when the user presses the ENTER key on the last line.
- `ECO_NOHIDESEL` Negates the default behavior for an edit control. The default behavior hides the selection when the control loses the input focus and shows the selection when the control receives the input focus. If you specify `ECO_NOHIDESEL`, the selected text is inverted, even if the control does not have the focus.
- `ECO_READONLY` Prevents the user from typing or editing text in the edit control.
- `ECO_WANTRETURN` Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line rich edit control in a dialog box. If you do not specify this style, pressing the ENTER key sends a command to the rich edit control's parent window, which mimics clicking the parent window's default button (for example, the OK button in a dialog box). This style has no effect on a single-line edit control.
- `ECO_SAVESEL` Preserves the selection when the control loses the focus. By default, the entire contents of the control are selected when it regains the focus.
- `ECO_VERTICAL` Draws text and objects in a vertical direction. Available for Asian languages only.
For more information, see [EM_SETOPTIONS](http://msdn.microsoft.com/library/windows/desktop/bb774254) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#27](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#27)]
## <a name="cricheditctrl__setparaformat"></a> CRichEditCtrl::SetParaFormat
Sets the paragraph formatting attributes for the current selection in this `CRichEditCtrl` object.

Parameters

lpRectCRect or pointer to a RECT that indicates the new bounds for the formatting rectangle.

Remarks

The formatting rectangle is the limiting rectangle for the text. The limiting rectangle is independent of the size of the rich edit control window. When this CRichEditCtrl object is first created, the formatting rectangle is the same size as the client area of the window. Use SetRect to make the formatting rectangle larger or smaller than the rich edit window.

Parameters

crCHARRANGE structure which holds the bounds of the current selection.

Remarks

The two forms of this function provide alternate ways to set the bounds for the selection. Brief descriptions of these forms follow:

SetSel(cr) This form uses the CHARRANGE structure with its cpMin and cpMax members to set the bounds.

SetSel(nStartChar,nEndChar) This form use the parameters nStartChar and nEndChar to set the bounds.

The caret is placed at the end of the selection indicated by the greater of the start ( cpMin or nStartChar) and end ( cpMax or nEndChar) indices. This function scrolls the contents of the CRichEditCtrl so that the caret is visible.

To select all the text in this CRichEditCtrl object, call SetSel with a start index of 0 and an end index of – 1.

Example

BOOL SetSelectionCharFormat(CHARFORMAT& cf);
BOOL SetSelectionCharFormat(CHARFORMAT2& cf);
```
### Parameters
`cf`
In the first version, a pointer to a [CHARFORMAT](http://msdn.microsoft.com/library/windows/desktop/bb787881) structure containing the new character formatting attributes for the current selection.
In the second version, a pointer to a [CHARFORMAT2](http://msdn.microsoft.com/library/windows/desktop/bb787883) structure, which is a Rich Edit 2.0 extension to the **CHARFORMAT** structure, containing the new character formatting attributes for the current selection.
### Return Value
Nonzero if successful; otherwise, 0.
### Remarks
Only the attributes specified by the **dwMask** member of `cf` are changed by this function.
For more information, see the [EM_SETCHARFORMAT](http://msdn.microsoft.com/library/windows/desktop/bb774230) and the **CHARFORMAT** and **CHARFORMAT2** structures in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#31](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#31)]
## <a name="cricheditctrl__settargetdevice"></a> CRichEditCtrl::SetTargetDevice
Sets the target device and line width used for WYSIWYG (what you see is what you get) formatting in this `CRichEditCtrl` object.

### Parameters
`hDC`
Handle to the device context for the new target device.
*lLineWidth*
Line width to use for formatting.
`dc`
[CDC](../Topic/CDC%20Class.md) for the new target device.
### Return Value
Nonzero if successful; otherwise, 0.
### Remarks
If this function is successful, the rich edit control owns the device context passed as a parameter. In that case, the calling function should not destroy the device context.
For more information, see [EM_SETTARGETDEVICE](http://msdn.microsoft.com/library/windows/desktop/bb774282) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Example
[!CODE [NVC_MFC_CRichEditCtrl#32](../CodeSnippet/VS_Snippets_Cpp/NVC_MFC_CRichEditCtrl#32)]
## <a name="cricheditctrl__settextmode"></a> CRichEditCtrl::SetTextMode
Sets the text mode or undo and redo level for a rich edit control.

### Parameters
*fMode*
Specifies the new settings for the control's text mode and undo level parameters. For a list of the possible values, see the mode parameter for [EM_SETTEXTMODE](http://msdn.microsoft.com/library/windows/desktop/bb774286) in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
### Return Value
Zero if successful, otherwise nonzero.
### Remarks
For a description of the text modes, see **EM_SETTEXTMODE** in the [!INCLUDE[winSDK](../Token/winSDK_md.md)].
This member function fails if the control contains text. To make sure the control is empty, send a [WM_SETTEXT](http://msdn.microsoft.com/library/windows/desktop/ms632644) message with an empty string.
## <a name="cricheditctrl__setundolimit"></a> CRichEditCtrl::SetUndoLimit
Sets the maximum number of actions that can stored in the undo queue.

### Parameters
*nLimit*
Specifies the maximum number of actions that can be stored in the undo queue. Set to zero to disable Undo.
### Return Value
The new maximum number of undo actions for the rich edit control.
### Remarks
By default, the maximum number of actions in the undo queue is 100. If you increase this number, there must be enough available memory to accommodate the new number. For better performance, set the limit to the smallest possible value.
## <a name="cricheditctrl__setwordcharformat"></a> CRichEditCtrl::SetWordCharFormat
Sets the character formatting attributes for the currently selected word in this `CRichEditCtrl` object.

Parameters

cf
In the first version, a pointer to a CHARFORMAT structure containing the new character formatting attributes for the currently selected word.

In the second version, a pointer to a CHARFORMAT2 structure, which is a Rich Edit 2.0 extension to the CHARFORMAT structure, containing the new character formatting attributes for the currently selected word.

Return Value

Nonzero if successful; otherwise, 0.

Remarks

Only the attributes specified by the dwMask member of cf are changed by this function.

For more information, see the EM_SETCHARFORMAT message and the CHARFORMAT and CHARFORMAT2 structures in the Windows SDK.

Parameters

nFormat
Flags specifying the input data formats. See the Remarks section for more information.

esEDITSTREAM structure specifying the input stream. See the Remarks section for more information.

Return Value

Number of characters read from the input stream.

Remarks

The value of nFormat must be one of the following:

SF_TEXT Indicates reading text only.

SF_RTF Indicates reading text and formatting.

Either of these values can be combined with SFF_SELECTION. If SFF_SELECTION is specified, StreamIn replaces the current selection with the contents of the input stream. If it is not specified, StreamIn replaces the entire contents of this CRichEditCtrl object.

In the EDITSTREAM parameter es, you specify a callback function that fills a buffer with text. This callback function is called repeatedly, until the input stream is exhausted.

Any of these values can be combined with SFF_SELECTION. If SFF_SELECTION is specified, StreamOut writes out the current selection into the output stream. If it is not specified, StreamOut writes out the entire contents of this CRichEditCtrl object.

In the EDITSTREAM parameter es, you specify a callback function which fills a buffer with text. This callback function is called repeatedly, until the output stream is exhausted.

Return Value

Nonzero if the undo operation is successful; otherwise, 0.

Remarks

An undo operation can also be undone. For example, you can restore deleted text with the first call to Undo. As long as there is no intervening edit operation, you can remove the text again with a second call to Undo.