Click here to Skip to main content
6,635,160 members and growing! (20,022 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » General     Intermediate License: The Code Project Open License (CPOL)

CAutoRichEditCtrl - automate rich edit formatting and RTF handling

By Jeremy Iverson

A CRichEditCtrl derived class to handle formatting quickly.
VC6, MFC, Dev
Posted:19 Nov 1999
Updated:11 Feb 2003
Views:183,334
Bookmarked:63 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
64 votes for this article.
Popularity: 8.13 Rating: 4.50 out of 5

1
1 vote, 5.0%
2
3 votes, 15.0%
3
4 votes, 20.0%
4
12 votes, 60.0%
5
  • Download demo project - 40 Kb
  • Download source - 6 Kb
  • 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!

    License

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

    About the Author

    Jeremy Iverson


    Member

    Occupation: Web Developer
    Location: United States United States

    Other popular Edit Controls articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
    GeneralUnicode suported?? PinmemberArrin0:17 30 Oct '09  
    GeneralCan it work with UNICODE [modified] PinmemberRandyS5:17 12 Feb '07  
    Generaltables / tab positions Pinmemberandrewtruckle11:57 28 Apr '06  
    GeneralDLL Pinmemberrdba7:59 9 Mar '04  
    GeneralProblem in win98 PinmemberCodeStylite16:50 29 Jan '04  
    Generalwho can tell me how to set image as richedit background? Pinmemberokboy5:42 26 Dec '03  
    GeneralCatching Tab Presses PinmemberFranklin57751:43 11 Dec '03  
    GeneralRe: Catching Tab Presses PinmemberJason Morris19:18 20 Dec '04  
    GeneralCAutoRichEditCtrl and OLE objects PinsussMilen Manev23:43 9 Nov '03  
    Generalto remove white space after "." Pinmemberramya8323:17 14 Oct '03  
    GeneralAdding Text In Design Time Pinmemberorb6k15:22 3 Oct '03  
    Generalabc PinsussAnonymous19:48 26 Aug '03  
    Generalstarange error PinmemberIrek Zielinski2:30 14 Aug '03  
    GeneralRe: starange error Pinmembernaveed8:06 21 Jan '04  
    GeneralRe: starange error <Bug with Bulleting> Pinsussp. Kimmel13:22 8 May '04  
    Generalis it posible by u?... PinmemberBalaji20005:22 28 May '03  
    Generalevaluation of fields in RTF files Pinmemberluedi0:07 29 Apr '03  
    GeneralAuthor information and bitmaps PinmemberOle Setnes3:49 19 Feb '03  
    General12 Feb 03 update PinmemberNicolas Bonamy3:11 13 Feb '03  
    GeneralRe: 12 Feb 03 update PinmemberJeremy Iverson4:19 13 Feb '03  
    GeneralBug in SetFontName PinsussAnonymous3:11 30 Aug '02  
    GeneralRe: Bug in SetFontName Pinmembernostradammit8:55 13 Apr '06  
    GeneralRe: Bug in SetFontName Pinmemberandrewtruckle11:56 28 Apr '06  
    GeneralRe: Bug in SetFontName Pinmembernostradammit17:14 28 Apr '06  
    GeneralRichedit2.0(3.0) Clipboard Operations PinmemberJaiWalia12:00 16 Jul '02  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 11 Feb 2003
    Editor: Nick Parker
    Copyright 1999 by Jeremy Iverson
    Everything else Copyright © CodeProject, 1999-2009
    Web18 | Advertise on the Code Project