出于工作需要,需要制作一个嵌入在桌面应用中的C语言编辑器,经过一系列调研,目前ScintillaNET应该是最合适的了,开源、轻便、功能丰富,但是踩得坑也很多,接下面一一说道。
目前ScintillaNET托管在https://github.com/jacobslusser/ScintillaNET,拉下来重新编译。由于需要移植到.NET 2.0的平台上,需要修改源码中的对Linq的依赖,这里不多说,把目标框架改为.NET 2.0,编译,哪里报错改哪里。
1. 编辑器风格
参考:https://github.com/robinrodricks/ScintillaNET.Demo
2. 括号的匹配和高亮
为了方便多处调用该控件,继承Scintilla类,实现“自定义”控件,以下均以这种做法来实现功能。
重写OnUpdateUI事件,在UpdateUI中实现括号匹配功能。


1 private int m_lastCaretPos =0; 2 protected override void OnUpdateUI(UpdateUIEventArgs e) 3 { 4 base.OnUpdateUI(e); 5 MatchAndLightBracket(); 6 } 7 private void MatchAndLightBracket() 8 { 9 // Has the caret changed position? 10 int caretPos = this.CurrentPosition; 11 if (m_lastCaretPos != caretPos) 12 { 13 m_lastCaretPos = caretPos; 14 int bracePos1 = -1; 15 int bracePos2 = -1; 16 17 // Is there a brace to the left or right? 18 if (caretPos > 0 && IsBrace(this.GetCharAt(caretPos - 1))) 19 bracePos1 = (caretPos - 1