1.此案例以WinForm为例,首先下载个ICSharpCode.TextEditor.dll文件(官方版本不报错,下载地址:/download/king_miQ/15709167)。
2.将下载的ICSharpCode.TextEditor.dll文件拖拽至工具栏中,注意要打开文件所在位置,拖拽源文件此时才会在工具栏生成一个控件。
3.再将工具栏中的控件拖拽至窗体中,就会有如下文本域。
4.双击文本,此时就会生成加载事件的方法,路径指向文件配置路径,可直接复制如下代码,注意path的路径指向xshd文件
private void textEditorControl1_Load(object sender, EventArgs e){textEditorControl1.Encoding = System.Text.Encoding.UTF8;textEditorControl1.Font = new Font("Hack", 12);//自定义代码高亮string path = AppDomain.CurrentDomain.BaseDirectory + "../..//HighLighting";//指向自定义的文件FileSyntaxModeProvider fsmp;if (Directory.Exists(path)){fsmp = new FileSyntaxModeProvider(path);HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);textEditorControl1.SetHighlighting("JackC#");}}}
5.新建一个文件夹和一个后缀为xshd的文件,文件的具体配置如下。
<?xml version="1.0"?><!-- syntaxdefinition for C# 2000 by Mike Krueger --><SyntaxDefinition name = "JackC#" extensions = ".cs"><Environment><Custom name="TypeReference" bold="false" italic="false" color="#04ABAB" /><Custom name="UnknownEntity" bold="false" italic="false" color="#AB0404" /></Environment><Properties><Property name="LineComment" value="//"/></Properties><Digits name = "Digits" bold = "false" italic = "false" color = "DarkBlue"/><RuleSets><RuleSet ignorecase="false"><Delimiters>&<>~!%^*()-+=|\#/{}[]:;"' ,.?</Delimiters><MarkPrevious bold = "true" italic = "false" color = "MidnightBlue">(</MarkPrevious><KeyWords name = "Punctuation" bold = "false" italic = "false" color = "Blue"><Key word = "Abs" /><Key word = "Acos" /><Key word = "Asin" /><Key word = "Atan" /><Key word = "Ceiling" /><Key word = "Cos" /><Key word = "Exp" /><Key word = "Floor" /><Key word = "IEEERemainder" /><Key word = "Log" /><Key word = "Log10" /><Key word = "Max" /><Key word = "Min" /><Key word = "Pow" /><Key word = "*" /><Key word = "Round" /><Key word = "Sign" /><Key word = "Sin" /><Key word = "Sqrt" /><Key word = "Tan" /><Key word = "Truncate" /></KeyWords><KeyWords name = "Punctuation" bold = "false" italic = "false" color = "Red"><Key word = "?" /><Key word = "," /><Key word = "." /><Key word = ";" /><Key word = "(" /><Key word = ")" /><!--<Key word = "[" /><Key word = "]" />--><Key word = "{" /><Key word = "}" /><Key word = "+" /><Key word = "-" /><Key word = "/" /><Key word = "%" /><Key word = "*" /><Key word = "not" /><Key word = "and" /><Key word = "or" /><Key word = "<" /><Key word = ">" /><Key word = "^" /><Key word = "or" /><Key word = "=" /><Key word = "~" /><Key word = "!" /><Key word = "|" /><Key word = "&" /></KeyWords><!--获取类函数--><KeyWords name = "Punctuation" bold = "false" italic = "false" color = "#3E6776"><Key word = "CalcArea" /><Key word = "CalcRel" /><Key word = "GetEleValue" /><Key word = "GetSenValue" /><Key word = "GetTID" /><Key word = "GetCID" /><Key word = "GetAPID" /><Key word = "GetRCID" /></KeyWords><!--类型转换--><KeyWords name = "Punctuation" bold = "false" italic = "false" color = "#246AF2"><Key word = "ToDecimal" /><Key word = "ToDouble" /><Key word = "ToFloat" /><Key word = "ToInt16" /><Key word = "ToInt32" /><Key word = "ToInt64" /><Key word = "ToDateTime" /><Key word = "ToBoolean" /><Key word = "ToChar" /><Key word = "ToString" /></KeyWords><!--专用函数--><KeyWords name = "Punctuation" bold = "false" italic = "false" color = "#4297E2"><Key word = "Q_TriaWeir" /><Key word = "Q_TrapWeir" /><Key word = "Q_RectWeir" /><Key word = "Q_VAM" /><Key word = "VWP_P" /><Key word = "VWP_HW" /><Key word = "VWP_HA" /><Key word = "IsNULL" /></KeyWords><!--测值--><KeyWords name = "Punctuation" bold = "false" italic = "false" color = "#67B23B"><Key word = "[]" /></KeyWords></RuleSet></RuleSets></SyntaxDefinition>
6.最终效果,因为我们此时定义的是Blue(蓝色)并在底下定义了Abs关键字,所以显示蓝色。
效果如下。