语法高亮显示richedit控件无法正常工作

时间:2014-07-06 19:28:23

标签: c++ winapi richedit

我正在尝试使用richedit实现语法高亮编辑器,它可以很好地处理当前选定的行,但我可能会遗漏一些东西。 CRichEdit是我自己的richedit控制器的包装器实现,问题似乎是文本未正确选择,即使我确保使用代码生成的选定范围是我得到的EM_EXGETSEL消息。 当线条下降时,选择似乎增加1,所以我决定将ed_source.sendMessage(EM_LINEFROMCHAR,pos,0)放到部分修复问题的范围内,除了几条线,其中着色似乎是某个时间的一个或位于之前和真正适当的,所以这就是为什么我可能不理解的东西。

void parse(WIN::CRichEdit &ed_source, bool curseline)
{
    int pos, offset = 0;
    char delimiter[]={" \n\r(){};"}, *tok, *start;
    CStringA s;
    CString text;
    CWnd api;

    if(curseline){      
        ed_source.getLine(ed_source.getRow() - 1, text);
        offset = ed_source.sendMessage(EM_LINEINDEX, -1, 0);
    }else{
        text = ed_source.getCaption();
    }

    s = text;
    start = s.c_str();
    if(!start) return;

    tok = strtok(s.c_str(), delimiter);

    CHARRANGE cr = ed_source.getSelecteRange();
    ed_source.sendMessage(EM_HIDESELECTION, 1, 0) ;
    CHARRANGE range;
    while(tok)
    {
        int len = strlen(tok);

        pos = (tok - start);
        int x = ed_source.sendMessage(EM_LINEFROMCHAR, pos, 0);
        range.cpMin = offset + pos - x;
        range.cpMax = range.cpMin + len;

        ed_source.selectRange(range);
        if(isReserved(tok)){

            ed_source.setTextStyle(true, false);
            ed_source.setTextColor(keyboardColor);
        }else
            if(isType(tok)){
                ed_source.setTextStyle(false, false);
                ed_source.setTextColor(typeColor);
            }else {
                ed_source.setTextStyle(false, true);
                ed_source.setTextColor(textColor);
            }
        tok = strtok(0, delimiter);
    }

    ed_source.sendMessage(EM_HIDESELECTION, 0, 0) ;
    ed_source.selectRange(cr);
}

只是为了更具体,我在上面加上文本后立即调用上面的函数。我假设您可能希望看到上述某些功能的实现,所以在这里。

CHARRANGE CRichEdit::getSelecteRange()
{
    CHARRANGE crg = {0} ;
    sendMessage(EM_EXGETSEL, 0, (LPARAM)&crg);
    return crg;
}

void CRichEdit::selectRange(const CHARRANGE &cr)
{
    sendMessage( EM_EXSETSEL, 0, (LPARAM) &cr);
}


void CRichEdit::setTextColor(COLORREF col)
{ 
    CHARFORMAT format;
    memset(&format, 0, sizeof(CHARFORMAT));
    format.cbSize       = sizeof(CHARFORMAT);
    format.dwMask       = CFM_COLOR;
    format.crTextColor  = col;

    sendMessage( EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
}

1 个答案:

答案 0 :(得分:1)

看一下这篇文章的一些想法:

更快的丰富编辑语法突出显示 http://bcbjournal.org/articles/vol3/9910/Faster_rich_edit_syntax_highlighting.htm

它是为C ++ Builder中的TRichEdit控件编写的,但其中的大多数提示都使用了直接的Win32 API调用,并且使用VCL惯用法的少数几个地方可以很容易地适应Win32等效项。

更新:尝试从循环中删除EM_LINEFROMCHARoffset + pos已经是RichEdit中的绝对字符位置,不需要在每次循环迭代时调整它。如果您真的想要考虑行索引,那么您应该一次循环一行,分别解析每一行,而不是将整个内容解析为单个字符串。尝试更像这样的东西:

void parse(WIN::CRichEdit &ed_source, bool curseline)
{
    int startLine, endLine, offset;
    const char* delimiters = " \n\r(){};";
    char *tok, *start;
    CStringA s;
    CWnd api;

    if (curseline)
    {
        startLine = ed_source.getRow() - 1;
        endLine = startLine + 1;
    }
    else
    {
        startLine = 0;
        endLine = ed_source.sendMessage(EM_GETLINECOUNT, 0, 0);
    }

    CHARRANGE cr = ed_source.getSelecteRange();

    int eventMask = ed_source.SendMessage(EM_SETEVENTMASK, 0, 0);
    ed_source.SendMessage(WM_SETREDRAW, FALSE, 0);

    for (int line = startLine; line < endLine; ++line)
    {
        CString text;
        ed_source.getLine(line, text);

        s = text;
        start = s.c_str();
        if (!start) continue;

        offset = ed_source.sendMessage(EM_LINEINDEX, line, 0);

        tok = strtok(start, delimiters);
        while (tok)
        {
            CHARRANGE range;
            range.cpMin = offset + (int)(tok - start);
            range.cpMax = range.cpMin + strlen(tok);

            ed_source.selectRange(range);
            if (isReserved(tok))
            {
                ed_source.setTextStyle(true, false);
                ed_source.setTextColor(keyboardColor);
            }
            else if (isType(tok))
            {
                ed_source.setTextStyle(false, false);
                ed_source.setTextColor(typeColor);
            }
            else
            {
                ed_source.setTextStyle(false, true);
                ed_source.setTextColor(textColor);
            }

            tok = strtok(0, delimiters);
        }
    }

    ed_source.SendMessage(WM_SETREDRAW, TRUE, 0);
    ed_source.Invalidate(); // whatever your wrapper does to call ::InvalidateRect()

    ed_source.SendMessage(EM_SETEVENTMASK, 0, eventMask);

    ed_source.selectRange(cr);
}

据说,您可以考虑使用getLine()来定位字词,而不是使用strtok()EM_FINDWORDBREAKEM_EXSETSEL / EM_GETSELTEXT来解析文字。检索每个单词的字符。这样,你使用更少的内存并让RichEdit更多地搜索你。如果要自定义搜索的单词分隔符,可以使用EM_SETWORDBREAKPROC/EX