更改范围的字符格式

时间:2012-02-18 00:07:05

标签: c++ c winapi syntax-highlighting richedit

我正在尝试实现一个代码来将语法高亮添加到winapi的丰富内容中,因此我搜索必须着色的文本范围,但是如果我选择带有EM_EXSETSEL消息的范围来格式化EM_SETCHARFORMAT消息,它很慢,所有用户都看到了。如何更快地更改字符格式?

1 个答案:

答案 0 :(得分:1)

我通过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;
}
...