首页 技术 正文
技术 2022年11月14日
0 收藏 522 点赞 2,508 浏览 2510 个字

  出于工作需要,需要制作一个嵌入在桌面应用中的C语言编辑器,经过一系列调研,目前ScintillaNET应该是最合适的了,开源、轻便、功能丰富,但是踩得坑也很多,接下面一一说道。

  目前ScintillaNET托管在https://github.com/jacobslusser/ScintillaNET,拉下来重新编译。由于需要移植到.NET 2.0的平台上,需要修改源码中的对Linq的依赖,这里不多说,把目标框架改为.NET 2.0,编译,哪里报错改哪里。

1. 编辑器风格

参考:https://github.com/robinrodricks/ScintillaNET.Demo

ScintillaNET的应用

2. 括号的匹配和高亮

为了方便多处调用该控件,继承Scintilla类,实现“自定义”控件,以下均以这种做法来实现功能。

重写OnUpdateUI事件,在UpdateUI中实现括号匹配功能。

 private int m_lastCaretPos =;
protected override void OnUpdateUI(UpdateUIEventArgs e)
{
base.OnUpdateUI(e);
MatchAndLightBracket();
}
private void MatchAndLightBracket()
{
// Has the caret changed position?
int caretPos = this.CurrentPosition;
if (m_lastCaretPos != caretPos)
{
m_lastCaretPos = caretPos;
int bracePos1 = -;
int bracePos2 = -; // Is there a brace to the left or right?
if (caretPos > && IsBrace(this.GetCharAt(caretPos - )))
bracePos1 = (caretPos - );
else if (IsBrace(this.GetCharAt(caretPos)))
bracePos1 = caretPos; if (bracePos1 >= )
{
// Find the matching brace
bracePos2 = this.BraceMatch(bracePos1);
if (bracePos2 == CodeEditor.InvalidPosition)
{
ReleaseHighlightB();
}
else
{
ReleaseHighlightB();
HighlightBracket(bracePos1);
HighlightBracket(bracePos2);
}
}
else
{
ReleaseHighlightB();
}
}
}
private void HighlightBracket(int pos)
{
if (pos < )
return;
this.IndicatorFillRange(pos, );
}
private void ReleaseHighlightB()
{
this.IndicatorClearRange(, this.TextLength);
}
private static bool IsBrace(int c)
{
switch (c)
{
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case '<':
case '>':
return true;
} return false;
}

3. Ctrl+Z会一次性清空所有的修改

 protected override void OnBeforeInsert(BeforeModificationEventArgs e)
{
base.OnBeforeInsert(e);
Count = this.Text.Length;
this.BeginUndoAction();
}
protected override void OnInsert(ModificationEventArgs e)
{
base.OnInsert(e);
if (Count < this.Text.Length)
this.EndUndoAction();
}

4.缩进调整

 protected override void OnCharAdded(CharAddedEventArgs e)
{
base.OnCharAdded(e);
AutoIndicator();
}
private void AutoIndicator()
{
int pos = this.CurrentPosition;
if (pos > && this.GetCharAt(pos - ) == '\n' && this.GetCharAt(pos - ) == '\r')
{
if (this.GetCharAt(pos - ) == '{')
{
string[] Text = GetStringList();
string line = Text[this.CurrentLine - ];
int start = line.IndexOf(line.TrimStart());
string ss = line.Substring(, start);
string str = new string(' ',);
this.InsertText(pos, ss+str);
this.SelectionStart = this.SelectionEnd = pos + ss.Length + ;
}
else
{
string[] Text = GetStringList();
string line = Text[this.CurrentLine - ];
int start = line.IndexOf(line.TrimStart());
string ss = line.Substring(, start);
this.InsertText(pos, ss);
this.SelectionStart = this.SelectionEnd = pos + ss.Length;
} }
}
private string[] GetStringList()
{
string[] s = new string[] { "\n" };
return this.Text.Split(s, StringSplitOptions.None);
}

目前整理了这么多,有机会再继续深入理解和应用ScintillaNET。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844