---------------求救啊,看不懂 这个控件是什么现实 自动变色的。----------------------
我的问题:
  求救啊,看不懂 这个控件是什么现实[关键字][自动变色]。
下面的是原码。..................................它封装得太好了,几乎 用接口 一个接一个,点击下载 代码编辑器 自动 关键字变色 源码 vs2008 vs2005 vs 2010 可以打开运行,请高手帮忙!!!主要应用如下:
  1· 拖一个控件到winForm 窗体。
  2· 写下如下代码:
private void myWindow_Load(object sender, EventArgs e)
{ //this .TextEditorControl控件 :txtContent 的值 ,
textEditorControl1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
textEditorControl1.Encoding = System.Text.Encoding.Default; //
textEditorControl1.Text = "int in while to add hello goto for ";
}就行了, 当我边输入时它就会边变色 ,现在 ,我看这个[控件] 一个事件都没有[注册]。没有注册事件, 却可以动态变色!
  我仔细看看,又找不到 在控件中定义变色的[实现代码] 。高手们,一定要帮我解答啊。 ..............................在上面的源码时点击下载,用就 vs 可以打开运行。

解决方案 »

  1.   

    你可以用Reflector看一下里面的方法和属性
      

  2.   

    我看了一下
    他主要是在Resource文件下有很多个xshd文件,其实就是xml文件。你点进去看看,就知道,比如C#有这么一段 <KeyWords name = "Punctuation" bold = "false" italic = "false" color = "DarkGreen">
    <Key word = "?" />
    <Key word = "," />
    <Key word = "." />
    <Key word = ";" />
    <Key word = "(" />
    <Key word = ")" />
    <Key word = "[" />
    <Key word = "]" />
    <Key word = "{" />
    <Key word = "}" />
    <Key word = "+" />
    <Key word = "-" />
    <Key word = "/" />
    <Key word = "%" />
    <Key word = "*" />
    <Key word = "&lt;" />
    <Key word = "&gt;" />
    <Key word = "^" />
    <Key word = "=" />
    <Key word = "~" />
    <Key word = "!" />
    <Key word = "|" />
        <Key word = "&amp;" />
       </KeyWords>看上面设置了颜色的。就是把这些关键字的颜色设置为DarkGreen,另外设置高亮他是用hashtable来存储的
    在Src文件夹下的doucument下的HighlightingStrategy有一个SyntaxModes文件夹下有个SyntaxMode.cs类里面有个方法 public static ArrayList GetSyntaxModes(Stream xmlSyntaxModeStream)
    读取对应的xshd文件(XML)
    其实整个思路就是根据你选择的语言,(已经对各种语言的关键字进行了搜集,放在了xshd文件里,而且对不同的关键字设置了不同的颜色),然后就找到相应的xshd文件,显示关键字。效果就明显了。这就好像当时对时候学习编译原理的词法分析,语法分析
      

  3.   

    我知道他是 从xml文件 那里读数据。  但是我打开[TextEditorControl] 控件的定义。
    就是找到不 他有部分改变颜色的方法,
      当我 在 [TextEditorControl]控件中输入文本时,
    究竟触发了 哪些 [改变颜色的方法],
    这个多文件与类,一个个找也找不到,  如何通过调试发现了.........(当我输入 int 时,触发了什么?)
      

  4.   

    算了,
      这个这是看不懂的了,
    另外,我已查看到文档 自已写控件:从 [RichTextBox] 中继承,并实现了上面的功能了。
      

  5.   

    这个控件从写如下:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using HWND = System.IntPtr;
    using System.Drawing;
    using SyntaxEditor;namespace SyntaxControl
    {
    public class SyntaxEditor : RichTextBox
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; //使用win32api:SendMessage来防止控件着色时的闪烁现象 [DllImport("user32")]
    private static extern int SendMessage(HWND hwnd, int wMsg, int wParam, IntPtr lParam);
    private const int WM_SETREDRAW = 0xB; public SyntaxEditor()
    {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent();
    base.WordWrap = false;
    // TODO: 在 InitComponent 调用后添加任何初始化 } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    if (components != null)
    components.Dispose();
    }
    base.Dispose(disposing);
    } #region 组件设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    //
    // SyntaxEditor
    //
    this.Name = "SyntaxEditor"; }
    #endregion //重写基类的OnTextChanged方法。为了提高效率,程序是对当前文本插入点所在行进行扫描, //以空格为分割符,判断每个单词是否为关键字,并进行着色。 protected override void OnTextChanged(EventArgs e)
    {
    if (base.Text != "")
    {
    int selectStart = base.SelectionStart;
    int line = base.GetLineFromCharIndex(selectStart); string lineStr = base.Lines[line];
    int linestart = 0;
    for (int i = 0; i < line; i++)
    {
    linestart += base.Lines[i].Length + 1;
    } SendMessage(base.Handle, WM_SETREDRAW, 0, IntPtr.Zero); base.SelectionStart = linestart;
    base.SelectionLength = lineStr.Length;
    base.SelectionColor = Color.Black;
    base.SelectionStart = selectStart;
    base.SelectionLength = 0; string[] words = lineStr.Split(new char[] { ' ' });
    Parser parser = new Parser(this.language);
    for (int i = 0; i < words.Length; i++)
    {
    if (parser.IsKeyWord(words[i]))
    { int length = 0;
    for (int j = 0; j < i; j++)
    {
    length += words[j].Length;
    }
    length += i; int index = lineStr.IndexOf(words[i], length); base.SelectionStart = linestart + index;
    base.SelectionLength = words[i].Length;
    base.SelectionColor = Color.Blue;
    base.SelectionStart = selectStart;
    base.SelectionLength = 0;
    base.SelectionColor = Color.Black; }
    }
    SendMessage(base.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
    base.Refresh();
    }
    base.OnTextChanged(e);
    } public new bool WordWrap
    {
    get
    {
    return base.WordWrap;
    }
    set
    {
    base.WordWrap = value;
    }
    } private Languages language = Languages.SQL; public Languages Language
    {
    get
    {
    return this.language;
    }
    set
    {
    this.language = value;
    }
    }
    }
    public enum Languages
    {
    SQL,
    VBSCRIPT,
    CSHARP,
    JSHARP
    }
    }
    算是非常简单的了。