简体   繁体   English

需要帮助来设置 RichEdit

[英]Need help to setup RichEdit

I'm trying to set the following text in RichEdit (v2.0 I guess, as I use "Riched20.dll" library):我正在尝试在 RichEdit(我猜是 v2.0,因为我使用“Riched20.dll”库)中设置以下文本:

{\\rtf1Привет!\\par{ \\i This } is super {\\b text}.\\par} {\\rtf1Привет!\\par{ \\i This } 超级{\\b text}。\\par}

The first problem is wrong symbols instead of non-latin text Привет , the second problem is bold text section {\\\\b text} , which is rendered as non bold.第一个问题是错误的符号而不是非拉丁文本Привет ,第二个问题是粗体文本部分{\\\\b text} ,呈现为非粗体。 Here is the screenshot:这是屏幕截图:

RTF 示例

Visual Studio set up to "Use Unicode Character Set" (the app I'm working on is already setuped this way, and I'm still quiet bad in how Win encodings work). Visual Studio 设置为“使用 Unicode 字符集”(我正在开发的应用程序已经以这种方式设置,而且我对 Win 编码的工作方式仍然很不满意)。 I use ordinar (eg not wide char) std::string, as wide char classes don't work for my code - that was my previous question .我使用普通(例如不是宽字符)std::string,因为宽字符类不适用于我的代码 - 这是我之前的问题

Here is the code snippet:这是代码片段:

    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;
    }

    // ...
    auto hwndEdit = CreateRichEdit(hWnd, 100, 100, 300, 300, hInstance);
    std::stringstream rtf("{\\rtf1Привет!\\par{ \\i This } is super {\\b text}.\\par}");

    EDITSTREAM es  = { 0 };
    es.dwCookie    = (DWORD_PTR)&rtf;
    es.pfnCallback = &EditStreamInCallback;

    SendMessage(hwndEdit, EM_STREAMIN, SF_RTF, (LPARAM)&es);

Update: The end goal is: get some RTF-string (which may consists of unicode(?) text, links, etc.) from JSON like:更新:最终目标是:从 JSON 中获取一些 RTF 字符串(可能包含 unicode(?) 文本、链接等),例如:

{
    "text": "{\\rtf1Привет!\\par{ \\i This } is super {\\b text}.\\par}"
}

, show it, handle clicks at hyperlinks, and almost certainly to modify specific symbols (the specific symbol is custom symbol that replaces original symbol in our own modified .ttf font). ,显示它,处理超链接的点击,并且几乎可以肯定会修改特定符号(特定符号是自定义符号,它替换了我们自己修改的 .ttf 字体中的原始符号)。 I didn't read the RTF documentation yet and used given string just to check out how RichEdit contol and corresponding winapi work.我还没有阅读 RTF 文档并使用给定的字符串来检查 RichEdit 控制和相应的 winapi 是如何工作的。

The final RTF-text would be formed in RTF-editor, I suppose.我想最终的 RTF 文本将在 RTF 编辑器中形成。 Almost certainly, the WordPad.几乎可以肯定,写字板。

Convert your text according the RTF format specification:根据 RTF 格式规范转换您的文本:

std::string rtf("{\\rtf1\\deff1{\\fonttbl{\\f0\\fcharset0 Times New Roman;}{\\f1\\fcharset0 Segoe UI;}}{\\lang1033{\\f1{\\ltrch\\u1055?\\u1088?\\u1080?\\u1074?\\u1077?\\u1090?!}\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\par}{\\f1{\\i\\ltrch This }{\\ltrch is super }{\\b\\ltrch text}{\\ltrch .}\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\par}}}");

std::stringstream ss(rtf);
EDITSTREAM es = { 0 };
es.dwCookie = (DWORD_PTR)&ss;
es.pfnCallback = &EditStreamInCallback;
SendMessage(richedit, EM_STREAMIN, SF_RTF, (LPARAM)&es);

This rtf string produces the following text:rtf字符串生成以下文本:

在此处输入图片说明

List of main control words used in the rtf string above (according the Rich Text Format (RTF) Version 1.5 Specification):上面rtf字符串中使用的主要控制字列表(根据富文本格式 (RTF) 1.5 版规范):

  • \\rtf1 \\rtf1
    The RTF document Specification version is 1. RTF 文档规范版本为 1。

  • \\deffN \\定义N
    The \\deff control word specified the default font number. \\deff 控制字指定了默认字体编号。

  • \\fonttbl \\fonttbl
    The \\fonttbl control word introduces the font table group. \\fonttbl 控制字引入了字体表组。

  • \\lang1033 \\lang1033
    Applies a language to a character.将语言应用于字符。 N is a number corresponding to a language. N是对应于一种语言的数字。

In the project settings you can define Character Set as Use Unicode character , Use Multi-Byte Character or Not Set , does not matter for this case.在项目设置中,您可以将Character Set定义为Use Unicode characterUse Multi-Byte CharacterNot Set ,在这种情况下无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM