Painless streaming of long rich text from/to CRichEditView

Environment: VC6


There are a number of articles on the Internet as well as on CodeGuru offering advice on streaming the constituent text out of rich edit components. Although some provided source code, they still seemed a little ambiguous, and in certain cases did not provide fully what I wanted.

Reading rich text from a Rich Edit View (REV) simply involves defining a suitable callback function to transfer data. The job of calling these functions is then even easier. Example callback functions are frequently found on the Internet, however, for my own use these were unsuitable as they did not handle large amounts of text (as you would expect if you had inline objects). The code presented below is able to handle much larger documents, involving multiple calls to the callback function.

I will first describe the callback functions themselves, giving a short overview of what we're trying to achieve, then move onto explaining how you would use them in your own program.

Stream in callback function

This function takes a string passed via dwCookie and copies as much as it is allow (specified by cb) into the buffer. When the copy is done it crops the copied data from the string, reader for the next call (if necessary).

DWORD __stdcall MEditStreamInCallback( DWORD dwCookie, 
                                       LPBYTE pbBuff, 
                                       LONG cb, 
                                       LONG *pcb)
  CString *psBuffer = (CString *)dwCookie;
  if (cb < psBuffer->GetLength()) cb = psBuffer->GetLength();

  for (int i=0;i<cb;i++)
    *(pbBuff+i) = psBuffer->GetAt(i);

  *pcb = cb;

  *psBuffer = psBuffer-&rt;Mid(cb);

  return 0;

Stream out callback function

This copies as much as the buffer contains to a temporary CString, then to the end of the whole CString.

DWORD __stdcall MEditStreamOutCallback( DWORD dwCookie,
                                        LPBYTE pbBuff, 
                                        LONG cb, 
                                        LONG *pcb)
  CString sThisWrite;

  CString *psBuffer = (CString *)dwCookie;
  for (int i=0;i<cb;i++) {

  *psBuffer += sThisWrite;

  *pcb = sThisWrite.GetLength();
  return 0;

These functions can near enough be copied and pasted into your project. I recommend adding them to the MyApp.cpp and MyApp.h files usually created by AppWizard.

Next we actually want to use the functions to do something useful. For this example I've created a sample project, and made two events associated to menu items. This shows how to use the callback functions with EDITSTREAM.

How to read rich text into a CString

The function below defines a CString, then streams rich text into it. For the purposes of viewing, it will show the first 500 characters in a message box.

void CRichEgView::OnReadout() 
  //Where the rich text will be streamed into
  CString sReadText; 


  // Pass a pointer to the CString to the callback function 
  es.dwCookie = (DWORD)&sReadText; 

  // Specify the pointer to the callback function.
  es.pfnCallback = MEditStreamOutCallback; 

  // Perform the streaming

  // Show you the first 500 chars of rich codes

How to read rich text out of a Rich Edit View

The function below defines a string and sets it to some rich text (as shown in the sample project). It then streams the text in, which will in turn show up on the screen.

void CRichEgView::OnReadin() 
  //Where the text will be streamed from
  CString sWriteText; 

  sWriteText="Rich text is shown here in sample project";
  // This is hard-coded for example purposes. It is likely 
  // this would be read from file or another source.


  // Pass a pointer to the CString to the callback function
  es.dwCookie = (DWORD)&sWriteText; 

  // Specify the pointer to the callback function
  es.pfnCallback = MEditStreamInCallback; 

  // Perform the streaming

This is basically all you need to deal with streaming rich text in and out. Using CFile you will be able to store the rich text to a file for future use.


Download example project and source - 22Kb


  • richeditctrl

    Posted by vinay kaul on 10/06/2012 11:21am

    i want to log colored traces in mfc richeditctrl at 10millisecond interval, how to acheive it. I am using vc++ 10.By use of replacesel() it is logging very slow .

  • Another thanks

    Posted by asahukar on 01/27/2006 12:01pm

    ...for taking the time and effort to post this information.

  • StreamOut and Undo

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: Jai

    I am using stream out. I do a streamout immediately after a paste operation. And if I want to undo the paste operation the Undo doesn't work.

  • Exat File Format Conversion

    Posted by Legacy on 12/18/2002 12:00am

    Originally posted by: RAKIB

    can i do this In VB?
    If yes then please can u tell me how can i convert print a file with its exact file format in a word document via rich text format.

  • Can you do this in VB?

    Posted by Legacy on 12/15/2002 12:00am

    Originally posted by: KOya

    Is there a way to do this in VB?

    I would like to get the RTF of some textbox in VB using code.

    thank you.
    please help

  • Text cannot display

    Posted by Legacy on 11/16/2002 12:00am

    Originally posted by: Guohua Zhao

    I tried the sample, but the text can not be displayed, no any error message

  • Thanks

    Posted by Legacy on 10/24/2002 12:00am

    Originally posted by: Steve Miller

    Your work here is much appreciated :)

  • Thanks and let me apologize

    Posted by Legacy on 07/30/2002 12:00am

    Originally posted by: Mike Pliam

    This really works beautifully. Thank you so much for making it available.

    I apologize for my earlier questions. These were extremely naive. It's taken me some months to learn more about RTF before I really understood what you were driving at here. It must have seemed to you like explaining a watch to a pig. Thanks for your patience.

  • Problem with RichEdit

    Posted by Legacy on 06/27/2002 12:00am

    Originally posted by: Vikram Kashyap

    I am using a RichEdit Control in a dialog box, the problem I am facing is that it stops accepting text after a certain limit, e.g if there are 25000 chars in the RichEdit, it will stop accepting further text, but after some time it again starts accepting and now it has stopped after 32000. I've counted the text limit using msword's 'Word Count' feature, also i am using the richedit as a Script Editor with syntax highlighting, Please help.

  • Nice work!

    Posted by Legacy on 01/23/2002 12:00am

    Originally posted by: Christian Frenz

    I just saw your article and looked up the code in your sample. A very good idea. I always tried to get the text of a RTF-Edit into a CString, but I wouldn't work.

    Your sample is very easy to understand. The most effective and intelligible use of the StreamIn an StreamOut function I've ever seen.



  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

  • Are you truly leading your team or simply managing them? Organizations need leaders and your team needs someone to follow. With some ongoing development, you could become that leader. Learn the top leadership qualities that inspire others to want to follow you and the direction of your company.

Most Popular Programming Stories

More for Developers

RSS Feeds

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