Windows Controls: The Rich Edit Control

Use Rich Edit 2.0 Control to store files

2025-11-01

Written by: xiaobin

First, set up EDITSTREAM, then StreamOut.

Two Rich Edit 2.0 controls: RICHEDIT2LOG2 and RICHEDIT2LOG1; used to store plain text and hexadecimal text.

EDITSTREAM structure (richedit.h)

typedef struct _editstream {
  DWORD_PTR          dwCookie;
  DWORD              dwError;
  EDITSTREAMCALLBACK pfnCallback;
} EDITSTREAM;

callback - es

EDITSTREAMCALLBACK Editstreamcallback;

DWORD Editstreamcallback(
  [in] DWORD_PTR dwCookie,
  [in] LPBYTE pbBuff,
  [in] LONG cb,
  [in] LONG *pcb
)
{...}

StreamOut

long StreamOut(
    int nFormat,
    EDITSTREAM& es);

example

// My callback procedure that writes the rich edit control contents
// to a file.
static DWORD CALLBACK 
MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CFile* pFile = (CFile*) dwCookie;

   pFile->Write(pbBuff, cb);
   *pcb = cb;

   return 0;
}
// The example code.

// The file to store the contents of the rich edit control.
CFile cFile(TEXT("My_RichEdit_OutFile.rtf"),
            CFile::modeCreate | CFile::modeWrite);
EDITSTREAM es;

es.dwCookie = (DWORD)&cFile;
es.pfnCallback = MyStreamOutCallback;
m_myRichEditCtrl.StreamOut(SF_RTF, es);

SerialCommView.cpp

DWORD CALLBACK CSerialCommView::MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    CFile *pFile = (CFile *)dwCookie;

    pFile->Write(pbBuff, cb);
    *pcb = cb;

    return 0;
}
    CFile cFile(TEXT(strPathName), CFile::modeCreate|CFile::modeWrite);
    EDITSTREAM es;

    if (((CButton *)GetDlgItem(IDC_CHECK1HEX))->GetCheck()) { // 日志2
        es.dwCookie = (DWORD) &cFile;
        es.pfnCallback = MyStreamOutCallback;

        ((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT2LOG2))->StreamOut(SF_RTF, es);
    } else {                                                  // 日志1
        es.dwCookie = (DWORD) &cFile;
        es.pfnCallback = MyStreamOutCallback;

        ((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT2LOG1))->StreamOut(SF_RTF, es);
    }

Ref