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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds