RTF File Handling

Environment: Visual C++ 6, richedit controls

Introduction

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!

Downloads

Download source - 48 Kb


Comments

  • 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

    Reply
  • 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;
    mnu.LoadMenu(IDR_MENU2);
    mnu.GetSubMenu(0)->TrackPopupMenu(TPM_CENTERALIGN,point.x,point.y,AfxGetMainWnd());

    }


    OR


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

    ClientToScreen(&point);

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

    CMenu mnu;
    mnu.LoadMenu(IDR_MENU2);
    mnu.GetSubMenu(0)->TrackPopupMenu(TPM_CENTERALIGN,point.x,point.y,AfxGetMainWnd());

    CRichEditCtrl::OnRButtonDown(nFlags, point);
    }

    Reply
  • rtf and showing tables and printing

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

    Originally posted by: Leon

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

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds