一直對語法加亮編輯器控件非常感興趣,想弄個過來在項目中show一把以掙點眼球。遂上網搜搜,發現ActiproSoftware公司的SyntaxEditor是個好鼕鼕啊(SnippetCompiler就是用的這個控件),VS的功能它都基本實現了,流口水啊(http://www.chinaaspx.com/comm/dotnetbbs/Showtopic.aspx?Forum_ID=45&id=146786&PPage=8&Page=1)。
但好東西都是要銀子買的,人家要銀子3000大洋,咱們窮人沒辦法,沒銀兩,又搜不到,只好自己動手編了。邊上網參考網友代碼,加上廢寢忘食三五天,雛形出來了,能滿足一般性的代碼加亮展示要求,免費奉送!
功能概述
語法加亮
加亮規則由XML文件進行配置
AutoComplete(ctrl+shift+space)
可控步數Undo/Redo
全選、拷貝、粘貼快鍵(Ctrl+A, Ctrl+C, Ctrl+V)
直接操作RTF文檔格式,提高效率
效果
示例代碼
this.txtSyntax.ConfigFile = "csharp.xml";
this.txtSyntax.AcceptsTab = true;
this.txtSyntax.CaseSensitive = false;
this.txtSyntax.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtSyntax.FilterAutoComplete = true;
this.txtSyntax.Location = new System.Drawing.Point(0, 0);
this.txtSyntax.MaxUndoRedoSteps = 50;
this.txtSyntax.Name = "txtSyntax";
this.txtSyntax.Size = new System.Drawing.Size(464, 257);
this.txtSyntax.TabIndex = 0;
this.txtSyntax.Text = "if then while begin";
this.txtSyntax.WordWrap = false;
this.txtSyntax.AcceptsTab = true;
this.txtSyntax.CaseSensitive = false;
this.txtSyntax.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtSyntax.FilterAutoComplete = true;
this.txtSyntax.Location = new System.Drawing.Point(0, 0);
this.txtSyntax.MaxUndoRedoSteps = 50;
this.txtSyntax.Name = "txtSyntax";
this.txtSyntax.Size = new System.Drawing.Size(464, 257);
this.txtSyntax.TabIndex = 0;
this.txtSyntax.Text = "if then while begin";
this.txtSyntax.WordWrap = false;
XML配置文件示例(csharp.xml)
<?xml version="1.0" encoding="utf-8"?>
<DescriptorCollection>
<Descriptor>
<Token>/*</Token>
<CloseToken>*/</CloseToken>
<Color>0,100,0</Color>
<Font>宋體,20</Font>
<DescriptorType>ToCloseToken</DescriptorType>
<DescriptorRecognition>StartsWith</DescriptorRecognition>
<UseForAutoComplete>False</UseForAutoComplete>
</Descriptor>
<Descriptor>
<Token>[</Token>
<CloseToken>]</CloseToken>
<Color>255,0,0</Color>
<Font>宋體,20</Font>
<DescriptorType>ToCloseToken</DescriptorType>
<DescriptorRecognition>StartsWith</DescriptorRecognition>
<UseForAutoComplete>False</UseForAutoComplete>
</Descriptor>
<Descriptor>
<Token>public</Token>
<CloseToken />
<Color>0,0,255</Color>
<Font>宋體,20</Font>
<DescriptorType>Word</DescriptorType>
<DescriptorRecognition>WholeWord</DescriptorRecognition>
<UseForAutoComplete>True</UseForAutoComplete>
</Descriptor>
...
</DescriptorCollection>
<DescriptorCollection>
<Descriptor>
<Token>/*</Token>
<CloseToken>*/</CloseToken>
<Color>0,100,0</Color>
<Font>宋體,20</Font>
<DescriptorType>ToCloseToken</DescriptorType>
<DescriptorRecognition>StartsWith</DescriptorRecognition>
<UseForAutoComplete>False</UseForAutoComplete>
</Descriptor>
<Descriptor>
<Token>[</Token>
<CloseToken>]</CloseToken>
<Color>255,0,0</Color>
<Font>宋體,20</Font>
<DescriptorType>ToCloseToken</DescriptorType>
<DescriptorRecognition>StartsWith</DescriptorRecognition>
<UseForAutoComplete>False</UseForAutoComplete>
</Descriptor>
<Descriptor>
<Token>public</Token>
<CloseToken />
<Color>0,0,255</Color>
<Font>宋體,20</Font>
<DescriptorType>Word</DescriptorType>
<DescriptorRecognition>WholeWord</DescriptorRecognition>
<UseForAutoComplete>True</UseForAutoComplete>
</Descriptor>
...
</DescriptorCollection>