Search code examples
c++componentsc++builderrichedit

C++ Builder Syntax Highlighter


I have searched on the Internet how to do this, but I can't find how.

I need to colour specific words in RichEdit, such as the words false and true.


Solution

  • TRichEdit is not well-suited for syntax highlighting (use something more like SynEdit instead), but it can be done. Basically, you need to highlight the desired text, and then set the selected text's attributes, like color and formatting. You can do that with the TRichEdit::SelStart, TRichEdit::SelLength, and TRichEdit::SelAttributes properties. Or you can use the corresponding Win32 API EM_EXSETSEL and EM_SETCHARFORMAT messages directly.

    See Robert Dunn's excellent article on this very topic: "Faster Rich Edit Syntax Highlighting". The Original article first appeared in the C++Builder Journal in October 1999, and is also available for download on Robert's "Yet Another Code Site" website (which is archived on my website).