"Syntax Highlighting" in a (Rich)Edit Control
Posted on 1997-10-28
I have taking on a personal task of trying to create an edit control that will do "syntax highlighting" a'la every decent IDE out there.
My first approach was using a RichEdit control, and then starting a thread that looks for specific keywords and turns them blue. This thread would start (starting at the current line, or last word) whenever <SPACE>, <TAB> or <ENTER> was hit. Another thread would start (when scrolling occured) at the first visible line. (Redraws are disabled while the thread is running.)
This works OK, but is a tad slow, and holding down a key produces unexpected results because a character can be entered while the thread has something selected, and then that selection gets replaced.
My problem? There has GOT to be a faster, and/or easier way to do this. I can deal with the speed issue if I can guarantee that the actual text will not get changed if a user holds down a key. Right now, setting and unsetting Read Only and/or waiting for the thread to finish produce undiserable results.
1: Is there a way to apply a CHARFORMAT effect without actually causing a selection to take place?
2: Is threre a better way for me to be doing this? Making the user wait for the thread to finish is not acceptable.
3: Currently, I am applying a "Normal" (color black) style to words that are being types (worrd that have the caret in them), to prevent "false highlighting". This, too is slow.
Thoughts on this one?
BTW: I have tried using both AfxBeginThread() and CreateThread(), both give the same results (of course, CreateThread() is faster).