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.

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.

Rich Edit

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.

Rich Edit

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.

Rich Edit
mfc_windows_controls.htm
Advertisements