Click here to Skip to main content
11,411,840 members (46,512 online)
Click here to Skip to main content

CAutoRichEditCtrl - automate rich edit formatting and RTF handling

, 11 Feb 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
A CRichEditCtrl derived class to handle formatting quickly.
  • Download demo project - 40 Kb
  • Download source - 6 Kb
  • <!-- Article Starts -->

    <!-- The 'p' starts a paragraph of normal text --> 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.

    You are free to use/modify this source code for whatever you like. If you do make any modifications or additions, I would appreciate it you let me know (or better, let everybody know by posting a comment). 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!


    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    About the Author

    Jeremy Iverson
    Software Developer
    United States United States
    No Biography provided

    Comments and Discussions

    QuestionGood Sample ! Pinmemberwy73729-Oct-13 19:55 
    QuestionIs is possible to set default font size colour etc to an edit box PinmemberCandleman30-Oct-12 23:08 
    QuestionCAutorichedit control how to set defaults for edit box using CAutoRichEdit PinmemberCandleman13-Oct-12 21:55 
    GeneralMy vote of 5 PinmemberCTI Guru19-Apr-11 12:09 
    Worked perfectly right out of the gate.
    GeneralThank you! PinmemberCTI Guru19-Apr-11 12:07 
    Generalhi Pinmembertimidfool16-Sep-10 23:13 
    QuestionUnicode suported?? PinmemberArrin30-Oct-09 0:17 
    QuestionCan it work with UNICODE [modified] PinmemberRandyS12-Feb-07 5:17 
    Generaltables / tab positions Pinmemberandrewtruckle28-Apr-06 11:57 
    GeneralDLL Pinmemberrdba9-Mar-04 7:59 
    GeneralProblem in win98 PinmemberCodeStylite29-Jan-04 16:50 
    Questionwho can tell me how to set image as richedit background? Pinmemberokboy26-Dec-03 5:42 
    GeneralCatching Tab Presses PinmemberFranklin577511-Dec-03 1:43 
    GeneralRe: Catching Tab Presses PinmemberJason Morris20-Dec-04 19:18 
    GeneralCAutoRichEditCtrl and OLE objects PinsussMilen Manev9-Nov-03 23:43 
    Generalto remove white space after &quot;.&quot; Pinmemberramya8314-Oct-03 23:17 
    GeneralAdding Text In Design Time Pinmemberorb6k3-Oct-03 15:22 
    Generalabc PinsussAnonymous26-Aug-03 19:48 
    Generalstarange error PinmemberIrek Zielinski14-Aug-03 2:30 
    GeneralRe: starange error <Bug with Bulleting> Pinmembernaveed21-Jan-04 8:06 
    GeneralRe: starange error &lt;Bug with Bulleting&gt; Pinsussp. Kimmel8-May-04 13:22 
    Generalis it posible by u?... PinmemberBalaji200028-May-03 5:22 
    Generalevaluation of fields in RTF files Pinmemberluedi29-Apr-03 0:07 
    GeneralAuthor information and bitmaps PinmemberOle Setnes19-Feb-03 3:49 
    General12 Feb 03 update PinmemberNicolas Bonamy13-Feb-03 3:11 
    GeneralRe: 12 Feb 03 update PinmemberJeremy Iverson13-Feb-03 4:19 
    GeneralBug in SetFontName PinsussAnonymous30-Aug-02 3:11 
    GeneralRe: Bug in SetFontName Pinmembernostradammit13-Apr-06 8:55 
    GeneralRe: Bug in SetFontName Pinmemberandrewtruckle28-Apr-06 11:56 
    GeneralRe: Bug in SetFontName Pinmembernostradammit28-Apr-06 17:14 
    GeneralRichedit2.0(3.0) Clipboard Operations PinmemberJaiWalia16-Jul-02 12:00 
    GeneralPrinting Pinmembernaveed16-Jul-02 11:25 
    GeneralRe: Printing: With ScrollView PinmemberBalaji200028-May-03 6:46 
    Generalproblem with NT 4 and AutoRichEdit PinmemberAnonymous20-Mar-02 13:03 
    GeneralRe: problem with NT 4 and AutoRichEdit PinsussAnonymous30-Aug-02 2:18 
    QuestionCatch tabs?? PinmemberAnonymous25-Feb-02 12:36 
    AnswerRe: Catch tabs?? Pinmembersrikanthvarma27-Feb-03 0:11 
    QuestionHow to format Spacing in RichEdit2.0? Help me please! PinmemberMila24-Dec-01 16:59 
    GeneralVery nice idea and implementation but... PinmemberVladimir L.15-Dec-01 10:37 
    GeneralAfxInitRichEdit ! Pinmembermazy21-Oct-01 10:05 
    GeneralRe: AfxInitRichEdit ! Pinmemberqwerty20014-Dec-01 21:50 
    GeneralAutoHScroll PinmemberFrunza Adriana11-Oct-01 21:37 
    GeneralSetFontName(CString sFontName) ... bug PinmemberAlfonso Bastias4-Oct-01 14:28 
    GeneralRe: SetFontName(CString sFontName) ... bug PinsussAnonymous25-Sep-02 20:24 
    GeneralDear author PinmemberManikandan5-Jun-01 2:14 
    QuestionGetting it as normal text? PinmemberJohannes15-May-01 2:53 
    AnswerRe: Getting it as normal text? PinmemberPeter SJöström11-Jan-02 10:44 
    GeneralMFC AfxInitRichEdit PinmemberKevin Cao14-Nov-00 0:28 
    GeneralRe: MFC AfxInitRichEdit PinsussAnonymous22-Jul-03 5:51 
    QuestionWTL version? PinsussScott Leonard29-Aug-00 6:36 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.150414.5 | Last Updated 12 Feb 2003
    Article Copyright 1999 by Jeremy Iverson
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid