65.9K
CodeProject is changing. Read more.
Home

Enhanced rich edit control

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.12/5 (13 votes)

Aug 23, 2003

CPOL

3 min read

viewsIcon

202227

downloadIcon

4143

A rich edit control with lots of extra features

MyRtf.GIF

Introduction

I needed a dialog box app based on RichEdit control. I have searched over the Net and I found Jeremy Iverson's article. I downloaded the code and studied it. It was not what I have needed but it was a good start point. So, my app is a kind of a RTF word processor. It has menus, toolbar, status bar etc. You can print the file, open and save a file, you can choose any fonts you want from Edit--- Format Font. If you close a file and you didn't save it the program asks if 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 appears the opened file path. If you want to save your file with the name of an existing one my program will ask if you want to overwrite the file. My program has accelerator table taken from MSDN sample - Mfccalc - CCalcDlg::CCalcDlg(...).The code from OnSize is partially taken from MSDN sample Ftptree - CFTPTREEDlg::OnSize(...).I have limited the text to 100000000 characters. CMyRichEditCtrl is derived from CRichEditCtrl class.

Details

This class has the following functions:

  1. Calling this will insert the file content --- void SetRtf(CFile* pInputFile ); This function is taken from Jeremy Iverson's article and modified
  2. Set the selection bold --- void SetSelectionBold();
  3. Set the selection italic --- void SetSelectionItalic();
  4. Set the selection underlined --- void SetSelectionUnderline();
  5. Displays a color selection dialog box and sets the selection to the chosen color (if OK is pressed). --- void SetColour();
  6. This is for CFontDialog color box --- void SetColor(COLORREF clr);
  7. Sets the current paragraph to be left aligned---void SetParagraphLeft();
  8. Sets the current paragraph to be right aligned --- void SetParagraphRight();
  9. Sets the current paragraph to be centered --- void SetParagraphCenter();
  10. Sets the current paragraph to be bulleted void SetParagraphBulleted();This function is taken from Jeremy Iverson's article.
  11. Sets the font name ---void SetFontName(CString sFontName); This function is taken from Jeremy Iverson's article.
  12. Sets the font size --- void SetFontSize(int nFontSize); This function is taken from Jeremy Iverson's article.

Using the control

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 making this program. I needed a toolbar and status bar in my app and I have taken the code from MSDN sample --- dlgcbr32. What I haven't done is menu with icons because I don't know how to do it. Maybe you will succeed and if so, let me know.

Conclusion

My program was compiled with Visual C++ 6 and 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!

History

  • 14 Sep 2003 - Updated sourcecode