[英]How to find runs of bold text inside of a RichEdit control?
I can obviously do it one character at a time using EM_GETCHARFORMAT, but it is extremely slow.很明显,我可以使用 EM_GETCHARFORMAT 一次完成一个字符,但它非常慢。
One idea is to somehow use the ITextDocument/ITextFont interfaces, the other is to use the EM_STREAMOUT message and manually parse the RTF.一种想法是以某种方式使用 ITextDocument/ITextFont 接口,另一种是使用 EM_STREAMOUT 消息并手动解析 RTF。 But I can't decide which approach is better and am very fuzzy on the implementation details.但我无法决定哪种方法更好,并且对实现细节非常模糊。 Will appreciate any help, thanks!将不胜感激任何帮助,谢谢!
I've found a solution that satisfies me and think will share it with you:我找到了一个让我满意并认为会与您分享的解决方案:
The ITextRange
interface contains a very useful method Expand
which can be used to find continuous runs of constant character ( tomCharFormat
) and paragraph ( tomParaFormat
) formatting. ITextRange
接口包含一个非常有用的方法Expand
可用于查找连续运行的常量字符 ( tomCharFormat
) 和段落 ( tomParaFormat
) 格式。
Here is some sample code (warning: code is a proof-of-concept spaghetti without any error handling, apply refactoring as needed):这是一些示例代码(警告:代码是概念验证意大利面条,没有任何错误处理,根据需要应用重构):
// 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.