You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.5 KiB
C#

using DevExpress.XtraEditors;
using System.Drawing;
namespace ZJ_BYD.Untils
{
/// <summary>
/// TextEdit自定义文本样式
/// </summary>
public class CustomHighlightText
{
#region 颜色配置
/// <summary>
/// 关键字颜色
/// </summary>
private static Color KeywordColor { get; set; } = Color.FromArgb(0, 176, 240);
/// <summary>
/// 字符串颜色
/// </summary>
private static Color StringColor { get; set; } = Color.FromArgb(0, 176, 80);
/// <summary>
///被注释的字符颜色
/// </summary>
private static Color CommentColor { get; set; } = Color.FromArgb(127, 127, 127);
/// <summary>
/// 数字颜色
/// </summary>
private static Color NumberColor { get; set; } = Color.FromArgb(247, 150, 70);
/// <summary>
/// 日志info标识的颜色
/// </summary>
private static Color LogInfoColor { get; set; } = Color.FromArgb(0, 176, 240);
/// <summary>
/// 日志err标识的颜色
/// </summary>
private static Color LogErrColor { get; set; } = Color.FromArgb(240, 0, 0);
#endregion
/// <summary>
/// 自定义文本显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
/// <summary>
/// 关键字样式
/// </summary>
/// <param name="e"></param>
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 数字样式
/// <summary>
/// 数字样式
/// </summary>
/// <param name="e"></param>
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);
}
}
}