175 lines
5.4 KiB
VB.net
175 lines
5.4 KiB
VB.net
|
|
Imports System.Threading
|
|||
|
|
|
|||
|
|
Public Class RichTextPrint
|
|||
|
|
'输出线程
|
|||
|
|
Public OutputThread As Thread
|
|||
|
|
'输出等级
|
|||
|
|
Public Shared level As Integer = 1 '日志级别 0:不输出日志 1:输出日志2:输出基础日志和错误信息
|
|||
|
|
|
|||
|
|
'接收器
|
|||
|
|
Public UpdateUIqueue As Queue(Of (Control, RichTextNodeConfig, lev As Integer))
|
|||
|
|
|
|||
|
|
'默认输出控件
|
|||
|
|
Public G_DefaultControl As Control
|
|||
|
|
|
|||
|
|
|
|||
|
|
Sub New(DefaultControl As Control)
|
|||
|
|
G_DefaultControl = DefaultControl
|
|||
|
|
'初始化线程
|
|||
|
|
OutputThread = New Thread(AddressOf RunUpdateUI)
|
|||
|
|
'OutputThread.IsBackground = True
|
|||
|
|
OutputThread.Start()
|
|||
|
|
UpdateUIqueue = New Queue(Of (Control, RichTextNodeConfig, lev As Integer))
|
|||
|
|
End Sub
|
|||
|
|
'关闭线程
|
|||
|
|
Sub Close()
|
|||
|
|
OutputThread.Abort()
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
Public Sub AddQueue(c As Control, rl As RichTextNodeConfig, Optional lev As Integer = 0)
|
|||
|
|
If IsNothing(UpdateUIqueue) OrElse IsNothing(c) OrElse IsNothing(rl) Then Return
|
|||
|
|
UpdateUIqueue.Enqueue((c, rl, lev))
|
|||
|
|
End Sub
|
|||
|
|
Public Sub AddQueue(rl As RichTextNodeConfig, Optional lev As Integer = 0)
|
|||
|
|
If IsNothing(UpdateUIqueue) OrElse IsNothing(G_DefaultControl) OrElse IsNothing(rl) Then Return
|
|||
|
|
UpdateUIqueue.Enqueue((G_DefaultControl, rl, lev))
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
|
|||
|
|
Private Sub RunUpdateUI()
|
|||
|
|
While True
|
|||
|
|
|
|||
|
|
If IsNothing(UpdateUIqueue) OrElse UpdateUIqueue.Count = 0 Then
|
|||
|
|
Thread.Sleep(1)
|
|||
|
|
Continue While
|
|||
|
|
End If
|
|||
|
|
If UpdateUIqueue.Count > 0 Then
|
|||
|
|
Dim str As (Control, RichTextNodeConfig, Integer) = UpdateUIqueue.Dequeue()
|
|||
|
|
OutputLogsToTheControl(str.Item1, str.Item2, str.Item3)
|
|||
|
|
End If
|
|||
|
|
Thread.Sleep(1)
|
|||
|
|
End While
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
Delegate Function Deleg_OutputLogsToTheControl(c As Control, config As RichTextNodeConfig, Controltype As Integer) As Boolean
|
|||
|
|
'输出日志到控件
|
|||
|
|
''' <summary>
|
|||
|
|
''' ''输出日志到控件
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="c">控件 </param>
|
|||
|
|
''' <param name="config">日志</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
Public Shared Function OutputLogsToTheControl(c As Control, config As RichTextNodeConfig, Controltype As Integer) As Boolean
|
|||
|
|
Try
|
|||
|
|
If c.InvokeRequired Then
|
|||
|
|
Dim dev As New Deleg_OutputLogsToTheControl(AddressOf OutputLogsToTheControl)
|
|||
|
|
c.Invoke(dev, New Object() {c, config, Controltype})
|
|||
|
|
Else
|
|||
|
|
If Controltype >= level Then
|
|||
|
|
Dim nc As RichTextBox = c
|
|||
|
|
'获取富文本字符串末端位置
|
|||
|
|
Dim index As Integer = nc.TextLength
|
|||
|
|
If (Not IsNothing(config.strdata)) AndAlso config.strdata.Length > 0 Then
|
|||
|
|
config.logstr = config.logstr & SharedFunction.ByteToHexString(config.strdata)
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
Dim strtxt = config.logstr & vbCrLf
|
|||
|
|
'设置光标位置
|
|||
|
|
nc.SelectionStart = index
|
|||
|
|
'添加文本
|
|||
|
|
nc.AppendText(strtxt)
|
|||
|
|
'设置选择区域
|
|||
|
|
nc.Select(index, strtxt.Length)
|
|||
|
|
'设置选择区域颜色
|
|||
|
|
nc.SelectionColor = config.logstrColor
|
|||
|
|
'设置选择区域字体
|
|||
|
|
nc.SelectionFont = config.logstrFont
|
|||
|
|
'取消选中区域
|
|||
|
|
nc.DeselectAll()
|
|||
|
|
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
|
|||
|
|
End If
|
|||
|
|
Catch ex As Exception
|
|||
|
|
|
|||
|
|
End Try
|
|||
|
|
|
|||
|
|
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
'字符串附加时间戳
|
|||
|
|
Public Shared Function AddTimeStamps(str As String) As String
|
|||
|
|
Return Now.ToString("yyyy-MM-dd HH:mm:ss:fff") & " " & str
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
End Class
|
|||
|
|
Public Class RichTextNodeConfig
|
|||
|
|
'日志信息
|
|||
|
|
Public logstr As String
|
|||
|
|
'日志信息颜色
|
|||
|
|
Public logstrColor As Color
|
|||
|
|
'日志信息字体
|
|||
|
|
Public logstrFont As Font
|
|||
|
|
'日志信息字体大小
|
|||
|
|
'Public logstrFontSize As Integer
|
|||
|
|
''日志信息字体粗细
|
|||
|
|
'Public logstrFontBold As Boolean
|
|||
|
|
''日志信息字体斜体
|
|||
|
|
'Public logstrFontItalic As Boolean
|
|||
|
|
''日志信息字体下划线
|
|||
|
|
'Public logstrFontUnderline As Boolean
|
|||
|
|
''日志信息字体删除线
|
|||
|
|
'Public logstrFontStrikeout As Boolean
|
|||
|
|
''日志信息字体对齐方式
|
|||
|
|
'Public logstrFontAlign As StringAlignment
|
|||
|
|
'日志信息字体背景色
|
|||
|
|
Public logstrFontBackColor As Color
|
|||
|
|
Public strdata As Byte()
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 文本、颜色、Font
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="args"></param>
|
|||
|
|
Sub New(ParamArray args() As Object)
|
|||
|
|
If args.Length > 0 Then
|
|||
|
|
logstr = RichTextPrint.AddTimeStamps(args(0).ToString)
|
|||
|
|
|
|||
|
|
If args.Length > 1 Then
|
|||
|
|
|
|||
|
|
logstrColor = args(1)
|
|||
|
|
If args.Length > 2 Then
|
|||
|
|
logstrFont = args(2)
|
|||
|
|
Else
|
|||
|
|
logstrFont = New Font("宋体", 8)
|
|||
|
|
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
If args.Length > 3 Then
|
|||
|
|
strdata = args(3)
|
|||
|
|
Else
|
|||
|
|
strdata = Nothing
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
Else
|
|||
|
|
logstrColor = Color.Black
|
|||
|
|
End If
|
|||
|
|
Else
|
|||
|
|
logstr = "输空文本"
|
|||
|
|
logstrColor = Color.Black
|
|||
|
|
logstrFont = New Font("宋体", 12)
|
|||
|
|
End If
|
|||
|
|
|
|||
|
|
End Sub
|
|||
|
|
|
|||
|
|
End Class
|