CAutoRichEditCtrl - automate rich edit formatting and RTF handling

While MFC does provide a rich edit control, it does not provide a quick and easy way to handle formatting and RTF codes. CAutoRichEditCtrl is a derivative of CRichEditCtrl. This new class adds several functions that allow you to add formatting quickly. It also provides two functions to let you retrieve the formatted text (RTF) in a CString, and to insert an RTF CString into the control.

To use CAutoRichEditCtrl in a dialog box, follow these instructions.

  1. Insert a normal rich edit control in your dialog box.
  2. Using ClassWizard, attach a control variable of type CRichEditCtrl to your control.
  3. Copy AutoRichEditCtrl.cpp and AutoRichEditCtrl.h (download below) to your source directory, and #include "AutoRichEditCtrl.h" in your dialog's header.
  4. In your dialog's header, change CRichEditCtrl to CAutoRichEditCtrl.
  5. In your application's InitInstance (CMyApp::InitInstance()), include a call to AfxInitRichEdit() - this is needed for all rich edit controls.
  6. Add interface elements that use CAutoRichEditCtrls's functions - see below.

Once you've inserted the control into your project, you can add toolbar buttons, menu commands, or other elements to call the following functions:

  • GetRTF() - returns a CString holding the text of the control (including the RTF formatting code).
  • SetRTF(CString sRTF) - parameter sRTF is an RTF formatted CString. Calling this will insert and format the string. If a CString is passed that does not have valid RTF code, the control will be cleared.

  • SelectionIsBold() - returns true if the current selection is bold, otherwise false.
  • SelectionIsItalic()- returns true if the current selection is italic, else false.
  • SelectionIsUnderlined() - returns true if the current selection is underlined, else false.

  • SetSelectionBold() - sets the current selection to be bold (Note: this will actually act like pressing the Bold toolbar button in Wordpad - if the text is not bold, it will become bold; if the text is already bold, it will remove the bold formatting; if part of the selection is bold, the entire selection will become bold; if there is not anything selected, the format is changed to bold so when the user starts typing, it will be in bold).
  • SetSelectionItalic() - sets the current selection to italic (see note in SetSelectionBold()).
  • SetSelectionUnderlined() - sets the current selection to underlined (see note in SetSelectionBold()).

  • SetParagraphCenter() - sets the current paragraph to be centered.
  • SetParagraphLeft() - sets the current paragraph to be left justified.
  • SetParagraphRight() - sets the current paragraph to be right justified.

  • ParagraphIsCentered() - returns true if the current paragraph is centered, else false.
  • ParagraphIsLeft() - returns true if the current paragraph is left justified, else false.
  • ParagraphIsRight() - returns true if the current paragraph is right justified, else false.

  • SetParagraphBulleted() - sets the bullet style (see note in SetSelectionBold()).
  • ParagraphIsBulleted() - returns true if the current selection is bulleted, else false.

  • SelectColor() - displays a color selection dialog box and sets the selection to the chosen color (if OK is pressed).

  • SetFontName(CString sFontName) - sets the selected font to sFontName.
  • SetFontSize(int nPointSize) - sets the selection to nPointSize.

  • GetSelectionFontName() - returns a CString holding the selection's font name.
  • GetSelectionFontSize() - returns a long holding the selection's font size.

  • GetSystemFonts(CStringArray &saFontList) - saFontList will hold an array of all true-type fonts available on the system.

  • GetCharFormat() - returns the CHARFORMAT structure of the current selection.

To see examples of these functions, download the demo app. The source code that shows examples of how to use each function is located in CRichEdDlg::OnExecute().

A quick example here: if you want a rich edit control in a dialog to use formatting, you may create a toolbar with the typical bold, italic, and underline buttons. Just use ClassWizard to add a function for when the user presses the button (like OnBold), then call the right function (like m_richedit.SetSelectionBold();, assuming your rich edit control is named m_richedit). That's all it takes to set up the formatting correctly.

When you want to save the text, just do a CString sText = m_richedit.GetRTF();, then save the string however you like - when you call m_richedit.SetRTF(sText) with the same string, the formatting will be just like it was when you saved it.

Two thank you's are in order for Zafir Anjum and Girish Bharadwaj. The code that streams the RTF string into the control was taken from Zafir's article, Inserting an RTF string using StreamIn. The code that retrieves all the fonts on the system was derived from Girish's article, Owner Drawn Font Selection ComboBox.

While this source code is Copyright 1999 me, feel free to use/modify it for whatever you like. If you do make any modifications or additions, I would appreciate it you let me know (or let everybody know by posting a comment). Also, if you use this in any application, let me know (just for my own curiosity =) ). Of course, this control is provided as is, and no warranty is expressed or implied as to its fitness for any particular purpose.

Hope this helps you out!

Download demo project - 56 KB

Download source - 6 KB



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

Most Popular Programming Stories

More for Developers

RSS Feeds