【问题标题】:Get underlined text as strings获取带下划线的文本作为字符串
【发布时间】:2013-12-30 10:07:18
【问题描述】:

假设我有一个非常基本的文本编辑器,就像这里描述的那样:

http://aclacl.brinkster.net/MFC/ch12e.htm

此文本编辑器能够创建、保存和打开包含以下格式文本的文件:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Exceptioneur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum。

现在假设斜体文本也加了下划线。

有没有办法将上述段落中带下划线的文本的每个实例都作为字符串?

另外,有没有一种方法可以获取当前选择为字符串的任何文本?

【问题讨论】:

  • 是的,是的。阅读例如CFontRich text control 的文档。
  • 您好 Joachim,感谢您的回复。虽然我找到了将当前选定的文本作为字符串(EM_GETSELTEXT)获取的方法......我似乎仍然无法找到如何将每个带下划线的文本实例作为字符串......我假设你得到了什么是答案在于 CFont 类,但我查看了文档但仍然无法弄清楚。你能详细说明一下吗?
  • 将文本作为 RTF 流式传输。解析它。或者分析每个位置的格式,看看格式是否包含下划线选项。
  • xMRi - 听起来效率低下......所以富文本控件中没有一种方法可以让这个过程更容易?

标签: c++ mfc text-editor


【解决方案1】:

只是,我想向您展示几种方法之间的线索。

CHARFORMAT cf;
CString text;
GetDlgItemText(IDC_RICHEDIT21, text);

//loop for all text in rich edit control
for(int i = 0; i < text.GetLength(); i++)
{
    m_rich->SetSel(0 + i, i + 1); //from start to end character
    m_rich->GetSelectionCharFormat(cf);

    if(cf.dwEffects & CFE_UNDERLINE) //check whether underline character
    {
        //get underline character using GetSelText() etc.
    }
}

但是,这段代码不关心性能的任何方面,所以如果富编辑控件中有很多字符串和下划线字符,则必须检查执行时间等。 另外,我认为xMRi的回复是基本正确的方法。

我也想推荐以下文章。

how to print data in tabular format in rich edit control

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

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 1970-01-01
    • 2019-03-19
    • 2021-12-05
    • 2020-12-11
    • 2021-05-12
    • 1970-01-01
    • 2019-04-25
    • 2014-11-02
    相关资源
    最近更新 更多