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

  • Available On-Demand Today's changing workforce dynamics, economic challenges, and technological advances are placing immense pressure on business leaders to turn their focus on people – their most valuable asset – in order to remain competitive. Research shows that a significant number of new employees quit within one year of taking a new job*. Whether it's through a merger and acquisition, or standard hiring process, like any first impression, early experiences shape their opinions of their new …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds