精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品原创,禁止转载和任何形式的非法内容使用,违者必究
ICSharpCode.TextEditor编辑框是编辑框开发的好例子,功能强大,合理使用了面向对象技术,里面输入、显示、光标、选择、常用快捷键和位置接口模块设计合理,完美实现了编辑框。初学者可以用它学面向对象,也可以学习设计编辑框,里面有复杂的细节。
如果初学者对代码不理解,请找锐英源孙老师,QQ396806883,微信ryysoft。
本文禁止转载,特别是中文理解。
Note: ICSharpCode.TextEditor and the sample application require a C# 3.0 compiler, but they are configured to target .NET Framework 2.0. Normally, we get ICSharpCode.TextEditor bundled with the SharpDevelop source code, but the sample project includes the latest version (3.0.0.3437) all by itself.需要3.0编译,配置为2.0。示例包里有最新版本代码。
SharpDevelop has a feature-rich, if not documentation-rich, text editor control. The demo attached to this article shows how to use it to load and save files, find and replace strings in a document, do clipboard actions, use TextMarkers to highlight strings, use a FoldingStrategy to let the user collapse areas of the document, use bookmarks, and change some display settings.用TextMarkers来高亮显示字符串,用FoldingStrategy来折叠文档区域,标签。
Hopefully, the remainder of this article will provide enough information to point you in the right direction for any customization you might want to make to the text editor's behavior.文本编辑框如果定制,本例给出了指引。
A text editor actually contains three nested controls that are closely coupled to one another:3个嵌套控件,紧密耦合。
If there's one thing more important than the control classes, it's the IDocument interface. The IDocument interface, implemented in the DefaultDocument class, is the hub that provides access to most of the features of SharpDevelop's text editor: undo/redo, markers, bookmarks, code folding, auto-indenting, syntax highlighting, settings, and last but not least, management of the text buffer.
Here is a diagram of these basic classes:
Note: On Visual Studio class diagrams, I can't show a derivation arrow from a class to the interface it implements, so instead, I point DefaultDocument's "interface lollipop" toward its interface.
Below is a more complete diagram that includes other classes and interfaces you can reach via the properties of TextArea and IDocument. The features of the text editor such as code folding and syntax highlighting are neatly divided into separate classes or interfaces, most of which can be replaced with a custom or derived version, if that's what you need.
The TextEditor (and SharpDevelop, in general) often uses the "Strategy" pattern, so you'll see that word a lot. In the Strategy pattern, an interface defines the functionality required, but not how to implement it. If you need a different implementation, you can write your own and call the appropriate setter in IDocument to change the strategy. In theory, anyway. For some reason, MarkerStrategy is a sealed class with no corresponding interface, and therefore cannot be replaced.
Let's talk about the features branching out from IDocument.
Curiously, there is another class which serves a similar purpose: TextAnchor anchors to a single point, and automatically moves as the document is changed, but you can't use this class because its constructor is internal.
In the presence of code folding, there are two kinds of line numbers.
IFormattingStrategy also contains methods to search backward or forward in the document for matching brackets so they can be highlighted, but this is just part of the mechanism for highlighting matching brackets, a mechanism whose implementation spans several classes including TextUtilities, BracketHighlightingSheme, BracketHighlight, and TextArea. Anyway, it appears that TextArea is hard-coded to provide brace matching for (), [], and {} only.
ITextEditorProperties has no way to inform any other object that its properties have been changed. If you change one of these properties directly, and it affects the appearance of the control, the control doesn't repaint automatically. For that reason, TextEditorControlBase has a wrapper for every property in ITextEditorProperties that it needs to monitor. For instance, TextEditorControlBase.TabIndent is a wrapper around ITextEditorProperties.TabIndent. By the way, you can share a ITextEditorProperties object among many text editors, and I have done so in the demo.
In addition to all this, the the ICSharpCode.TextEditor project contains some code related to what is commonly known as "intellisense": an "insight window" (a tooltip-like window typically used to show method signatures) and a "code completion" list.
ICSharpCode.TextEditor itself does not actually perform intellisense, but it contains some code for the GUI of these features. However, this code is not used directly by the text editor, and my demo does not demonstrate it (in fact, I don't know how to use it).
The text editor library is very large; there are a number of other miscellaneous classes that couldn't fit on the diagram, which I don't have time to describe in this article. Notable ones include TextWord, the atomic unit of syntax highlighting; LineManager, which DefaultDocument uses to convert "offsets" to "positions"; and TextUtilities, a collection of static methods.
The text editor library is very large; there are a number of other miscellaneous classes that couldn't fit on the diagram, which I don't have time to describe in this article. Notable ones include TextWord, the atomic unit of syntax highlighting; LineManager, which DefaultDocument uses to convert "offsets" to "positions"; and TextUtilities, a collection of static methods. DefaultDocument用于转换“偏移”到“位置”。
Here are some more tips: