Painless streaming of long rich text from/to CRichEditView


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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-&rtMid(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


  • Terminal APPCRASH

    Posted by Mike Pliam on 05/05/2015 11:54pm

    Any idea why it crashes on exit in Win7 (64 bit)? _AFXWIN_INLINE LRESULT CWnd::_AFX_FUNCNAME(SendMessage)(UINT message, WPARAM wParam, LPARAM lParam) const { ASSERT(::IsWindow(m_hWnd)); return ::SendMessage(m_hWnd, message, wParam, lParam); }

  • richeditctrl

    Posted by vinay kaul on 10/06/2012 06:21pm

    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 08:01pm

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

  • StreamOut and Undo

    Posted by Legacy on 05/30/2003 07: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 08: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 08: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 08: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 07:00am

    Originally posted by: Steve Miller

    Your work here is much appreciated :)

  • Thanks and let me apologize

    Posted by Legacy on 07/30/2002 07: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 07: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.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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