我正试图实现一个代码,将语法高亮显示添加到winapi的richedit中,因此我搜索必须着色的文本范围,但如果我用EM_EXSETSEL消息选择范围,用EM_SETCHARFORMAT消息格式化,则速度非常慢,所有内容都被用户看到。如何更快地更改字符格式?
我是通过RTF完成的:这是一种标记语言,在概念上类似于HTML。立即插入颜色指令,然后重新加载缓冲区。诚然,我使用了MFC,但我认为CRichEdit是一个薄薄的层,没有添加任何进一步的功能(当然,除了框架集成)。这里有一些代码(很旧,对不起):
....
// reformat line
//
void RichEditSyntaxColor::FmtLine(CRichEditCtrl &c, int nLine)
{
int iLine = c.LineIndex(nLine),
lLine = c.LineLength(iLine);
if (lLine > 0)
{
fmt(c, nLine, lLine);
long ss, es;
c.GetSel(ss, es);
c.ShowWindow(SW_HIDE);
stream(c, iLine, lLine);
c.ShowWindow(SW_SHOW);
c.SetSel(ss, es);
}
}
// apply formatted streaming to selected pos
//
void RichEditSyntaxColor::stream(CRichEditCtrl &c, int iLine, int lLine)
{
c.SetSel(iLine, iLine + lLine);
EDITSTREAM eds = { DWORD(this), 0, fmtLineCb };
c.StreamIn(SF_RTF|SFF_SELECTION, eds);
}
// required callback
//
DWORD CALLBACK RichEditSyntaxColor::fmtLineCb(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
RichEditSyntaxColor* pCookie = (RichEditSyntaxColor*)dwCookie;
memcpy(pbBuff, pCookie->obuf, *pcb = pCookie->os.pcount());
return 0;
}
// colors data, useful to configure
//
SyntaxColor::aColor SyntaxColor::_colorsDef[] = {
{"Atom", RGB( 16, 16, 32)},
{"Number", RGB(128, 32,128)},
{"Var", RGB( 16, 16,255)},
{"String", RGB(128,128,128)},
{"Comment", RGB(128,128, 20)}, //RGB( 32,128, 32)},
{"SepChar", RGB(196, 32, 32)}
};
char SyntaxColor::_colorsStr[];
void SyntaxColor::Table2String()
{
ostringstream s(_colorsStr);//, sizeof _colorsStr);
s << "{\colortbl;";
for (int i = 0; i < __maxTag__; i++)
{
COLORREF c = _colorsDef[i].value;
s << "\red" << int(GetRValue(c))
<< "\green" << int(GetGValue(c))
<< "\blue" << int(GetBValue(c))
<< ';';
}
s << '}' << ends;
}
...