Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text with RTF format from Rich Edit Win API?

(Sorry for my crazy English) I want to get all the text in Rich Edit with RTF format, not plain text to a variable. I tried SendMessage() with EM_STREAMOUT to write directly Rich Edit to file, but I can't save the content to specific variables, for example LPWSTR. Please remember, only Win API, not MFC. Thanks for you help!

like image 876
quangteo.media Avatar asked Jan 14 '23 19:01

quangteo.media


1 Answers

You can pass your variable to the EM_STREAMOUT callback so it can be updated as needed, eg:

DWORD CALLBACK EditStreamOutCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    std::stringstream *rtf = (std::stringstream*) dwCookie;
    rtf->write((char*)pbBuff, cb);
    *pcb = cb;
    return 0;
}

.

std::stringstream rtf;

EDITSTREAM es = {0};
es.dwCookie = (DWORD_PTR) &rtf;
es.pfnCallback = &EditStreamOutCallback; 
SendMessage(hRichEditWnd, EM_STREAMOUT, SF_RTF, (LPARAM)&es);

// use rtf.str() as needed...

Update: to load RTF data into the RichEdit control, use EM_STREAMIN, eg:

DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    std::stringstream *rtf = (std::stringstream*) dwCookie;
    *pcb = rtf->readsome((char*)pbBuff, cb);
    return 0;
}

.

std::stringstream rtf("...");

EDITSTREAM es = {0};
es.dwCookie = (DWORD_PTR) &rtf;
es.pfnCallback = &EditStreamInCallback; 
SendMessage(hRichEditWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
like image 181
Remy Lebeau Avatar answered Jan 31 '23 07:01

Remy Lebeau