一、TSynEdit
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace SQLDialog.SynEdit
{
/// <summary>
/// 作者:徐侠君
/// 创建时间:2004-08-15
/// <修改1>增加对引号内字符串的处理</修改1>
/// <修改2>增加对变化的文本的处理,主要是监视键盘的输入 2004-08-19</修改2>
/// <修改3>增加对多行注释的处理 2004-08-21 2004-08-22完成</修改3>
/// </summary>
public class TSynEdit : System.Windows.Forms.RichTextBox
{
public TSynEdit()
{
_KeyWordHighlighter = new TSynHighlighter();
_CommentHighlighter = new TSynHighlighter();
_FunctionHighlighter = new TSynHighlighter();
_CommonHighlighter = new TSynHighlighter();
_CommonHighlighter.ForeColor = this.ForeColor;
_CommonHighlighter.fontStyle = this.Font.Style;
_TheBuffer=new ArrayList();
_SingleRowRemark = "";
_MultiRowRemHead = "";
Quota=new TSynQuotaCollection();
this.WordWrap = false;
this.AcceptsTab = true;
this.Multiline = true;
this.KeyDown += new KeyEventHandler(OnKeyEventHandler);
_needRedrawColorRow.startPos =-1;
_LineSelfClose = new ArrayList();
this.DetectUrls = false;
this.Text ="";
_LineSelfClose.Add(true);
}
/// <summary>
/// 每一行的元素是否有跨行的注释
/// </summary>
private System.Collections.ArrayList _LineSelfClose;
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
private int GetRowBeginPos(int Index)
{
// 找当前行的开始位置
int pos = Index;
while ( (pos > 0) && (this.Text[pos-1] != '/n'))
pos--;
return pos;
}
/// <summary>
/// 发生变化的行
/// </summary>
private struct NeedRedrawColorRow
{
public int startPos; //起始位置
public int RowNums; //总行
}
private NeedRedrawColorRow _needRedrawColorRow;
/// <summary>
/// 剪切,重写该方法,主要是为了监视到剪切,以便重新着色
/// </summary>
public new void Cut()
{
if(this.SelectionLength>0)
{
_needRedrawColorRow.startPos=GetRowBeginPos(this.SelectionStart);
_needRedrawColorRow.RowNums = 1;
int DeleteLineNum = GetNewLineNum(this.Text.Substring(this.SelectionStart,this.SelectionLength));
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
for(int i = 1;i < DeleteLineNum;i++)
{
_LineSelfClose.RemoveAt( currentLine + 1);
}
base.Cut();
}
}
/// <summary>
/// 判断是否可以粘贴
/// </summary>
/// <returns></returns>
public bool CanPaste()
{
if(this.CanPaste(DataFormats.GetFormat(DataFormats.StringFormat)) ||
this.CanPaste(DataFormats.GetFormat(DataFormats.UnicodeText)) ||
this.CanPaste(DataFormats.GetFormat(DataFormats.Text)) )
{
object PasteData=Clipboard.GetDataObject().GetData(typeof(string));
if(PasteData != null)
return true;
else
return false;
}
else
return false;
}
/// <summary>
/// 处理粘贴的数据
/// </summary>
/// <returns></returns>
private bool DoWithPaste()
{
if(this.CanPaste(DataFormats.GetFormat(DataFormats.StringFormat)) ||
this.CanPaste(DataFormats.GetFormat(DataFormats.UnicodeText)) ||
this.CanPaste(DataFormats.GetFormat(DataFormats.Text)) )
{
object PasteData=Clipboard.GetDataObject().GetData(typeof(string));
if(PasteData != null)
{
string s=PasteData.ToString();
int i = GetNewLineNum(s);
_needRedrawColorRow.startPos = GetRowBeginPos(this.SelectionStart);
_needRedrawColorRow.RowNums = i;
int j=1;
if(this.SelectionLength > 0)
{
j = this.GetNewLineNum( this.Text.Substring(this.SelectionStart,this.SelectionLength));
}
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
//粘贴的数据行数,比选中的数据行数多
for(int k = 1;k <= i - j;k++)
{
if( currentLine + i >= _LineSelfClose.Count)
_LineSelfClose.Add(true);
else
_LineSelfClose.Insert ( currentLine + i,true);
}
//粘贴的数据行数,比选中的数据行数少
for(int k = 1;k <= j - i;k++)
{
_LineSelfClose.RemoveAt( currentLine + i);
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 重写粘贴方法,以便监视到编辑内容的变化
/// </summary>
public new void Paste()
{
DoWithPaste();
base.Paste();
}
/// <summary>
/// 重写粘贴方法,以便监视到编辑内容的变化
/// </summary>
/// <param name="clipFormat">这个方法,实际上无效,只是为了能够监视到文本编辑内容的变化</param>
public new void Paste(System.Windows.Forms.DataFormats.Format clipFormat)
{
this.Paste();
}
/// <summary>
/// 根据字符串内容,判断有多少行
/// </summary>
/// <returns></returns>
private int GetNewLineNum(string s)
{
Regex r=new Regex("/n",RegexOptions.None);
Match M;
int i=1;
for(M=r.Match(s);M.Success;M=M.NextMatch())
{
i++;
}
return i;
}
protected override bool ProcessDialogKey(Keys keyData)
{
//keyData=System.Windows.Forms.Keys.
return base.ProcessDialogKey (keyData);
}
/// <summary>
/// 监视,键盘的输入,以便及时更新代码着色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnKeyEventHandler(object sender,KeyEventArgs e)
{
_needRedrawColorRow.startPos=-1;
_NeedReDrawColor =true;
//如果按下的是回车键,则需要着色本行以及上一行
if(e.KeyValue ==13)
{
if(this.Text.Length > this.SelectionStart && this.Text[this.SelectionStart] == '/n')
{
//在一行的结束处,按了回车
_NeedReDrawColor = false;
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
if(currentLine == this.Lines.Length -1 )
{
_LineSelfClose.Add(_LineSelfClose[currentLine]);
}
else
{
_LineSelfClose.Insert(currentLine+1,_LineSelfClose[currentLine]);
}
}
else if(this.SelectionStart > 0 && this.Text[this.SelectionStart-1] == '/n')
{
//在一行的开始处,按了回车
_NeedReDrawColor = false;
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
if(currentLine == this.Lines.Length -1 )
{
_LineSelfClose.Add(_LineSelfClose[currentLine]);
}
else
{
_LineSelfClose.Insert(currentLine+1,_LineSelfClose[currentLine]);
}
}
else
{
_NeedReDrawColor = true;
_needRedrawColorRow.startPos = GetRowBeginPos(this.SelectionStart);
_needRedrawColorRow.RowNums =2;
//两行发生变化
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
if(currentLine == this.Lines.Length -1 )
{
_LineSelfClose.Add(_LineSelfClose[currentLine]);
}
else
{
_LineSelfClose.Insert(currentLine+1,_LineSelfClose[currentLine]);
}
}
}
//退格键
else if(e.KeyValue == 8)
{
//在一行的开始处退格
if(this.SelectionStart >= 1 && this.Text[this.SelectionStart-1] == '/n' && this.SelectionLength == 0 )
{
_NeedReDrawColor = false;
_LineSelfClose.RemoveAt(this.GetLineFromCharIndex(this.SelectionStart));
}
else
{
_NeedReDrawColor = true;
//只需要重绘当前行
_needRedrawColorRow.startPos = GetRowBeginPos(this.SelectionStart);
_needRedrawColorRow.RowNums =1;
//获取选中的部分中有多少行,从自封闭标志数组中删除
if(this.SelectionLength > 0 )
{
int DeleteLineNum = GetNewLineNum(this.Text.Substring(this.SelectionStart,this.SelectionLength));
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
for(int i = 1;i < DeleteLineNum ;i++)
{
_LineSelfClose.RemoveAt( currentLine + 1);
}
}
}
}
//删除键
else if(e.KeyValue == 46)
{
if(this.Text.Length > this.SelectionStart && this.Text[this.SelectionStart] == '/n')
{
_NeedReDrawColor = false;
_LineSelfClose.RemoveAt(this.GetLineFromCharIndex(this.SelectionStart) +1 );
}
else
{
_NeedReDrawColor = true;
//只需要重绘当前行
_needRedrawColorRow.startPos = GetRowBeginPos(this.SelectionStart);
_needRedrawColorRow.RowNums =1;
//获取选中的部分中有多少行,从自封闭标志数组中删除
if(this.SelectionLength > 0 )
{
int DeleteLineNum = GetNewLineNum(this.Text.Substring(this.SelectionStart,this.SelectionLength));
int currentLine = this.GetLineFromCharIndex(this.SelectionStart);
for(int i = 1;i < DeleteLineNum;i++ )
{
_LineSelfClose.RemoveAt( currentLine + 1);
}
}
}
}
//粘贴键
else if( e.Control && (e.KeyValue =='V' || e.KeyValue =='v') )
{
e.Handled = !DoWithPaste();
}
//其他输入
else
{
//2004-08-26 添加
if(e.KeyData == Keys.ProcessKey ) //输入法输入
{
_NeedReDrawColor = false;
}
else
{
_NeedReDrawColor=true;
_needRedrawColorRow.startPos = GetRowBeginPos(this.SelectionStart);
_needRedrawColorRow.RowNums =1;
if(this.Text == "" && _LineSelfClose.Count == 0 )
{
_LineSelfClose.Add(true);
}
}
}
}
private string _SingleRowRemark="";
/// <summary>
/// 单行注释符号,只允许0到2个字符
/// </summary>
public string SingleRowRemark
{
get
{
return _SingleRowRemark;
}
set
{
if(value.Length>2)
{
throw new Exception("单行注释符号,只能是一个或者2个字符");
}
else
{
_SingleRowRemark = value;
}
}
}
private string _MultiRowRemHead;
/// <summary>
/// 多行注释,头部注释符号,只允许0到2个字符
/// </summary>
public string MultiRowRemHead
{
get
{
return _MultiRowRemHead;
}
set
{
if(value.Length>2)
{
throw new Exception("多行注释,头部注释符号,只能是一个或者2个字符");
}
else
{
_MultiRowRemHead = value;
}
}
}
private string _MultiRowRemTail;
/// <summary>
/// 多行注释,尾部注释符号,只允许0到2个注释符号
/// </summary>
public string MultiRowRemTail
{
get
{
return _MultiRowRemTail;
}
set
{
if(value.Length>2)
{
throw new Exception("多行注释,头部注释符号,只能是一个或者2个字符");
}
else
{
_MultiRowRemTail = value;
}
}
}
/// <summary>
/// 引号,在这些字符之内的关键词,注释、函数不做处理
/// </summary>
public TSynQuotaCollection Quota;
/// <summary>
/// 编辑框的文本内容,重写该属性的主要目的是监视到文本框的赋值,以便及时着色
/// </summary>
public new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
_NeedReDrawColor = true;
}
}
/// <summary>
/// 当文本内容,发生变化时,是否需要着色,具体着色的行,由_needRedrawColorRow存储
/// </summary>
private bool _NeedReDrawColor=false;
/// <summary>
/// 是否允许Paint,当正在着色的时候,不允许Paint,达到禁止闪烁的目的
/// </summary>
private bool _Paint = true;
private bool _Scroll = false;
/// <summary>
/// 重新处理WM_PAINT,WM_HSCROLL,WM_VSCROLL消息,以便在着色的时候,禁止闪烁
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref System.Windows.Forms.Message m)
{
const short WM_PAINT = 0x000F;//重画
const short WM_HSCROLL = 0x0114;
const short WM_VSCROLL = 0x0115;
if (m.Msg == WM_PAINT )
{
if (_Paint)
base.WndProc(ref m);
else
m.Result = IntPtr.Zero;
}
else if( m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL )
{
//当使用this.Select时,会产生滚动,当手工着色的时候,暂时屏蔽
if (_Scroll)
{
base.WndProc(ref m);
}
else
m.Result = IntPtr.Zero;
}
else
{
base.WndProc (ref m);
this.Tag = m.Msg;
}
}
/// <summary>
/// 匹配到的单词的结构体
/// </summary>
private struct WordAndPosition
{
/// <summary>
/// 匹配的单词
/// </summary>
public string Word;
/// <summary>
/// 起始位置
/// </summary>
public int Position;
/// <summary>
/// 单词长度
/// </summary>
public int Length;
};
/// <summary>
/// 匹配到的单词集合
/// </summary>
private System.Collections.ArrayList _TheBuffer;
/// <summary>
/// 关键词高亮度属性
/// </summary>
private TSynHighlighter _KeyWordHighlighter;
public TSynHighlighter KeyWordHighlighter
{
set
{
_KeyWordHighlighter = value;
}
get
{
return _KeyWordHighlighter;
}
}
private TSynHighlighter _CommonHighlighter;
/// <summary>
/// 函数高亮度属性
/// </summary>
private TSynHighlighter _FunctionHighlighter;
public TSynHighlighter FunctionHighlighter
{
set
{
_FunctionHighlighter = value;
}
get
{
return _FunctionHighlighter;
}
}
/// <summary>
/// 注释的高亮度属性
/// </summary>
private TSynHighlighter _CommentHighlighter;
public TSynHighlighter CommentHighlighter
{
set
{
_CommentHighlighter = value;
}
get
{
return _CommentHighlighter;
}
}
/// <summary>
/// 文本发生变化的处理程序,主要是着色,根据_NeedReDrawColor和_needRedrawColorRow进行注释
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
//if (_populating)
// return;
_Paint = false;
_Scroll = false;
try
{
if(_NeedReDrawColor)
{
if(_needRedrawColorRow.startPos>=0)
{
int currentStat,selectLength;
currentStat = this.SelectionStart;
selectLength = this.SelectionLength;
int Row=GetLineFromCharIndex(_needRedrawColorRow.startPos);
int start = 0;
for(int i = 0;i < _needRedrawColorRow.RowNums && i < this.Lines.Length;i++)
{
//逐行着色
if( i == 0)
{
start = GetRowBeginPos(_needRedrawColorRow.startPos);
}
else
{
start += this.Lines[i+Row-1].Length +1;
}
MakeColorSyntax(start,i + Row);
}
//判断改变的最后一行有没有封闭
int j = _needRedrawColorRow.RowNums-1 + Row;
if( (bool)_LineSelfClose[j] == true )
{
while( j+1 < this.Lines.Length && (bool)_LineSelfClose[j] != (bool)_LineSelfClose[j+1] )
{
start +=this.Lines[j].Length +1;
MakeColorSyntax(start,j+1);
j++;
}
}
else if( j+1 < this.Lines.Length )
{
//注释行
start += this.Lines[j].Length +1;
int endPos = this.Text.IndexOf(this.MultiRowRemTail,start);
int lastRow;
if( endPos > 0)
{
this.SelectionStart = start;
this.SelectionLength = endPos - start + this.MultiRowRemTail.Length;
this.SetSelection(CommentHighlighter);
lastRow = this.GetLineFromCharIndex(endPos);
}
else
{
this.SelectionStart = start;
this.SelectionLength = this.Text.Length - start;
this.SetSelection(CommentHighlighter);
lastRow = this.Lines.Length -1;
}
for(int k = j+1; k < lastRow; k++)
{
_LineSelfClose[k] = false;
}
if( endPos > 0)
_LineSelfClose[lastRow] = true;
else
_LineSelfClose[lastRow] = false;
}
_Scroll = true;
this.SelectionStart = currentStat;
this.SelectionLength = selectLength;
_needRedrawColorRow.startPos = -1;
}
else
{
MakeColorSyntaxForAllText();
_Scroll = true;
}
}
_NeedReDrawColor =true;
}
finally
{
_Scroll = true;
_Paint = true;
}
}
/// <summary>
/// 设置文本选中项的着色属性
/// </summary>
/// <param name="Highlighter"></param>
private void SetSelection(TSynHighlighter Highlighter)
{
this.SelectionColor = Highlighter.ForeColor;
this.SelectionFont = new Font(this.Font,Highlighter.fontStyle);
}
/// <summary>
/// 文本编辑框单行着色
/// </summary>
/// <param name="start">开始字符的索引</param>
/// <param name="Row">行号</param>
private void MakeColorSyntax(int start,int Row)
{
if(Row < this.Lines.Length )
{
string s = this.Lines[Row];
if(Row > 0)
{
#region
//上一行的多行注释未结束
if((bool)_LineSelfClose[Row-1] == false )
{
int i = s.IndexOf(this.MultiRowRemTail);
if (i >= 0)
{ //在本行找到结束
//
this.SelectionStart = start;
this.SelectionLength = i + MultiRowRemTail.Length;
SetSelection(CommentHighlighter);
s = s.Substring(i+this.MultiRowRemTail.Length);
start = start + i + this.MultiRowRemTail.Length;
}
else
{
//整行都是注释
this.SelectionStart = start;
this.SelectionLength = s.Length;
SetSelection(CommentHighlighter);
//尚未封闭注释
_LineSelfClose[Row] = false;
return;
}
}
#endregion
}
#region
int count = ParseLine(s);
for (int i = 0;i < _TheBuffer.Count; i++)
{
WordAndPosition wp = (WordAndPosition)_TheBuffer[i];
//判断是否是单行注释
if((SingleRowRemark.Length > 0 && s.Length >= (wp.Position + SingleRowRemark.Length)
&& s.Substring(wp.Position,SingleRowRemark.Length) == SingleRowRemark)
|| _CommentHighlighter.IsInclude(wp.Word) )
{
#region 单行注释
//着色到一行的结束
this.Select( wp.Position + start,s.Length - wp.Position);
SetSelection(_CommentHighlighter);
_LineSelfClose[Row] = true;
return;
#endregion
}
//判断是否是多行注释的开始
else if((this.MultiRowRemHead.Length > 0 && this.MultiRowRemTail.Length > 0
&& s.Length >= (wp.Position + MultiRowRemHead.Length)
&& s.Substring(wp.Position,MultiRowRemHead.Length) == MultiRowRemHead) )
{
#region
int posCommentStart = wp.Position;
int posCommentEnd = i;
bool FindEnd = false;
i++;
#region
while( i < _TheBuffer.Count)
{
wp=(WordAndPosition)_TheBuffer[i];
if( wp.Word == this.MultiRowRemTail)
{
//找到注释的结束
FindEnd = true;
break;
}
else if(wp.Word == this.MultiRowRemTail[0].ToString() && this.MultiRowRemTail.Length == 2 )
{
i++;
if(i< _TheBuffer.Count )
{
wp = (WordAndPosition)_TheBuffer[i];
if(wp.Word == this.MultiRowRemTail[1].ToString())
{
//找到注释
FindEnd = true;
break;
}
}
}
else
{
i++;
}
}
#endregion
if(FindEnd)
{
//要是找到,
posCommentEnd = wp.Position+wp.Length;
this.Select(posCommentStart+start, posCommentEnd - posCommentStart);
SetSelection(_CommentHighlighter);
}
else //没找到结束
{
#region
this.Select(posCommentStart+start,s.Length-posCommentStart);
SetSelection(_CommentHighlighter);
_LineSelfClose[Row] = false;
return;
#endregion
}
#endregion
}
else
{
#region
TSynHighlighter c = Lookup(wp.Word);
if(c != null)
{
this.Select(wp.Position+start, wp.Length);
SetSelection(c);
}
#endregion
}
}
#endregion
_LineSelfClose[Row] = true;
}
}
/// <summary>
/// 整个文本编辑框的代码着色
/// </summary>
private void MakeColorSyntaxForAllText()
{
_Paint = false;
_Scroll = false;
try
{
string s = this.Text;
int start = 0;
this._LineSelfClose.Clear();
for(int i = 0;i < this.Lines.Length; i++)
{
this._LineSelfClose.Add(true);
//逐行着色
if( i == 0 )
start = 0;
else
start += this.Lines[i-1].Length + 1;
MakeColorSyntax(start,i);
}
_Scroll = true;
}
finally
{
_Scroll = true;
_Paint = true;
}
}
/// <summary>
/// 根据传进来单词,决定选用哪一个高亮度着色属性
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
private TSynHighlighter Lookup(string word)
{
TSynHighlighter theHighlighter = null;
if (_KeyWordHighlighter.IsInclude(word))
{
theHighlighter = _KeyWordHighlighter;
}
else if (_FunctionHighlighter.IsInclude(word))
{
theHighlighter = _FunctionHighlighter;
}
else if (_CommentHighlighter.IsInclude(word))
{
theHighlighter = _CommentHighlighter;
}
else
{
theHighlighter = _CommonHighlighter;
}
return theHighlighter;
}
/// <summary>
/// 分析单行字符串,分析的单词存放在_TheBuffer集合中
/// </summary>
/// <param name="s">单行字符串</param>
/// <returns>合计单词数</returns>
private int ParseLine(string s)
{
_TheBuffer.Clear();
int count = 0;
Regex r = new Regex(@"/w+|[^A-Za-z0-9_ /f/t/v]", RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match m;
bool FindQuota = false; //找到字符串开始的
bool FindShiftStr = false; //找到转义字符的标志
int strIndex = -1; //字符串,不需要处理的字符串的开始位置
TSynQuota q=new TSynQuota(' ',' ',"");
for (m = r.Match(s); m.Success ; m = m.NextMatch())
{
if(!FindQuota)
{
for(int i = 0;i < Quota.Count;i++)
{
TSynQuota _quota=Quota[i];
if(m.Value == _quota.QuotaHead.ToString())
{
FindQuota = true;
strIndex = m.Index;
q = _quota;
break;
}
}
if(!FindQuota)
{
WordAndPosition A = new WordAndPosition();
A.Word = m.Value;
A.Position = m.Index;
A.Length = m.Length;
_TheBuffer.Add(A);
count++;
}
}
else //已经发现有字符串
{
//找转义字符,以及结束字符
if (!FindShiftStr)
{
if(q.ShiftStr.Length > 0 && s.Length >= m.Index+q.ShiftStr.Length &&
s.Substring(m.Index,q.ShiftStr.Length) == q.ShiftStr)
{
if(m.Value!=q.ShiftStr)
m=m.NextMatch();
//找到转移字符
FindShiftStr = true;
//判断下一个字符
continue;
}
}
if(m.Value == q.QuotaTail.ToString())
{ //字符串结束
WordAndPosition A = new WordAndPosition();
A.Word = s.Substring(strIndex,m.Index-strIndex+1);
A.Position = strIndex;
A.Length = A.Word.Length;
_TheBuffer.Add(A);
count++;
FindQuota = false;
}
}
}
return count;
}
}
}
二、TSynHighlighter
using System;
namespace SQLDialog.SynEdit
{
/// <summary>
/// TSynHighlighter 的摘要说明。
/// </summary>
public class TSynHighlighter
{
public TSynHighlighter()
{
_WordList = new SQLDialog.TStringList();
_fontStyle = System.Drawing.FontStyle.Regular;
BackColor = System.Drawing.Color.White;
ForeColor = System.Drawing.Color.Black;
_IsIgnoreCase = true;
}
private bool _IsIgnoreCase;
public bool IsInclude(string word)
{
if(_IsIgnoreCase)
word=word.ToUpper();
if(_WordList.BinarySearch(word) >= 0)
return true;
else
return false;
}
/// <summary>
/// 是否忽略大小写 ///
/// </summary>
public bool IsIgnoreCase
{
set
{
if(this._WordList.Count > 0)
throw new Exception("请在类TSynHighlighter实例化后,立即更改IsIgnoreCase属性");
_IsIgnoreCase =value;
}
get
{
return _IsIgnoreCase;
}
}
/// <summary>
/// 需要着色的单词集合
/// </summary>
private SQLDialog.TStringList _WordList;
/// <summary>
/// 增加一个需要着色的单词
/// </summary>
/// <param name="theWord"></param>
public void AddOneWord(string theWord)
{
if(IsIgnoreCase)
theWord = theWord.ToUpper();
_WordList.Add(theWord);
_WordList.Sort();
}
/// <summary>
/// 增加一组需要着色的单词
/// </summary>
/// <param name="C"></param>
public void AddMultiWords(System.Collections.ICollection C)
{
if(IsIgnoreCase)
{
System.Collections.IEnumerator ir = C.GetEnumerator();
while(ir.MoveNext())
{
_WordList.Add(ir.Current.ToString().ToUpper());
}
}
else
{
_WordList.AddRange(C);
}
_WordList.Sort();
}
/// <summary>
/// 需要着色的字体样式
/// </summary>
private System.Drawing.FontStyle _fontStyle;
/// <summary>
/// 需要着色的字体样式
/// </summary>
public System.Drawing.FontStyle fontStyle
{
set
{
_fontStyle = value;
}
get
{
return _fontStyle;
}
}
/// <summary>
/// 背景
/// </summary>
public System.Drawing.Color BackColor;
/// <summary>
/// 前景色
/// </summary>
public System.Drawing.Color ForeColor;
}
}
三、TSynQuota
using System;
namespace SQLDialog.SynEdit
{
/// <summary>
/// 字符串定义,在着色的时候,作为一个整体处理
/// </summary>
public struct TSynQuota
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="PQuotaHead">字符串的头部引号字符</param>
/// <param name="PQuotaTail">字符串的尾部引号字符</param>
/// <param name="PShiftStr">转义字符串</param>
public TSynQuota(char PQuotaHead,char PQuotaTail,string PShiftStr)
{
QuotaHead = PQuotaHead;
QuotaTail = PQuotaTail;
_ShiftStr = PShiftStr;
}
/// <summary>
/// 字符串的头部引号字符,
/// </summary>
public char QuotaHead;
/// <summary>
/// 字符串的尾部引号字符,
/// </summary>
public char QuotaTail;
/// <summary>
/// 转义字符串,只能为0到2个字符
/// </summary>
private string _ShiftStr;
public string ShiftStr
{
get
{
return _ShiftStr;
}
set
{
if(value.Length>2)
throw new Exception("转义字符串,只能为一个或者连个字符");
else
_ShiftStr = value;
}
}
}
}
四、TSynQuotaCollection
using System;
namespace SQLDialog.SynEdit
{
/// <summary>
/// 字符串定义集合,一般的开发语言,只有一个
/// </summary>
public class TSynQuotaCollection
{
public TSynQuotaCollection()
{
list=new System.Collections.ArrayList();
}
private System.Collections.ArrayList list;
public void Add(TSynQuota quota)
{
list.Add(quota);
}
public TSynQuota this[int index]
{
get
{
if(index<=this.list.Count)
return (TSynQuota)this.list[index];
else
throw new Exception("超出数组的下届");
}
}
public void Clear()
{
this.list.Clear();
}
public int Count
{
get
{
return this.list.Count;
}
}
}
}
五、TCSharpSynEdit
using System;
namespace SQLDialog.SynEdit
{
/// <summary>
/// 作者:徐侠君
/// 描述:C#代码着色编辑器
/// 创建时间:2004-08-15
/// </summary>
public class TCSharpSynEdit:TSynEdit
{
public TCSharpSynEdit()
{
string[] _KeyWords =new string[]{"using","namespace","public","private","protected","internal",
"class","interface","new","int","double","char","string",
"this","byte","float","if","else","get","set",
"while","return","true","false",
"override","static","void","abstract",
"default","switch","case","struct","for",
"foreach","break","continue","object","base"};
string[] _Functions = new string[]{"ToString"};
string[] _Comments = new string[] {};
this.SingleRowRemark ="//";
this.MultiRowRemHead = "/*";
this.MultiRowRemTail = "*/";
this.Quota.Add(new TSynQuota('"','"',"///""));
this.KeyWordHighlighter.IsIgnoreCase=false;
this.KeyWordHighlighter.AddMultiWords(_KeyWords);
this.KeyWordHighlighter.ForeColor = System.Drawing.Color.Blue;
this.FunctionHighlighter.IsIgnoreCase=false;
this.FunctionHighlighter.AddMultiWords(_Functions);
this.FunctionHighlighter.ForeColor = System.Drawing.Color.Blue;
this.CommentHighlighter.AddMultiWords(_Comments);
this.CommentHighlighter.ForeColor = System.Drawing.Color.Green;
}
}
}