Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / MFC
Article

Painless streaming of rich text to/from CRichEditCtrl

Rate me:
Please Sign up or sign in to vote.
4.66/5 (20 votes)
29 Jan 20022 min read 275K   3.2K   39   61
An article detailing the method of streaming RTF into and out of CRichEditCtrl as used by the RichEdit DocView architecture.

Sample Image

Introduction

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;
	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() 
{
	CString sReadText; //Where the rich text will be streamed into

	EDITSTREAM es;

	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.
	EDITSTREAM es;
	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.

License

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


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: What about UNICODE support? Pin
Chezecow7-Jan-04 10:02
Chezecow7-Jan-04 10:02 
GeneralRe: What about UNICODE support? Pin
Vladimir L.7-Jan-04 11:12
Vladimir L.7-Jan-04 11:12 
GeneralRe: What about UNICODE support? Pin
Vladimir L.7-Jan-04 11:20
Vladimir L.7-Jan-04 11:20 
GeneralRe: What about UNICODE support? Pin
Vladimir L.7-Jan-04 11:48
Vladimir L.7-Jan-04 11:48 
GeneralPrinting from RichEdit Controls Pin
phaedrus20-Mar-02 10:45
phaedrus20-Mar-02 10:45 
GeneralRe: Printing from RichEdit Controls Pin
paulccc20-Mar-02 11:09
paulccc20-Mar-02 11:09 
GeneralRe: Printing from RichEdit Controls Pin
Carlos Antollini20-Mar-02 11:18
Carlos Antollini20-Mar-02 11:18 
GeneralRe: Printing from RichEdit Controls Pin
21-Mar-02 5:57
suss21-Mar-02 5:57 
GeneralI'd be so pleased if author help me! Pin
Mazdak7-Feb-02 5:02
Mazdak7-Feb-02 5:02 
QuestionCan I ask you? Pin
1-Feb-02 8:31
suss1-Feb-02 8:31 
AnswerRe: Can I ask you? Pin
paulccc1-Feb-02 22:33
paulccc1-Feb-02 22:33 

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.