Click here to Skip to main content
11,922,641 members (55,005 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked

Painless streaming of rich text to/from CRichEditCtrl

, 29 Jan 2002
Rate this:
Please Sign up or sign in to vote.
An article detailing the method of streaming RTF into and out of CRichEditCtrl as used by the RichEdit DocView architecture.

Sample Image


There are a number of articles on the Internet 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 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->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() 
	CString sReadText; //Where the rich text will be streamed into


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

	GetRichEditCtrl().StreamOut(SF_RTF,es); // Perform the streaming

	MessageBox(sReadText.Mid(0,500)); // 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() 
	CString sWriteText; //Where the text will be streamed from
	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.
	es.dwCookie = (DWORD)&sWriteText; // Pass a pointer to the CString to the callback function
	es.pfnCallback = MEditStreamInCallback; // Specify the pointer to the callback function

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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Paul Maddox
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThanks! Pin
Gary Wheeler6-Mar-15 10:33
memberGary Wheeler6-Mar-15 10:33 
QuestionSynchronizing Connect and :: OnConnect Notification Pin
ForNow15-Jan-12 5:21
memberForNow15-Jan-12 5:21 
GeneralAre Process Handles unique Pin
ForNow4-Apr-10 7:43
memberForNow4-Apr-10 7:43 
Answerif unicode... Pin
flyingxu20-Mar-08 1:58
memberflyingxu20-Mar-08 1:58 
QuestionError in StreamInCallback? or just me? Pin
Zinkyu22-Jun-07 4:19
memberZinkyu22-Jun-07 4:19 
AnswerRe: Error in StreamInCallback? or just me? [modified] Pin
Hryuckinnen9-Oct-07 4:11
memberHryuckinnen9-Oct-07 4:11 
GeneralRe: Error in StreamInCallback? or just me? Pin
art_s_d25-Mar-08 6:02
memberart_s_d25-Mar-08 6:02 
QuestionPlease Help: How do you get RTF from HICON Pin
ArvinB13-Mar-07 14:00
memberArvinB13-Mar-07 14:00 
GeneralThe easiest way to put some string into CRichEditCtrl Pin
Tead18-Sep-06 0:23
memberTead18-Sep-06 0:23 
GeneralRe: The easiest way to put some string into CRichEditCtrl [modified] Pin
AllAtSea26-Feb-07 9:21
memberAllAtSea26-Feb-07 9:21 
GeneralBug Pin
waldermort18-Mar-06 4:46
memberwaldermort18-Mar-06 4:46 
GeneralLess loops, no Mid Pin
Jacob L. Anawalt18-Jan-05 15:01
memberJacob L. Anawalt18-Jan-05 15:01 
GeneralRe: Less loops, no Mid Pin
Jacob L. Anawalt18-Jan-05 15:09
memberJacob L. Anawalt18-Jan-05 15:09 
GeneralRe: Less loops, no Mid [modified] Pin
AllAtSea23-Jan-05 2:10
memberAllAtSea23-Jan-05 2:10 
GeneralImproved CBStreamOut Pin
are_all_nicks_taken_or_what27-Jan-09 6:57
memberare_all_nicks_taken_or_what27-Jan-09 6:57 
GeneralRe: Improved CBStreamOut Pin
AllAtSea24-Jun-10 21:56
memberAllAtSea24-Jun-10 21:56 
GeneralNice article Pin
Kalai Kandasamy4-Mar-04 12:57
memberKalai Kandasamy4-Mar-04 12:57 
GeneralStreaming richtext with pictures Pin
Eric Bennett5-Jan-04 5:51
memberEric Bennett5-Jan-04 5:51 
GeneralRe: Streaming richtext with pictures Pin
PaulMdx5-Jan-04 8:02
memberPaulMdx5-Jan-04 8:02 
GeneralRe: Streaming richtext with pictures Pin
Christopher Corlett3-Feb-04 13:32
memberChristopher Corlett3-Feb-04 13:32 
GeneralRe: Streaming richtext with pictures Pin
PaulMdx4-Feb-04 8:02
memberPaulMdx4-Feb-04 8:02 
GeneralRe: Streaming richtext with pictures Pin
Mike O'Neill23-Apr-04 15:20
memberMike O'Neill23-Apr-04 15:20 
GeneralStreamOut Pin
YBAVG1-Oct-03 15:52
memberYBAVG1-Oct-03 15:52 
QuestionHow can I prevent CR-LF convert to CR in StreamOut Pin
Hung N19-Aug-03 1:39
sussHung N19-Aug-03 1:39 
QuestionHow to extract pure string from streamout? Pin
ari_jas21-May-03 5:48
memberari_jas21-May-03 5:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.1 | Last Updated 30 Jan 2002
Article Copyright 2002 by Paul Maddox
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid