我需要你的帮助!所以,我正在创建一个带有语法高亮工具的RichEdit,我是这样做的:
SendMessage(hWin, WM_SETREDRAW, false, 0);
CHARFORMAT2 format, old;
format.cbSize = sizeof(format);
old.cbSize = sizeof(format);
MainRich.GetFormat(SCF_DEFAULT, &format);
MainRich.GetFormat(SCF_DEFAULT, &old);
format.dwMask = CFM_BOLD;
format.dwEffects = CFE_BOLD;
CHARRANGE* c = MainRich.GetSelectionRange();
int length = MainRich.GetLength();
string str = string(MainRich.GetText());
#define hl "true" //Example of syntax for highlight
int last = 0;
while (str.find(hl, last)!=string::npos)
{
MainRich.Select(str.find(hl, last), str.find(hl, last)+strlen(hl));
MainRich.SetFormat(SCF_SELECTION, &format);
last = str.find(hl, last)+strlen(hl);
}
MainRich.Select(c->cpMin, c->cpMax);
MainRich.SetFormat(SCF_SELECTION, &old);
SendMessage(hWin, WM_SETREDRAW, true, 0);
UpdateWindow(hWin);
}但是我看到在有很多高亮的大文件中,它会变得迟缓,你有没有更好的方法来做到这一点?我检查了Iczelion's Assembly,但代码一团糟,他似乎是在文本前面画高亮,但这样选择就不起作用了,对吧?如果是这样,你能给我一些如何做到这一点的提示吗?谢谢!
发布于 2014-03-01 12:27:46
我发现最快的方法是构建原始的RTF文档,然后通过EM_STREAMIN消息将其流式传输到控件。
EDITSTREAM stream;
stream.dwCookie = (DWORD_PTR)&streamData; // pointer your rtf data
stream.dwError = 0;
stream.pfnCallback = (EDITSTREAMCALLBACK)RtfStreamCallback; // callback which will push down the next chunk of rtf data when needed
LRESULT bytesAccepted = 0;
bytesAccepted = SendMessage(hWindow, EM_STREAMIN, SF_RTF, (LPARAM)&stream);另一件要记住的事情是,您使用的RTF控件对性能有严重的影响。当我这样做时,我发现默认控件(由Windows XP提供)慢得可怕,但Microsoft Office提供的RICHED20.DLL快了几个数量级。您应该尝试您有权访问的版本,并进行一些性能比较。
链接到1.6 RTF Specification
发布于 2014-03-01 13:26:45
您对MainRich.GetText()的使用和对std::string::find()的冗余调用是瓶颈。根本不检索文本。请改用CRichEditCtrl::FindText()。
不要在结尾处恢复原始选定内容的原始格式。如果在应用突出显示之前选择了突出显示的关键字,会发生什么情况?您可以取消对它的高亮显示。
另一个可以提高RichEdit速度的优化方法是,在对文本进行更改时,使用CRichEditCtrl::SetEventMask()关闭事件(如EN_SELCHANGE),然后在更改完成后恢复这些事件。
试试这个:
SendMessage(hWin, WM_SETREDRAW, false, 0);
CHARFORMAT2 format, old;
format.cbSize = sizeof(format);
MainRich.GetFormat(SCF_DEFAULT, &format);
old = format;
format.dwMask |= CFM_BOLD;
format.dwEffects |= CFE_BOLD;
CHARRANGE* c = MainRich.GetSelectionRange();
DWORD mask = MainRich.GetEventMask();
MainRich.SetEventMask(0);
FINDTEXTEX ft;
ft.chrg.cpMin = 0;
ft.chrg.cpMax = MainRich.GetLength();
ft.lpstrText = "true";
while (MainRich.FindText(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD, &ft) != -1)
{
MainRich.Select(ft.chrgText.cpMin, ft.chrgText.cpMax);
MainRich.SetFormat(SCF_SELECTION, &format);
ft.chrg.cpMin = ft.chrgText.cpMax;
}
MainRich.Select(ft.chrg.cpMax, ft.chrg.cpMax);
MainRich.SetFormat(SCF_SELECTION, &old);
MainRich.Select(c->cpMin, c->cpMax);
MainRich.SetEventMask(mask);
SendMessage(hWin, WM_SETREDRAW, true, 0);
UpdateWindow(hWin);发布于 2014-03-03 08:58:39
最好的方法是在TextOut上进行API挂钩。
正确地指定100%。
v2.0)使用: ExtTextOutA和ExtTextOutA,(richedit v4.1)使用:richedit ExtTextOutW和richedit
https://stackoverflow.com/questions/22109514
复制相似问题