需要引用的DLL
DevExpress.CodeParser
DevExpress.Office
DevExpress.RichEdit
DevExpress.XtraRichEdit
MySyntaxHighlightServiceCSharp类
using System.Collections.Generic;
using System.Drawing;
using DevExpress.CodeParser;
using DevExpress.LookAndFeel;
using DevExpress.Office.Utils;
using DevExpress.Skins;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Services;
namespace DBMT.Control.RichText
{
/// <summary>
/// This class implements the Execute method of the ISyntaxHighlightService interface to parse and colorize the text.
/// </summary>
public class MySyntaxHighlightServiceCSharp : ISyntaxHighlightService
{
readonly RichEditControl syntaxEditor;
SyntaxColors syntaxColors;
SyntaxHighlightProperties commentProperties;
SyntaxHighlightProperties keywordProperties;
SyntaxHighlightProperties stringProperties;
SyntaxHighlightProperties xmlCommentProperties;
SyntaxHighlightProperties textProperties;
public MySyntaxHighlightServiceCSharp(RichEditControl syntaxEditor)
{
this.syntaxEditor = syntaxEditor;
syntaxColors = new SyntaxColors(UserLookAndFeel.Default);
}
void HighlightSyntax(TokenCollection tokens)
{
commentProperties = new SyntaxHighlightProperties();
commentProperties.ForeColor = syntaxColors.CommentColor;
keywordProperties = new SyntaxHighlightProperties();
keywordProperties.ForeColor = syntaxColors.KeywordColor;
stringProperties = new SyntaxHighlightProperties();
stringProperties.ForeColor = syntaxColors.StringColor;
xmlCommentProperties = new SyntaxHighlightProperties();
xmlCommentProperties.ForeColor = syntaxColors.XmlCommentColor;
textProperties = new SyntaxHighlightProperties();
textProperties.ForeColor = syntaxColors.TextColor;
if (tokens == null || tokens.Count == 0)
return;
Document document = syntaxEditor.Document;
//CharacterProperties cp = document.BeginUpdateCharacters(0, 1);
List<SyntaxHighlightToken> syntaxTokens = new