RichEditCtrl Syntax Highlighting | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 28, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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()
{
 CRichEditView::OnInitialUpdate();
 CHARFORMAT cf;
 cf.dwMask = CFM_FACE | CFM_BOLD;
 cf.dwEffects = 0;
 strcpy(cf.szFaceName, "Arial");
 GetRichEditCtrl().SetDefaultCharFormat(cf);
 // 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)

COLORREF USER_DEFINED, COMMENT, KEYWORD;
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).

Downloads

Download demo source – 5 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.