Painless streaming of long rich text from/to CRichEditView

Environment: VC6

Introduction

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;
  sThisWrite.GetBufferSetLength(cb);

  CString *psBuffer = (CString *)dwCookie;
	
  for (int i=0;i<cb;i++) {
    sThisWrite.SetAt(i,*(pbBuff+i));
  }

  *psBuffer += sThisWrite;

  *pcb = sThisWrite.GetLength();
  sThisWrite.ReleaseBuffer();
  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; 

  EDITSTREAM es;

  // 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
  GetRichEditCtrl().StreamOut(SF_RTF,es); 

  // Show you the first 500 chars of rich codes
  MessageBox(sReadText.Mid(0,500)); 
}

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.

  EDITSTREAM es;

  // 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
  GetRichEditCtrl().StreamIn(SF_RTF,es); 
}

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.

Downloads

Download example project and source - 22Kb


Comments

  • 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 .

    Reply
  • Another thanks

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

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

    Reply
  • StreamOut and Undo

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

    Originally posted by: Jai

    Hi
    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.
    Thanks

    Reply
  • 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.
    thanks
    rakib

    Reply
  • 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

    Reply
  • 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

    Reply
  • Thanks

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

    Originally posted by: Steve Miller

    Your work here is much appreciated :)

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • 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.


    Thanks

    Christian

    Reply
  • Loading, Please Wait ...

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