MFC - Rich Edit

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. It is represented by CRichEditCtrl class.
CanPaste
Determines if the contents of the Clipboard can be pasted into this rich edit control.
CanRedo
Determines whether there are any actions in the controls redo queue.
CanUndo
Determines if an editing operation can be undone.
CharFromPos
Retrieves information about the character closest to a specified point in the client area of an edit control.
Clear
Clears the current selection.
Copy
Copies the current selection to the Clipboard.
Create
Creates the Windows rich edit control and associates it with this CRichEditCtrl object.
CreateEx
Creates the Windows rich edit control with the specified extended Windows styles and associates it with this CRichEditCtrl object.
Cut
Cuts the current selection to the Clipboard.
DisplayBand
Displays a portion of the contents of this CRichEditCtrl object.
EmptyUndoBuffer
Resets (clears) the undo flag of this CRichEditCtrl object.
FindText
Locates text within this CRichEditCtrl object.
FindWordBreak
Finds the next word break before or after the specified character position, or retrieves information about the character at that position.
FormatRange
Formats a range of text for the target output device.
GetCharPos
Determines the location of a given character within this CRichEditCtrl object.
GetDefaultCharFormat
Retrieves the current default character formatting attributes in this CRichEditCtrl object.
GetEventMask
Retrieves the event mask for this CRichEditCtrl object.
GetFirstVisibleLine
Determines the topmost visible line in this CRichEditCtrl object.
GetIRichEditOle
Retrieves a pointer to the IRichEditOle interface for this rich edit control.
GetLimitText
Gets the limit on the amount of text a user can enter into this CRichEditCtrl object.
GetLine
Retrieves a line of text from this CRichEditCtrl object.
GetLineCount
Retrieves the number of lines in this CRichEditCtrl object.
GetModify
Determines if the contents of this CRichEditCtrl object have changed since the last save.
GetOptions
Retrieves the rich edit control options.
GetParaFormat
Retrieves the paragraph formatting attributes in the current selection in this CRichEditCtrl object.
GetPunctuation
Retrieves the current punctuation characters for the rich edit control. This message is available only in Asianlanguage versions of the operating system.
GetRect
Retrieves the formatting rectangle for this CRichEditCtrl object.
GetRedoName
Retrieves the type of the next action, if any, in the control's redo queue.
GetSel
Gets the starting and ending positions of the current selection in this CRichEditCtrl object.
GetSelectionCharFormat
Retrieves the character formatting attributes in the current selection in this CRichEditCtrl object.
GetSelectionType
Retrieves the type of contents in the current selection in this CRichEditCtrl object.
GetSelText
Gets the text of the current selection in this CRichEditCtrl object.
GetTextLength
Retrieves the length of the text, in characters, in this CRichEditCtrl object. Does not include the terminating null character.
GetTextLengthEx
Retrieves the number of characters or bytes in the rich edit view. Accepts a list of flags to indicate the method of determining length of the text in a rich edit control.
GetTextMode
Retrieves the current text mode and undo level of a rich edit control.
GetTextRange
Retrieves the specified range of text.
GetUndoName
Retrieves the type of the next undo action, if any.
GetWordWrapMode
Retrieves the current word wrapping and word breaking options for the rich edit control. This message is available only in Asian-language versions of the operating system.
HideSelection
Shows or hides the current selection.
LimitText
Limits the amount of text a user can enter into the CRichEditCtrl object.
LineFromChar
Determines which line contains the given character.
LineIndex
Retrieves the character index of a given line in this CRichEditCtrl object.
LineLength
Retrieves the length of a given line in this CRichEditCtrl object.
LineScroll
Scrolls the text in this CRichEditCtrl object.
Paste
Inserts the contents of the Clipboard into this rich edit control.
PasteSpecial
Inserts the contents of the Clipboard into this rich edit control in the specified data format.

