我可以將純文本設定為帶有SF_TEXT標志的RichEdit 控制元件,但我無法使用標志設定 RTF 文本SF_RTF。
下面是控制元件的創建:
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);
}
這是我設定文本的方式(我在 SO 中的某處舉了個例子),結果是“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
}
uj5u.com熱心網友回復:
RTF 字串應該是"\\r \\p"etc. not "\r \p",或者使用原始字串文字。該字串應具有兼容的字體。例如:
std::wstring wrtf = (LR"({\rtf1{\fonttbl\f0\fswiss Helvetica;}
Hello world Привет Ελληνικ? 日本語 { \b bold \b } regular.\par})");
EM_STREAMIN/EM_STREAMOUT期待BYTE輸入/輸出。
讀/寫標準RTF檔案時,只要把檔案讀為char.
format = SF_RTF;
如果源是 UTF16 字串文字,將其轉換為 UTF8,其他一切保持不變,除了格式更改為:
format = SF_RTF | SF_USECODEPAGE | (CP_UTF8 << 16);
將 UTF16 字串文字讀入 RichEdit 控制元件的示例:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/345296.html
上一篇:需要幫助來設定RichEdit
