125 lines
4.2 KiB
C#
125 lines
4.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Media;
|
||
|
||
namespace 智慧物流软件系统
|
||
{
|
||
/// <summary>
|
||
/// 富文本框扩展类
|
||
/// </summary>
|
||
public static class RichTextBoxTool
|
||
{
|
||
/// <summary>
|
||
/// 清空内容
|
||
/// </summary>
|
||
public static void ClearText(this RichTextBox richTextBox)
|
||
{
|
||
try
|
||
{
|
||
richTextBox.Dispatcher.Invoke((Action)delegate
|
||
{
|
||
//清空文本
|
||
richTextBox.Document.Blocks.Clear();
|
||
//滚动到开头
|
||
richTextBox.ScrollToVerticalOffset(0);
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 插入文本
|
||
/// </summary>
|
||
/// <param name="richTextBox"></param>
|
||
/// <param name="brush">颜色</param>
|
||
/// <param name="txt">内容</param>
|
||
private static void AppendText(this RichTextBox richTextBox, Brush brush, string txt)
|
||
{
|
||
try
|
||
{
|
||
richTextBox.Dispatcher.Invoke((Action)delegate
|
||
{
|
||
var p = new Paragraph(); //Paragraph 类似于 html 的 P 标签
|
||
var r = new Run(txt); //Run 是一个 Inline 的标签
|
||
p.Inlines.Add(r);
|
||
p.LineHeight = 8;
|
||
p.Foreground = brush;//设置字体颜色
|
||
richTextBox.Document.Blocks.Add(p);
|
||
if (richTextBox.Document.Blocks.Count > 1000)
|
||
{
|
||
richTextBox.Document.Blocks.Remove(richTextBox.Document.Blocks.FirstBlock);
|
||
}
|
||
//滚动到末尾
|
||
richTextBox.ScrollToVerticalOffset(richTextBox.Document.Blocks.Count * 200);
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 插入Title(棕色),为:######Title######
|
||
/// </summary>
|
||
public static void AppendTile(this RichTextBox richTextBox, string txt)
|
||
{
|
||
AppendText(richTextBox, Brushes.Brown, "#####" + txt + "#####");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 插入信息(蓝色)
|
||
/// </summary>
|
||
/// <param name="addTime">是否在开头追加时间信息,如:HH:mm:ss.fff=>txt</param>
|
||
public static void AppendInfo(this RichTextBox richTextBox, string txt, bool addTime = false)
|
||
{
|
||
if (string.IsNullOrEmpty(txt))
|
||
return;
|
||
|
||
if (addTime)
|
||
AppendText(richTextBox, Brushes.Blue, $"{DateTime.Now.ToString("HH:mm:ss.fff")}=>{txt.Replace("\r", "\\r").Replace("\n", "\\n")}");
|
||
else
|
||
AppendText(richTextBox, Brushes.Blue, txt.Replace("\r", "\\r").Replace("\n", "\\n"));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 插入信息(深绿色)
|
||
/// </summary>
|
||
/// <param name="addTime">是否在开头追加时间信息,如:HH:mm:ss.fff=>txt</param>
|
||
public static void AppendResult(this RichTextBox richTextBox, string txt, bool addTime = false)
|
||
{
|
||
if (string.IsNullOrEmpty(txt))
|
||
return;
|
||
|
||
if (addTime)
|
||
AppendText(richTextBox, Brushes.Blue, $"{DateTime.Now.ToString("HH:mm:ss.fff")}=>{txt}");
|
||
else
|
||
AppendText(richTextBox, Brushes.ForestGreen, txt);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 插入错误(鲜红色)
|
||
/// </summary>
|
||
/// <param name="addTime">是否在开头追加时间信息,如:HH:mm:ss.fff=>txt</param>
|
||
public static void AppendErr(this RichTextBox richTextBox, string txt, bool addTime = false)
|
||
{
|
||
if (string.IsNullOrEmpty(txt))
|
||
return;
|
||
|
||
if (addTime)
|
||
AppendText(richTextBox, Brushes.Blue, $"{DateTime.Now.ToString("HH:mm:ss.fff")}=>{txt}");
|
||
else
|
||
AppendText(richTextBox, Brushes.Tomato, txt);
|
||
}
|
||
}
|
||
|
||
}
|