PosFromChar
Retrieves the client area coordinates of a specified character in an edit control.
Redo
Redoes the next action in the control's redo queue.
ReplaceSel
Replaces the current selection in this CRichEditCtrl object with specified text.
RequestResize
Forces this CRichEditCtrl object to send request resize notifications.
SetAutoURLDetect
Indicates if the auto URL detection is active in a rich edit control.
SetBackgroundColor
Sets the background color in this CRichEditCtrl object.
SetDefaultCharFormat
Sets the current default character formatting attributes in this CRichEditCtrl object.
SetEventMask
Sets the event mask for this CRichEditCtrl object.
SetModify
Sets or clears the modification flag for this CRichEditCtrl object.
SetOLECallback
Sets the IRichEditOleCallback COM object for this rich edit control.
SetOptions
Sets the options for this CRichEditCtrl object.
SetParaFormat
Sets the paragraph formatting attributes in the current selection in this CRichEditCtrl object.
SetPunctuation
Sets the punctuation characters for a rich edit control. This message is available only in Asianlanguage versions of the operating system.
SetReadOnly
Sets the read-only option for this CRichEditCtrl object.
SetRect
Sets the formatting rectangle for this CRichEditCtrl object.
SetSel
Sets the selection in this CRichEditCtrl object.
SetSelectionCharFormat
Sets the character formatting attributes in the current selection in this CRichEditCtrl object.
SetTargetDevice
Sets the target output device for this CRichEditCtrl object.
SetTextMode
Sets the text mode or undo level of a rich edit control. The message fails if the control contains text.
SetUndoLimit
Sets the maximum number of actions that can stored in the undo queue.
SetWordCharFormat
Sets the character formatting attributes in the current word in this CRichEditCtrl object.
SetWordWrapMode
Sets the word-wrapping and word-breaking options for the rich edit control. This message is available only in Asianlanguage versions of the operating system.
StopGroupTyping
Stops the control from collecting additional typing actions into the current undo action. The control stores the next typing action, if any, into a new action in the undo queue.
StreamIn
Inserts text from an input stream into this CRichEditCtrl object
StreamOut
Stores text from this CRichEditCtrl object into an output stream.
Undo
Reverses the last editing operation.
Let us into a simple example by creating a new MFC dialog based application.
Step 1 − Delete the TODO line and drag one Rich Edit Control and three buttons from the Toolbox.
Step 2 − Change the Caption of these three buttons from Bold, Italic and Under Line to IDC_BUTTON_BOLD, IDC_BUTTON_ITALIC and IDC_BUTTON_UNDERLINE respectively.
Step 3 − Set the following properties to True: Multiline, Want Return, Vertical Scroll.
Step 4 − Add the control variable m_richEdit for Rich Edit Control.
Step 5 − Go to the CMFCRichEditApp and call the ::AfxInitRichEdit2() in CMFCRichEditApp::InitInstance() function as shown in the following code.
BOOL CMFCRichEditApp::InitInstance() {
//TODO: call AfxInitRichEdit2() to initialize richedit2 library.
   // InitCommonControlsEx() is required on Windows XP if an application
   // manifest specifies use of ComCtl32.dll version 6 or later to enable
   // visual styles. Otherwise, any window creation will fail.
   INITCOMMONCONTROLSEX InitCtrls;
   InitCtrls.dwSize = sizeof(InitCtrls);
   // Set this to include all the common control classes you want to use
   // in your application.
   InitCtrls.dwICC = ICC_WIN95_CLASSES;
   InitCommonControlsEx(&InitCtrls);

   ::AfxInitRichEdit2();
   CWinApp::InitInstance();

   
   AfxEnableControlContainer();
   
   // Create the shell manager, in case the dialog contains
   // any shell tree view or shell list view controls.
   CShellManager *pShellManager = new CShellManager;

   // Activate "Windows Native" visual manager for enabling themes in MFC controls
   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

   // Standard initialization
   // If you are not using these features and wish to reduce the size
   // of your final executable, you should remove from the following
   // the specific initialization routines you do not need
   // Change the registry key under which our settings are stored
   // TODO: You should modify this string to be something appropriate
   // such as the name of your company or organization
   SetRegistryKey(_T("Local AppWizard-Generated Applications"));

   CMFCRichEditDlg dlg;
   m_pMainWnd = &dlg;
   INT_PTR nResponse = dlg.DoModal();
   if (nResponse == IDOK) {
      // TODO: Place code here to handle when the dialog is
      // dismissed with OK
   }else if (nResponse == IDCANCEL) {
      // TODO: Place code here to handle when the dialog is
      // dismissed with Cancel
   }else if (nResponse == -1) {
      TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so
                                application is terminating unexpectedly.\n");
      TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on
              the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
   }
	
   // Delete the shell manager created above.
   if (pShellManager != NULL) {
      delete pShellManager;
   }
	
   // Since the dialog has been closed, return FALSE so that we exit the
   // application, rather than start the application's message pump.
   return FALSE;
}
Step 6 − Add the Click event handler for all the three buttons. Here is the implementation for these events.
void CMFCRichEditDlg::OnBnClickedButtonBold() { 
   // TODO: Add your control notification handler code here 
   CHARFORMAT Cfm;
	
   m_richEdit.GetSelectionCharFormat(Cfm);  
	
   Cfm.cbSize = sizeof(CHARFORMAT); 
   Cfm.dwMask = CFM_BOLD; 
   Cfm.dwEffects ^= CFE_BOLD; 
	
   m_richEdit.SetSelectionCharFormat(Cfm); 
   m_richEdit.SetFocus(); 
}
  
void CMFCRichEditDlg::OnBnClickedButtonItalic() { 
   // TODO: Add your control notification handler code here
   CHARFORMAT Cfm;  
   
   m_richEdit.GetSelectionCharFormat(Cfm);  
	
   Cfm.cbSize = sizeof(CHARFORMAT); 
   Cfm.dwMask = CFM_ITALIC; 
   Cfm.dwEffects ^= CFE_ITALIC;  
	
   m_richEdit.SetSelectionCharFormat(Cfm); 
   m_richEdit.SetFocus(); 
}
  
void CMFCRichEditDlg::OnBnClickedButtonUnderline() { 
   // TODO: Add your control notification handler code here 
   CHARFORMAT Cfm;  
	
   m_richEdit.GetSelectionCharFormat(Cfm); 
	
   Cfm.cbSize = sizeof(CHARFORMAT); 
   Cfm.dwMask = CFM_UNDERLINE; 
   Cfm.dwEffects ^= CFE_UNDERLINE;  
	
   m_richEdit.SetSelectionCharFormat(Cfm); 
   m_richEdit.SetFocus(); 
} 
Step 7 − When the above code is compiled and executed, you will see the following output. Now enter text and change its formatting by selecting the text and then click on any of the three buttons.
 Print
 Add Notes
 Bookmark this page