
引言
CodeEdit 是一个轻量级的代码编辑器控件,提供基本的编辑功能,此外还支持语法高亮、行号、自动完成和语法提示。
背景
某些软件需要提供一个界面供用户输入代码/脚本进行处理,例如控制台窗口或其他任何东西。该控件必须易于开发人员处理,并且易于用户使用。但是,据我所知,市面上没有免费的此类控件(如有不当之处,请指正)。
Using the Code
CodeEdit 控件是在 Ai.Control
命名空间下开发的,并直接继承自 System.Windows.Forms.Control
类。CodeEdit
控件内部提供了几个 public
类、enum
、事件、属性和函数来支持其操作。
类和枚举
AutoCompleteEventArgs
LineChangedEventArgs
SyntaxSetting
SyntaxSettingCollection
SyntaxUsage
SyntaxWord
SyntaxWordCollection
事件
LineAdded
LineChanged
LineRemoved
ShowAutoComplete
属性
CaretPosition
IgnoreCase
LineNumbers
Lines
SeparatorChars
ShowDescription
Syntaxes
TabLength
UseAutoCompleteSupports
函数
appendLine
ensureVisible
insertLine
getLine
getSelected
getWords
setLine
setLineBackColor
showAutoCompleteWindow
描述
AutoCompleteEventArgs
AutoCompleteEventArgs
类是一个派生自 System.EventArgs
类的类,用于为 ShowAutoComplete
事件提供数据。您可以设置此类中的 Show
属性来决定是否显示自动完成窗口。您还可以通过设置此类中的 CustomSyntaxes
属性的值来提供将在自动完成窗口中显示的语法列表。Usage
属性允许您决定将在自动完成窗口中显示的语法的来源。
LineChangedEventArgs
LineChangedEventArgs
类是一个派生自 System.EventArgs
类的类,用于为 LineAdded
、LineChanged
和 LineRemoved
事件提供数据。LineIndex
属性提供了添加、更改或删除行的行的索引。OldValue
属性提供了行在更改或删除之前的先前值,添加时,旧值始终是空的 string
。NewValue
属性提供了行在添加或更改之后的当前值,删除时,新值始终是空的 string
。
SyntaxSetting
SyntaxSetting
类是一个用于提供语法着色和样式支持的类。
事件
不支持。 | 名称 | 参数 |
1. | FontStyleChanged | EventArgs |
| 当字体样式属性更改时发生。 |
2. | SyntaxColorChanged | EventArgs |
| 当语法颜色属性更改时发生。 |
3. | WordsChanged | EventArgs |
| 当语法单词更改时发生。 |
属性
不支持。 | 名称 | 类型 |
1. | FontStyle | FontStyle |
| 获取或设置用于在 CodeEdit 控件中绘制语法单词的字体样式。 |
2. | 名称 | 字符串 |
| 获取或设置语法的名称。 |
3. | SyntaxColor | Color |
| 获取或设置用于在 CodeEdit 控件中绘制语法单词的颜色。 |
4. | Words | SyntaxWordCollection |
| 获取一个包含语法设置中所有单词的集合。 |
函数
不支持。 | 名称 | 类型 |
1. | getWordDescription | 字符串 |
|
描述 | 返回 SyntaxWord 对象中给定单词的描述。 | 参数 |
不支持。 | 名称 | 类型 | 1. | 字 | 字符串 | | 作为搜索描述的关键字的单词。 | 2. | ignoreCase | bool | | 指定在比较单词时是否应使用不区分大小写。 |
|
|
SyntaxSettingCollection
SyntaxSettingCollection
类是一个继承自 CollectionBase
的类,代表一个 SyntaxSetting
对象集合。
SyntaxUsage
SyntaxUsage
enum
指定在自动完成窗口中显示的语法的用法。
值
不支持。 | 名称 | 描述 |
1. | DefaultOnly | 仅使用已分配给控件的语法。 |
2. | CustomOnly | 仅使用 AutoCompleteEventArgs 对象中提供的语法。 |
3. | 两者 | 将显示默认语法和自定义语法。 |
SyntaxWord
SyntaxWord
类是一个用于为每个语法单词及其信息提供数据的类。
属性
不支持。 | 名称 | 类型 |
1. | 单词 | 字符串 |
| 获取或设置语法单词。 |
2. | 描述 | 字符串 |
| 获取或设置将在语法提示窗口中显示的语法描述。 |
SyntaxWordCollection
SyntaxWordCollection
类是一个继承自 CollectionBase
的类,代表一个 SyntaxWord
对象集合。
事件
不支持。 | 名称 |
1. |
LineAdded | 描述 | 添加行时发生。 | 参数: | LineChangedEventArgs |
|
2. |
LineChanged | 描述 | 更改行时发生。 | 参数 | LineChangedEventArgs |
|
3. |
LineRemoved | 描述 | 删除行时发生。 | 参数 | LineChangedEventArgs |
|
4. |
ShowAutoComplete | 描述 | 将显示自动完成窗口时发生。 | 参数 | AutoCompleteEventArgs |
|
属性
不支持。 | 名称 | 类型 |
1. | CaretPosition | 点 |
| 获取或设置一个指示编辑器当前光标位置的值。 |
2. | IgnoreCase | bool |
| 指示在比较语法单词时是否应使用不区分大小写的比较。默认值为 false 。 |
3. | LineNumbers | bool |
| 获取或设置一个指示是否应显示行号的值。默认值为 false 。 |
4. | Lines | string[] |
| 获取或设置代码编辑控件中的文本行。 |
5. | SeparatorChars | 字符串 |
| 获取或设置一个包含用作单词分隔符的字符的值。默认值为“~!@#$%^&*()+`-={}|[]\:;"'<>?,./ ”。 |
6. | ShowDescription | bool |
| 指示当鼠标指针悬停在语法单词上时是否应显示描述窗口。默认值为 true 。 |
7. | Syntaxes | SyntaxSettingCollection |
| 获取 SyntaxSetting 对象集合。 |
8. | TabLength | int |
| 获取或设置绘制制表符字符的空格数。默认值为 4 。 |
9. | UseAutoCompleteSupports | bool |
| 获取或设置一个指示是否应使用自动完成支持的值。默认值为 true 。 |
函数
不支持。 | 名称 | 类型 |
1. | appendLine | void |
|
描述 | 将 string 值附加到行数组的末尾。string 值将使用回车符 char 分割成行。 | 参数 |
不支持。 | 名称 | 类型 | 1. | value | 字符串 | | 要附加到行数组末尾的 string 值。 |
|
|
2. | ensureVisible | void |
|
|
3. | insertLine | void |
|
描述 | 在数组的指定索引处插入 string 值。string 值将使用回车符 char 分割成行。 | 参数 |
不支持。 | 名称 | 类型 | 1. | index | int | | 将在其中插入值的数组中的 index 。 | 2. | value | 字符串 | | 要在指定 index 处插入数组的 string 值。 |
|
|
4. | getLine | 字符串 |
|
描述 | 获取指定索引处的 string 值。 | 参数 |
不支持。 | 名称 | 类型 | 1. | index | int | | 一个基于零的 index ,指示行的 index 。 |
|
|
5. | getSelected | string[] |
|
描述 | 返回一个表示当前选定行的 string 数组。 | 参数 |
|
|
6. | getWords | string[] |
|
描述 | 获取一个表示指定索引处行内所有单词的 string 数组。 | 参数 |
不支持。 | 名称 | 类型 | 1. | index | int | | 数组中行的索引。 |
|
|
7. | setLine | void |
|
描述 | 在指定的 index 处设置行的值。 | 参数 |
不支持。 | 名称 | 类型 | 1. | index | int | | 一个基于零的索引,指示行的索引。 | 2. | value | 字符串 | | 行的值。 |
|
|
8. | setLineBackColor | void |
|
描述 | 设置行的 背景颜色 。 | 参数 |
不支持。 | 名称 | 类型 | 1. | index | int | | 一个基于零的索引,指示行的索引。 | 2. | backcolor | Color | | 行的 背景颜色 。 |
|
|
9. | showAutoCompleteWindow | 字符串 |
|
描述 | 在当前光标位置显示自动完成窗口,使用指定的语法设置集合,并指定要高亮的单词。 | 参数 |
不支持。 | 名称 | 类型 | 1. | ssc | SyntaxSettingCollection | | 要在自动完成窗口中显示的 SyntaxSettingCollection 对象。 | 2. | selectedWord | 字符串 | | 要在自动完成窗口中高亮的单词。 |
|
|
示例
创建一个新的 WinForms 应用程序项目,并将 CodeEdit
控件添加到 Form1
中,并将其命名为 codeEdit1
。在窗体的构造函数中设置语法,如下所示:
public Form1() {
InitializeComponent();
Ai.Control.CodeEdit.SyntaxSetting ss = new Ai.Control.CodeEdit.SyntaxSetting();
ss.Name = "Reserved Word";
ss.SyntaxColor = Color.Blue;
ss.Words.Add("public", "The public keyword is an access modifier for types and type members.
\nPublic access is the most permissive access level.\nThere are no restrictions on accessing
public members.");
ss.Words.Add("this", "The this keyword refers to the current instance of the class and
is also used as a modifier of the first parameter of an extension method.");
ss.Words.Add("base", "The base keyword is used to access members of the base class from
within a derived class.");
ss.Words.Add("true", "Represents the boolean value true.");
ss.Words.Add("false", "Represents the boolean value false.");
ss.Words.Add("int", "The int keyword denotes an integral type that stores values
according to the size and range shown in the following table.");
ss.Words.Add("null", "The null keyword is a literal that represents a null reference,
one that does not refer to any object. null is the default value of reference-type variables.");
codeEdit1.Syntaxes.Add(ss);
ss = new Ai.Control.CodeEdit.SyntaxSetting();
ss.Name = "Classes";
ss.SyntaxColor = Color.Teal;
ss.Words.Add("ControlStyles");
ss.Words.Add("Color");
ss.Words.Add("Cursors");
ss.Words.Add("Font");
ss.Words.Add("FontStyle");
ss.Words.Add("StringAlignment");
codeEdit1.Syntaxes.Add(ss);
// Add lines to the CodeEdit control.
codeEdit1.appendLine("public CodeEdit() : base() {");
codeEdit1.appendLine("\tthis.SetStyle(ControlStyles.AllPaintingInWmPaint, true);");
codeEdit1.appendLine("\tthis.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);");
codeEdit1.appendLine("\tthis.SetStyle(ControlStyles.ResizeRedraw, true);");
codeEdit1.appendLine("\tthis.SetStyle(ControlStyles.Selectable, true);");
codeEdit1.appendLine("\tthis.SetStyle(ControlStyles.UseTextForAccessibility, true);");
codeEdit1.appendLine("\tthis.Cursor = Cursors.IBeam;");
codeEdit1.appendLine("\t_defaultSyntax.SyntaxColor = Color.Black;");
codeEdit1.appendLine("\tbase.BackColor = Color.White;");
codeEdit1.appendLine("\tbase.Font = new Font("Consolas", 8, FontStyle.Regular);");
codeEdit1.appendLine("}");
// Sets LineNumbers property to true.
codeEdit1.LineNumbers = true;
// Sets background color of the line number 3 to yellow.
codeEdit1.setLineBackColor(3, Color.Yellow);
}
进一步开发
进一步开发还有一些需要的功能
- 代码折叠,允许用户选择性地隐藏和显示——“折叠”——正在编辑文件的部分,作为常规编辑操作的一部分。
- 自动换行,将超出页面宽度的长行截断成行,使其适合页面的可用宽度。
- 自动缩进,自动为新创建的行添加与前一行对齐的缩进。
- 支持任何字体系列,不仅仅是等宽字体系列。