RichEditCtrl Syntax Highlighting

I wrote this class when I wanted to add syntax highlighting to a utility that I was writing. I had tried the syntax highlighting classes already available from CodeGuru and found each one lacking in some way for the application that I was writing (I was doing some really odd stuff to get it to work)

I cannot take all the credit though, some of the internal logic came from Randy More's Colorize class.

To use this class, simple setup you application to use CRichEditView, then do a search and replace changing CRichEditView to CRichSyntaxView.

If you override OnInitualUpdate I do some initializing there so either call mine yourself of do similar (or nothing if the defaults are good for you)

void CRichSyntaxView::OnInitialUpdate() 
 cf.dwMask = CFM_FACE | CFM_BOLD; 
 cf.dwEffects = 0; 
 strcpy(cf.szFaceName, "Arial"); 
 // Set the printing margins (720 twips = 1/2 inch). 
 SetMargins(CRect(720, 720, 720, 720)); 

Member functions:

bool LoadKeyWordFile(CString word); 
bool LoadKeyWordFile(CStdioFile& file);
Are from the guts of Randy More's control, the CString version calls the STDIO file version with a handle to the file pointed to by the CString

void AddComment(LPCTSTR key); 
void AddKeyWord(LPCTSTR key); 
void AddUserDefined(LPCTSTR key);
Do what they sound like, if you don't want to use a keyword file for simply want a way to give the user the ability to add things, Comments are single line comments, KeyWord, and UserDefined are 2 different COLORREF's with similar logic. This just gives you the ability to diferenciate between 2 different types(ie. C/C++ keywords and MFC keywords)

USER_DEFINED = RGB(100, 100, 0); KEYWORD = RGB(0, 0, 200); COMMENT = RGB(0, 200, 0);
These are the COLOREFS used to set the color of the keywords/symbols. They are initialized by the default constructor as shown

void SetFileName(CString word);
Gives you public access to the protected member sFileName, which is the string used by the OnSave and OnOpen member functions.

void OnSave(); 
void OnOpen();
These are the logic behind the save and restore mechanism. They allow you to save the text in the control to a Rich Text Format file. You need to do any open or save dialogs yourself, they do not work if sFileName.IsEmpty().

void Parse(); 
void ParseRange(int iMin, int iMax);
Causes the contents of the control(or a portion of the contents) to be parsed. Parse() does the whole thing, ParseRange() does a section(between iMin and iMax, iMin does not have to be less than iMax).


Download demo source - 5 Kb


  • Doesn't work on VC 7.0.

    Posted by klee777 on 12/08/2005 10:50am

    Due to lack of the project files, I created it by myself with VC7 and added some necessary initializations, but it doesn't work at all. It highlighted in a wrong place, and that was it. Does anyone have the completely working set of files and send it to me at ""? I can send mine for your review but I can't figure out how to upload it.

  • This isn't good.

    Posted by Legacy on 07/18/2003 12:00am

    Originally posted by: John

    This is truly something that requires other source to work, that wasn't included. It wasn't exactly made to plug in anyone else's source as it is.

  • DLL ?

    Posted by Legacy on 12/11/2002 12:00am

    Originally posted by: Rob


    Is there a DLL of this I can get ?

    I know nothing about C :-(

    It looks/sounds like a decent control.
    I'd like to (try to) use it.


  • Project File

    Posted by Legacy on 11/01/2002 12:00am

    Originally posted by: Edi Wibowo

    To anyone who wants the project file, please email me.


  • Find and Replace does not work

    Posted by Legacy on 02/21/2002 12:00am

    Originally posted by: D Schneider

    I used Find and Replace .. and replaced CRichEditView with CRichSyntaxView only to get a slew of errors .. i added the files to the project ... seems so simple in the instructions . wwhat else needs to be done?

  • Ctrl; not View

    Posted by Legacy on 03/15/2000 12:00am

    Originally posted by: Tim Smith

    How can I apply this view-derived class to a CRichEditCtrl, with subclassing? What modifications would have to be made? I attempted this myself but to no avail.
    Thankyou for any help.

  • How to convert color to white&black only when printing?

    Posted by Legacy on 12/02/1999 12:00am

    Originally posted by: Joo Min, Lee

    I want to change character color when printing.
    But cotrol's color must not be changed.
    I tried to use "pDC->SetTextColor(RGB(0,0,0))" in "PreparingDC(...)" in CRichEditView, but it
    doesn't work.

    Help me...

  • flickering

    Posted by Legacy on 07/15/1999 12:00am

    Originally posted by: Giorgi

    Not bad, but what about flickering?

  • Can we get a project ?

    Posted by Legacy on 07/14/1999 12:00am

    Originally posted by: xn

    Can you please add a project where we can see
    this "in action" ?



Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity/ collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

  • More than ever, agile Software Life Cycle Development and DevOps drives critical top-line business impact for customers across a broad range of industries. Learn how XtremIO is fundamentally enabling the next generation of agile Software Development & DevOps to: Radically improve developer efficiency and overall product quality via an All-Flash Dev Ops platform Enable modern lightning fast dev methodologies with zero-cost application & datasets full copies. Allow Developers to have zero storage obstacles, …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date