First, 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 {/// // / author: Xu Xia Jun /// created: 2004-08-15 /// increase in the processing of the quoted string Amendment 1> /// increase the processing of changing the text, Mainly in the input of the keyboard 2004-08-19 modification 2> /// Added processing for multi-line comments 2004-08-21 2004-08-22 Complete modification 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; _LineseelfClose = new arraylist (); this.detecturns = false; this.text = ""; _Lineseelfclose.add (true);} /// /// Each line element There is a cross-line note /// summary> private system.collections.arraylist _LineseelfClose; /// ////// summary> /// param> /// returns> private int getrowbeginpos (int index) {// Looking for the current line of start position INT POS = Index; while ((POS> 0) && (this.text [POS-1]! = '/ n')) POS -;
return pos; OK} /// /// change occurred /// summary> private struct NeedRedrawColorRow {public int startPos; // start position public int RowNums; // Head} private NeedRedrawColorRow _needRedrawColorRow; / // /// cut, rewritten the method, mainly to monitor the cut to reload /// 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 /// judgment can be pasted /// Summary> /// returns> public bool canpaste () {if (this.canpaste (dataformats.stringFormat) || this.canpaste (dataformats.genicodext) || this.canpaste (DataFormats.getFormat) (DataFormats.text))) {Object Pastedata = Clipboard.getDataObject (). GetData (TypeOf (String)); if (Pastedata! = NULL) Return True; Else Return False;} Else Return false;} /// /// Handling Paste Data //// //// Returns> Private Bool DowithPaste () {if (this.canpaste (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.selectionstart, this.selectionLength);} int currentline = this.getLineFromcharindex (this.SelectionStart); // Paste data line number, number of data lines Multi For (int K = 1; k <= i - j; k ) {if (currentline i> = _Lineseelfclose.count) _LineseelfClose.Add (true); else _LineseelfClose.Insert (Currentline i, true);} / / Paste the number of data lines, less than the selected data line count, for (int K = 1; k <= j - i; k ) {_Lineseelfclose.Removeat (Currentline i);} return true;} else {return false; }}}}}} /// /// Rewind the paste method to monitor the changes of editing content /// summary> public new void paste () {downithPaste (); base. Paste ();} /// /// Rewind the paste method to monitor the editing content Change /// summary> /// This method is actually invalid, just to monitor changes in text editing content param> public new void Paste (System.windows. Forms.DataFormats.Format ClipFormat) {this.paste ();} /// / / / According to the string content, how many rows /// summary> /// 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);} /// /// monitor, keyboard input to update code in time Coloring /// summary> /// param> private void onkeyeventhandler (Object sender, keyeventargs e) {_needredRawColorrow .StartPOS = -1; _needredrawcolor = true; // If you press the Enter key, you need to color this row and the last line IF (E.KEYVALUE == 13) {
IF (THIS.TEXT.LENGTH> this.SelectionStart && this.text [this.selectionstart] == '/ n') {// At the end of a line, press Enter _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') {// In the beginning of a line, press the carriage return _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; // two lines changes int currentLine = this.GetLineFromCharIndex (this.SelectionStart); if (currentLine == this.Lines.Length - 1) {_Lineseelfclose [currentline]);} else {_lineseelfclose.insert (currentline 1, _Lineseelfclose [currentline]);}}} //}}} //}}} // ELS ELSE IF (E.KEYVALUE == 8) {/ / Foliage in a row, retreat IF (this.SelectionStart> = 1 && this.text [this.selectionStart-1] == '/ n' && this. SelectionLength == 0) {_NeedReDrawColor = false; _LineSelfClose.RemoveAt (this.GetLineFromCharIndex (this.SelectionStart));} else {_NeedReDrawColor = true; // only need to redraw the current row _needRedrawColorRow.startPos = GetRowBeginPos (this.SelectionStart); _needredrawcolorrow.rownums = 1; // Get how many rows in the selected section, remove if IF (this.selectionLength> 0) {int deletenum = getNewlinenum (this.text.substring "from the selected section, deleting 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);}}}} // Remove key 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; // only need to redraw the current row _needRedrawColorRow.startPos = GetrowBeginpos (this.SelectionStart); _needRedrawColorrow.rownums = 1; // How many rows in the selected section, remove if IF from the selection flag array (this.selectionLength> 0) {int deletelinenum = getNewlinenum (THIS.TEXT.SUBSTRING (this.SelectionStart, this.SelectionLength)); int currentLine = this.GetLineFromCharIndex (this.SelectionStart); for (int i = 1; i
E.handLeD =! dowithPaste ();} // Other input else {// 2004-08-26 Add if (e.keydata == keys.processKey) // input method {_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 = ""; /// /// single line annotation symbol, only 0 to 2 characters /// summary> public string singlelowremark {get {return _singLerowRemark;} set {if (Value.Length> 2) {Throw new Exception ("single-line annotation symbol, can only be one or 2 characters");} else {_singlerowremark = value;}}} private string _multirowremhead; /// /// multi-line comments, Head Note symbol, only 0 to 2 characters /// summary> public string multirowremhead {get {return_multirowremhead;} set {i (value.length> 2) {throw new exception ("Multi-line comment, head Ministry Note Symbol, can only be one or 2 characters ");} else {_MultirowRemhead = value;}}} private string _multirowremtail; /// /// Multi-line comment, tail annotation symbol, only 0 to 2 annotation symbols /// summary> public string multirowremtail {Get { Return_multirowremtail;} set {if ("Value.Length> 2) {throw new exception (" Multi-line annotation, head comment symbol, only one or 2 characters ");} else {_MultirowreMtail = value;}}} /// /// quotes, keywords, comments, and functions within these characters /// summary> public tsynquotacollection quota;
/// /// Edit the text content of the box, rewriting the main purpose of this property is to monitor the assignment of the text box, in order to close coloring //// summary> public new string text {get {return base. TEXT;} set {base.text = value; _needRedrawColor = true;}} /// / / summary> private bool _needredrawcolor = false; /// /// Whether to allow Paint, when it is colored, Paint does not allow PAINT to achieve the purpose of disabling flashing /// summary> private bool _Paint = TRUE "Private bool _scroll = false; /// /// Reprocessing WM_PAINT, WM_HSCROLL, WM_VSCROLL message, in order to blame flash /// summary> /// param> protected override void WndProc (ref System.Windows.Forms.Message m) {const short WM_PAINT = 0x000F; // redraw 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) {// When using this.select, scrolling is generated, when hand-colored, temporarily shield IF (_Scroll) {base.WndProc Ref m);} else m.Result = intptr.zero;} else {base.wndproc (ref M); this.tag = m.msg;}}}}} /// /// Match the structure Body /// summary> private struct WordandPosition {/// /// Match /// summary> public string word; /// /// start position ///// summary> public int position; /// /// word length /// summary> public int length;}; /// /// Matching the word collection /// < / summary> private system.collections.ArrayList_thebuffer;
/// /// keyword high brightness properties /// summary> private TSynHighlighter _KeyWordHighlighter; public TSynHighlighter KeyWordHighlighter {set {_KeyWordHighlighter = value;} get {return _KeyWordHighlighter;}} private TSynHighlighter _CommonHighlighter; /// /// high luminance function attributes /// summary> private TSynHighlighter _FunctionHighlighter; public TSynHighlighter FunctionHighlighter {set {_FunctionHighlighter = value;} get {return _FunctionHighlighter;}} /// /// annotations high brightness properties /// summary> private TSynHighlighter _CommentHighlighter; public TSynHighlighter CommentHighlighter handler {set {_CommentHighlighter = value;;} get {return _CommentHighlighter}} /// /// text changes, mainly Coloring, based on _needredRawcolorrow /// summary> /// 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 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;
THISTSETSELECTION (CommentHighlight); Lastrow = this.Lines.Length -1;} for (int K = J 1; K 0) _LineseelfClose [ Lastrow] = true; else _lineseelfclose [lastrow] = false;
} _Scroll = true; this.SelectionStart = currentStat; this.SelectionLength = selectLength; _needRedrawColorRow.startPos = -1;} else {MakeColorSyntaxForAllText (); _Scroll = true;}} _NeedReDrawColor = true;} finally {_Scroll = true; _Paint = True;}} /// /// Setting the coloring property of text selection /// summary> /// param> private void setselection (tsynhighlight highlighter) { This.SelectionColor = highlighter.ForeColor; this.selectionFont = new font (this.font, highlighter.fontstyle);} /// /// text edit box single line color /// summary> /// < Param name = "start"> Start character's index param> /// line number param> Private void makecolorsyntax (int start, int {) {if (ROW 0) {#Region // Multi-line annotation of the upper line does not end if ((BOOL) _LINSelfclose [ROW-1] == false) {INT i = s.indexof (this.multirowremtail); if (i> = 0) {// find the end of the Bank // this.SelectionStart = start; this.SelectionLength = i MultiRowRemTail.Length; SetSelection (CommentHighlighter); s = s.Substring (i this.MultiRowRemTail.Length); start = start i this.MultiRowRemTail.Length;} else {// the entire row is annotated this.SelectionStart = start; this.SelectionLength = s.Length; SetSelection (CommentHighlighter); // not yet closed Note _LineSelfClose [row] = false; return }} #Endregion}
#Region int count = parseline (s); for (int i = 0; i <_thebuffer.count; i ) {WordandPosition WP = (WordandPosition) _thebuffer [i]; // Judgment is a single line notes IF ((SinglerowRemark.length > 0 && S.Length> = (wp.position singlerowremark.Length) && s.Substring (wp.position, singlerowremark.Length) == singlerowremark || _commentHighlight.isinClude (wp.word) {#Region single-line comment // Coloring to a row of this.select (wp.position start, s.length - wp.position); setSelection (_CommentHighlight); _lineseelfclose [row] = true; return; #ENDREGON} // Decision is more Note the start 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 <_thebuff Er.count) {wp = (wordandPosition) _thebuffer [i]; if (wp.word == this.multirowremtail) {// finds the end of the comment Findendnd = true; Break;} else f (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 ()) {// find the comment findend = true;
}}} Else {i ;}} #endregion if (FindEnd) {// If found, posCommentEnd = wp.Position wp.Length; this.Select (posCommentStart start, posCommentEnd - posCommentStart); SetSelection (_CommentHighlighter);} else // not find the end of {#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);} #ENDREGON}} #ENDREGON _LINESELFCLOSE [ROW] = true; }}}}} /// /// The code coloring /// summary> private void makecolorsyntaxForalText () {_paint = false; _Scroll = false; try {string s = this.text; int Start = 0; THIS._LI NESelfClose.clear (); for (int i = 0; i
} /// /// According to the password, decide which high-brightness coloring property /// summary> /// param> /// returns> private tsynhighlighter lookup (string word) {tsynhighlight thehighlight = 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 thehighlight;} /// /// Analysis of the single string, the analysis word is stored in _Thebuffer collection //// summary> /// single line String param> /// total word number 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; // Find the Bool Findshiftstr = false started by the string; // Find the turn Sign INT STRINDEX = -1; // String, the start position of the string that does not need to be processed Tsynquota Q = New Tsynquota ('', ', ""); for (m = r.match (s) ; M.Success; m = m.nextMatch ()) {if (! findue quota) {for (int i = 0; i
}}} If (! Findquota) {WordandPosition a = new wordandPosition (); a.Word = m.Value; a.position = m.index; a.length = m.Length; _Thebuffer.Add (a); count ;} } Else // has found a string {// find escape character, and end character 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 ();
/ / Find Transfer Character FindshiftStr = true; // Judgment Next Character Continue;}}}} (M.Value == Q.quotatail.toString ()) {// String ends 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;}
}
Second, TSYNHighLighter
Using system;
Namespace SqlDialog.synedit {/// /// Tsynhighlight's summary description. /// 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;} /// // / Whether to ignore the case ///////////////////////////// / summary> public bool isignoreCase {set {if (this._wordlist.count> 0) throw new exception ("Please After instantification of class tsynhighlight, change the isignoreCase property "); _ignorecase = value;} get {return_isignorecase;}} /// /// need coloring word collection /// summary> private sqldialog.tstringlist _WordList;
/// /// Add a word /// summary> /// param> public void address (isignorecase) Theword (ISIGNORECASE) theWord = theword.toupper (); _wordlist.add (theword); _WordList.Sort ();} /// /// Add a set of colored words /// summary> /// param> public void addmultiwords (system.collections.icollection c) {if (isignorecase) {system.collections.ienumerator r = c.GeteNumerator (); while (ir.moveNext ()) {_wordlist. Add (Ir.current.toString (). TouPper ());}} else {_wordlist.addrange (c);} _wordlist.sort ();} /// /// Need coloring font style /// / summary> private system.drawing.fontstyle _fontstyle; /// /// Required font style /// summary> public system.drawing.fontstyle fontstyle {set {_fontstysty = value;} Get {RETURN _FONTSTYLE;}} /// /// background /// summary> public system.drawing.color backcolor; /// /// foreground color /// summary> public SYSTEM.D Rawing.color forecolor;}}
Namespace Sqldialog.Synedit {/// /// String definition, in coloring, as a whole processing /// summary> public struct tsynquota {/// /// constructor / // summary> /// Strings head quotation marking character param> /// string tail quotation character param> / // escape string param> public TSynQuota (char PQuotaHead, char PQuotaTail, string PShiftStr) {QuotaHead = PQuotaHead; QuotaTail = PQuotaTail; _ShiftStr = PShiftStr;} /// /// String head quotation marks, /// summary> public char quotahead; /// /// string of tail quotation characters, /// summary> public char quotatail; / // /// escape strings, can only be 0 to 2 characters /// summary> private string _shiftstr; public string shiftstr {get {return _shiftstr;} set {if (value.length> 2) throw new exception ("escape string, only one or even characters"); else _shiftstr = value;}}}} four, Tsynquotacollection
Using system;
Namespace sqldialog.synedit {/// /// string definition collection, general development language, only one /// summary> public class tsynquotacole {public tsynquotacolection () {list = new system.collections.ArrayList (); Private system.collections.Arraylist list; public void add (tsota) {list.add (quota);} public tsynquota this [int index] {get {ified (index <= this.list.count) Return (Tsynquota) this.list [index]; Else Throw new Exception ("Out of the next" next ");}} public void clear () {this.list.clear ();} public int count {get {Return this. List.count;}}}} 5. tcsharpsynedit
Using system;
namespace SQLDialog.SynEdit {/// /// OF: Xia Jun Xu /// Description: C # code coloring editor // Create Time: 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 "," "" "," 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 KeywordHighlight.Ignorecase = false; this.keywordhighlight.addmultiwords (_keywords); this.keywordHighlight.ForeColor = system.drawing.color.bl ue; 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;}}}