Unicode RTF text in RichEdit(RichEdit 中的 Unicode RTF 文本)
问题描述
我无法让 RichEdit 控件显示 unicode RTF 文本.我的应用程序是 Unicode,所以所有字符串都是 wchar_t 字符串.
如果我将控件创建为RichEdit20A",我可以使用例如SetWindowText,并以正确的格式显示文本.如果我将控件创建为RichEdit20W",则使用 SetWindowText 会逐字显示文本,即显示所有 RTF 代码.如果我使用 EM_SETTEXTEX 参数,指定代码页 1200,MSDN 告诉我用于指示 unicode,也会发生同样的情况.
我尝试过使用 StreamIn 函数,但这似乎只有在我以 ASCII 文本进行流式传输时才有效.如果我在widechars中流式传输,那么我会在控件中得到空文本.我使用 SF_RTF|SF_UNICODE 标志,而 MSDN 提示可能不允许这种组合.
I'm having trouble getting a RichEdit control to display unicode RTF text. My application is Unicode, so all strings are wchar_t strings.
If I create the control as "RichEdit20A" I can use e.g. SetWindowText, and the text is displayed with the proper formatting. If I create the control as "RichEdit20W" then using SetWindowText shows the text verbatim, i.e. all the RTF code is displayed. The same happens if I use the EM_SETTEXTEX parameter, specifying codepage 1200 which MSDN tells me is used to indicate unicode.
I've tried using the StreamIn function, but this only seems to work if I stream in ASCII text. If I stream in widechars then I get empty text in the control. I use the SF_RTF|SF_UNICODE flags, and MSDN hints that this combination may not be allowed.
那该怎么办?有什么方法可以在不丢失 RTF 解释的情况下将宽字符放入 RichEdit,还是我需要对其进行编码?我曾考虑尝试 UTF-8,或者可能使用 RTF 中的编码工具,但不确定最佳选择是什么.
So what to do? Is there any way to get widechars into a RichEdit without losing RTF interpretation, or do I need to encode it? I've thought about trying UTF-8, or perhaps use the encoding facilities in RTF, but am unsure what the best choice is.
推荐答案
我最近不得不这样做,并注意到你正在做的同样的观察.
I had to do this recently, and noticed the same sorts of observations you're making.
看起来,尽管 MSDN 几乎建议,RTF"解析器仅适用于 8 位编码.所以我最终做的是使用 UTF-8,这是一种 8 位编码,但是仍然可以表示全范围的 Unicode 字符.您可以通过 PWSTR 获取 UTF-8noreferrer">WideCharToMultiByte():
It seems that, despite what MSDN almost suggests, the "RTF" parser will only work with 8-bit encodings. So what I ended up doing was using UTF-8, which is an 8 bit encoding but still can represent the full range of Unicode characters. You can get UTF-8 from a PWSTR
via WideCharToMultiByte():
PWSTR WideString = /* Some string... */;
DWORD WideLength = wcslen(WideString) + 1;
PSTR Utf8;
DWORD Length;
INT ReturnedLength;
// A utf8 representation shouldn't be longer than 4 times the size
// of the utf16 one.
Length = WideLength * 4;
Utf8 = malloc(Length);
if (!Utf8) { /* TODO: handle failure */ }
ReturnedLength = WideCharToMultiByte(CP_UTF8,
0,
WideString,
WideLength-1,
Utf8,
Length-1,
NULL,
NULL);
if (ReturnedLength)
{
// Need to zero terminate...
Utf8[ReturnedLength] = 0;
}
else { /* TODO: handle failure */ }
一旦你拥有 UTF-8 格式,你就可以这样做:
Once you have it in UTF-8, you can do:
SETTEXTEX TextInfo = {0};
TextInfo.flags = ST_SELECTION;
TextInfo.codepage = CP_UTF8;
SendMessage(hRichText, EM_SETTEXTEX, (WPARAM)&TextInfo, (LPARAM)Utf8);
当然(我最初忽略了这个,但是当我很明确时......):
And of course (I left this out originally, but while I'm being explicit...):
free(Utf8);
这篇关于RichEdit 中的 Unicode RTF 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RichEdit 中的 Unicode RTF 文本
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01