如何查找richit控件中粗体文本的运行
How to find runs of bold text inside of a RichEdit control?
我显然可以使用EM_GETCHARFORMAT一次处理一个字符,但它非常慢。
一个想法是以某种方式使用ITextDocument/ITextFont接口,另一个是使用EM_STREAMOUT消息并手动解析RTF。但我不能决定哪种方法更好,我对实现细节非常模糊。将感谢任何帮助,谢谢!
我已经找到了一个让我满意的解决方案,我想和你分享一下:
ITextRange
接口包含一个非常有用的方法Expand
,它可以用来查找连续运行的常量字符(tomCharFormat
)和段落(tomParaFormat
)格式。
下面是一些示例代码(警告:代码是一个没有任何错误处理的概念验证面条,根据需要应用重构):
// Get necessary interfaces
IRichEditOle* ole;
SendMessage(hwndRichEdit, EM_GETOLEINTERFACE, 0, (LPARAM)&ole);
ITextDocument* doc;
ole->QueryInterface(__uuidof(ITextDocument), (void**)&doc);
long start = 0;
// Get total length:
ITextRange* range;
doc->Range(start, start, &range);
range->Expand(tomStory, NULL);
long eof;
range->GetEnd(&eof);
// Extract formatting:
struct TextCharFormat { long start, length; DWORD effects; }
std::vector<TextCharFormat> fs;
while(start < eof - 1)
{
doc->Range(start, start, &range);
long n;
range->Expand(tomCharFormat, &n); // <-- Magic happens here
ITextFont* font;
range->GetFont(&font);
DWORD effects = 0;
long flag;
font->GetBold(&flag);
if (flag == tomTrue) effects |= CFE_BOLD;
font->GetItalic(&flag);
if (flag == tomTrue) effects |= CFE_ITALIC;
font->GetUnderline(&flag);
if (flag == tomSingle) effects |= CFE_UNDERLINE;
font->GetStrikeThrough(&flag);
if (flag == tomTrue) effects |= CFE_STRIKEOUT;
if (effects)
{
TextCharFormat f = { start, n, effects };
fs.push_back(f);
}
start += n;
}
相关文章:
- 如何减少代码的运行时间以对齐文本?
- 是否可以在运行时调用用于选择要调用的用户定义文本的逻辑?
- 在运行时使用用户定义的文本
- 如何将控制台输出的文本颜色更改为红色,但如果从Powershell或CMD运行,背景将保持相同的颜色
- 崇高文本 3 - 在终端中编译并运行 C++ 程序(路径包含空格)
- 运行时文本类型的惊人行为
- 如何编译和运行带有崇高文本的 c++ 程序 3?
- 在OS Ubuntu中运行GDB时,如何导出阀门值为文本文件
- 如何在运行控制台时更改文本
- 为什么当我在Visual c ++ studio 2010中运行应用程序时光标会转到最后一个文本框
- 我试图运行将文本从一个文件复制到另一个文件的程序
- Sublime文本3-编译程序并在终端上运行
- 从启动时运行的Qt程序中的文本文件中读取
- 传递文本框以运行.C++
- C++将文本文件读入数组,运行错误:分段错误
- C++从文本文件运行代码
- 通过文本文件发送字符串和 int 以运行
- 在编译/运行时将字符串文本的原始字节流入/流出Windows(非宽)执行字符集,以及ANSI代码页与UTF-8
- 在新运行的 c++ 程序中重新加载文本文件时保留文本文件的内容
- 为什么Qt在Qt内部运行时或调试时不会创建文本文件,而是直接从调试文件夹创建文本文件