using DevExpress.XtraEditors; using System.Drawing; namespace ZJ_BYD.Untils { /// /// TextEdit自定义文本样式 /// public class CustomHighlightText { #region 颜色配置 /// /// 关键字颜色 /// private static Color KeywordColor { get; set; } = Color.FromArgb(0, 176, 240); /// /// 字符串颜色 /// private static Color StringColor { get; set; } = Color.FromArgb(0, 176, 80); /// ///被注释的字符颜色 /// private static Color CommentColor { get; set; } = Color.FromArgb(127, 127, 127); /// /// 数字颜色 /// private static Color NumberColor { get; set; } = Color.FromArgb(247, 150, 70); /// /// 日志info标识的颜色 /// private static Color LogInfoColor { get; set; } = Color.FromArgb(0, 176, 240); /// /// 日志err标识的颜色 /// private static Color LogErrColor { get; set; } = Color.FromArgb(240, 0, 0); #endregion /// /// 自定义文本显示 /// /// /// public static void OnCustomHighlightText(object sender, TextEditCustomHighlightTextEventArgs e) { //HighlightKeyword(e); //HighlightNumbers(e); //HighlightStrings(e); // HighlightComment(e); HighlightLog(e); } #region [注释]的字符样式 private static void HighlightComment(TextEditCustomHighlightTextEventArgs e) { string text = e.Text; int index = text.IndexOf("--"); int index2 = text.IndexOf("//"); if (index != -1 || index2 != -1) e.HighlightRange(index, text.Length - index, CommentColor); } #endregion #region 关键字样式 private static string Keywords = "select;delete;from;insert;into;table;primary;key;varchar;integer;drop;if;exists;create;use;set;values;update;where;database;not;null"; private static string[] keywordList; private static string[] KeywordList { get { if (keywordList == null) keywordList = Keywords.Split(';'); return keywordList; } } /// /// 关键字样式 /// /// private static void HighlightKeyword(TextEditCustomHighlightTextEventArgs e) { //e.HighlightWords("with", (block) => { // block.BackColor = Color.Yellow; // block.Padding = new Padding(5, 0, 5, 0); //}); for (int i = 0; i < KeywordList.Length; i++) { e.HighlightWords(KeywordList[i], KeywordColor); } } #endregion #region 数字样式 /// /// 数字样式 /// /// private static void HighlightNumbers(TextEditCustomHighlightTextEventArgs e) { string text = e.Text; int length = text.Length; int startWordIndex = 0; for (int i = 0; i < length; i++) { var ch = text[i]; if (char.IsWhiteSpace(ch) || char.IsSeparator(ch) || char.IsPunctuation(ch)) { if (startWordIndex != -1 && i - startWordIndex > 0) e.HighlightRange(startWordIndex, i - startWordIndex, NumberColor); startWordIndex = i + 1; continue; } if (!char.IsNumber(ch)) startWordIndex = -1; } if (startWordIndex != -1) e.HighlightRange(startWordIndex, length - startWordIndex, NumberColor); } #endregion private static void HighlightLog(TextEditCustomHighlightTextEventArgs e) { //e.HighlightWords("NG", (block) => //{ // block.ForeColor = LogErrColor; //}); //整行变色 string text = e.Text; int index = text.IndexOf("NG"); if (index != -1) e.HighlightRange(0, text.Length, LogErrColor); } } }