RTF File Handling

Environment: Visual C++ 6, richedit controls


I needed a dialog box app based on the RichEdit control. I searched over the Net and I found Jeremy Iverson's article. I downloaded the code and studied it. It was not what I needed, but it was a good starting point.

So, my app is a kind of an RTF word processor. It has menus, toolbar, and a status bar. You can print the file, open, and save a file. You can choose any fonts you want from the Edit, Format Font menu. If you start to close a file and you haven't saved it, the program asks whethe you want to save the file. The program has tooltips. If text is selected, the Cut and Copy buttons are enabled; the program updates the message from statusbar. On the title bar, the opened file's path appears. If you want to save your file with the name of an existing one, my program asks whether you want to overwrite the file. My program has an accelerator table taken from MSDN sample Mfccalc(CCalcDlg::CCalcDlg(...)). The code from OnSize is patially taken from the MSDN sample Ftptree:CFTPTREEDlg::OnSize(...). I have limited the text to 100,000,000 characters.

CMyRichEditCtrl is derivated from the CRichEditCtrl class. This class has the following functions:

Code to Use Produces This Effect
void SetRtf(CFile* pInputFile );
Inserts the file's content. This function is taken from Jeremy Iverson's article and modified.
void SetSelectionBold();
Sets the selection to bold
void SetSelectionItalic();
Sets the selection to italic
void SetSelectionUnderline();
Sets the selection to be underlined
void SetColour();
Displays a color selection dialog box and sets the selection to the chosen color (if OK is pressed).
void SetColor(COLORREF clr);
Sets the CFontDialog color box
void SetParagraphLeft();
Sets the current paragraph to be left aligned
void SetParagraphRight();
sets the current paragraph to be right aligned
void SetParagraphCenter();
Sets the current paragraph to be centered
void SetParagraphBulleted();
Sets the current paragraph to be bulleted. This function is taken from Jeremy Iverson's article.
void SetFontName(CString sFontName);
Sets the font name. This function is taken from Jeremy Iverson's article.
void SetFontSize(int nFontSize);
Sets the font size. This function is taken from Jeremy Iverson's article.

Here's how to use this control in a dialog :

  • Include CMyRichEditCtrl.cpp and CMyRichEditCtrl.h in your project.
  • In the resource editor, create a rich edit control with the following styles: Multiline, Vertical scroll, AutoVScroll, No hide selection, Want return, Border.
  • Create a control member variable for this rich edit control in VC classwizard.
  • In CYourApp::InitInstance()), include a call to AfxInitRichEdit(); otherwise, you won't see anything.

I have learned a lot while making this program. I needed a toolbar and status bar in my app and I have taken the code from an MSDN sample, dlgcbr32. What I haven't done is to create menu with icons because I don't know how to do it. Maybe you will succeed and let me know.

My program was compiled with Visual C++ 6, under Windows 95.

You know the rule: You are free to use and modify this source code. If you make any enhancements, I would appreciate if you let me know. Of course, this control is provided as is, and no warranty is expressed or implied.

Happy programming!


Download source - 48 Kb


  • I need your help

    Posted by allooba on 11/12/2007 05:48am

    I tried to create rich text program using VC++. but I need when user press some keys in keyboard, I must show "dots" in screen. can you help me how to Handel the input from keyboard please. thanks. my email: alloob2@hotmail.com

  • Little Problem

    Posted by Legacy on 09/03/2003 12:00am

    Originally posted by: Muhammad Khalid Noor

    void CMyRichEditCtrl::OnContextMenu(CWnd* pWnd, CPoint point)

    CMenu mnu;



    Add following line if u r using Right Button Down Hanlder.


    void CMyRichEditCtrl::OnRButtonDown(UINT nFlags, CPoint point)

    CMenu mnu;

    CRichEditCtrl::OnRButtonDown(nFlags, point);

  • rtf and showing tables and printing

    Posted by Legacy on 08/25/2003 12:00am

    Originally posted by: Leon

    how can i display tables and boxes in rtf file and printing them?im stuk with that problem few weeks please help me

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

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • Power and Automation Equipment makers face constrained CAPEX, equipment interoperability issues, and process inefficiencies. However, they can take advantage of secure Internet of Things (IoT) intelligent devices and systems to make better business decisions around asset utilization and performance, while simultaneously optimizing equipment interoperability and services efficiency. Watch this brief video to see how you can unlock the full potential of your assets and value chain.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date