I can set plain text to RichEdit control with SF_TEXT
flag, but I cann't set RTF-text with SF_RTF
flag.
Here is the creation of control:
LoadLibrary(TEXT("Riched20.dll"));
richEdit = CreateWindowEx(
0,
RICHEDIT_CLASS,
TEXT(""),
ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
x,
y,
width,
height,
hwndOwner,
NULL,
hInstance,
NULL);
}
And here is how I set text (I took example somewhere in SO), resulting in "ERROR_INVALID_HANDLE":
DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
std::wstringstream *rtf = (std::wstringstream*) dwCookie;
*pcb = rtf->readsome((wchar_t*)pbBuff, cb);
return 0;
}
// ......
// Setting text
std::wstringstream rtf(L"{\rtf1Hello!\par{ \i This } is some { \b text }.\par}");
EDITSTREAM es = { 0 };
es.dwCookie = (DWORD_PTR)&rtf;
es.pfnCallback = &EditStreamInCallback;
if (SendMessage(richEdit, EM_STREAMIN, SF_RTF, (LPARAM)&es) && es.dwError == 0)
{
// ...
} else {
auto error = GetLastError(); // error == 6
}
CodePudding user response:
RTF string should be "\\r \\p"
etc. not "\r \p"
, or use raw string literal. The string should have compatible font. For example:
std::wstring wrtf = (LR"({\rtf1{\fonttbl\f0\fswiss Helvetica;}
Hello world Привет Ελληνικά 日本語 { \b bold \b } regular.\par})");
EM_STREAMIN/EM_STREAMOUT
expect BYTE
input/output.
When reading/writing standard RTF file, just read the file as char
, don't do any conversion to wide string, and use this format
format = SF_RTF;
If source is UTF16 string literal, convert it to UTF8, everything else stays the same, except format changes to:
format = SF_RTF | SF_USECODEPAGE | (CP_UTF8 << 16);
Example reading UTF16 string literal in to RichEdit control:
std::string utf8(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();
int sz = WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),-1,0,0,0,0);
std::string res(sz, 0);
WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),(int)wstr.size(),&res[0],sz,0,0);
return res;
}
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 ss(utf8(wrtf));
EDITSTREAM es = { 0 };
es.dwCookie = (DWORD_PTR)&ss;
es.pfnCallback = &EditStreamInCallback;
SendMessage(richedit, EM_STREAMIN,
SF_RTF | SF_USECODEPAGE | (CP_UTF8 << 16), (LPARAM)&es);