Imports System.Drawing
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public Class utsLabel
    'Inherits System.Windows.Forms.UserControl
    Inherits System.Windows.Forms.Label

    'UserControl 重写释放以清理组件列表。
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If

        Finally
            MyBase.Dispose(disposing)
        End Try

        If myThread IsNot Nothing AndAlso myThread.IsAlive Then
            '关闭线程
            myThread.Abort()
        End If
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    Dim myThread As Thread = Nothing '定义一个全局变量存储线程对象

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改它。  
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False 'Momo 2023-12-23 禁用跨线程检查，危险，少用

        Me.Label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(0, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(100, 23)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Label1"
        Me.Label1.TextAlign = ContentAlignment.MiddleLeft
        Me.ResumeLayout(False)

        If myThread IsNot Nothing AndAlso myThread.IsAlive Then
            '如果已经有线程正在运行，则先关闭该线程
            myThread.Abort()
        End If

        '创建新的线程并开始运行
        myThread = New Thread(AddressOf dispModeExe)
        myThread.Start()

    End Sub

    Friend WithEvents Label1 As Windows.Forms.Label


#Region "控件属性"

    Private _dispMode As Integer                '显示模式 1-普通，2-闪烁，3-滚动，4-自动增加字符
    Private _visible As Boolean                 '是否显示’
    Private _text As String                     '显示文本内容’
    Private _dispFlag As Boolean                '闪烁flag’
    Private _modePara As String                 '模式参数’

    Private modePara_Timeout_mS As Integer = 0  '闪烁超时’
    Private color_1 As Color = Color.Red        '闪烁颜色1’
    Private color_2 As Color = Color.White      '闪烁颜色2’
    Private AddCharacter As String = "."        '自增字母’
    Private AddCharMaxLen As Integer = 32       '自增最大长度’
    Private modeParaArry() As String            '参数数组’
    Private AddCharCnt As Integer = 0           '自增计数’


    Public Property modePara As String
        Get
            Return _modePara
        End Get
        Set(value As String)
            _modePara = value
        End Set
    End Property


    Public Property dispMode As Integer
        Get
            Return _dispMode
        End Get
        Set(value As Integer)
            _dispMode = value
            RunMode()
        End Set
    End Property
#End Region

#Region "控件属性实现"
    ''' <summary>
    ''' 解析模式，并且确定有关参数
    ''' </summary>
    Private Sub RunMode()
        If IsNumeric(_dispMode) = False Then Return

        If String.IsNullOrEmpty(_modePara) = True Then
            Return
        Else
            modeParaArry = _modePara.Split(CType(":", Char()))
        End If

        Dim paraArryCnt As Integer = modeParaArry.Count


        Select Case _dispMode
            Case 1 '默认

            Case 2 '闪烁
                If paraArryCnt = 3 Then
                    modePara_Timeout_mS = CInt(modeParaArry(0))
                    color_1 = HexStringToColor(modeParaArry(1).Trim)
                    color_2 = HexStringToColor(modeParaArry(2).Trim)
                Else
                    Return
                End If

            Case 3 '滚动


            Case 4 '自动增加字符
                If paraArryCnt = 3 Then
                    modePara_Timeout_mS = CInt(modeParaArry(0))
                    AddCharacter = modeParaArry(1).Trim
                    AddCharMaxLen = CInt(modeParaArry(2).Trim)
                    AddCharCnt = 0
                Else
                    Return
                End If


            Case Else

        End Select

    End Sub


    ''' <summary>
    ''' 常驻线程，执行各种Mode
    ''' </summary>
    Private Async Sub dispModeExe()
        While True
            Await Task.Delay(modePara_Timeout_mS)
            _dispFlag = Not _dispFlag
            ModeProcessing()
        End While
    End Sub

    Private Sub ModeProcessing()
        If IsNumeric(_dispMode) = False Then Return

        Select Case _dispMode
            Case 1 '默认

            Case 2 '闪烁
                If _dispFlag Then
                    ForeColor = color_1
                Else
                    ForeColor = color_2
                End If
            Case 3 '滚动

            Case 4 '自动增加字符
                If AddCharCnt < AddCharMaxLen Then
                    AddCharCnt += 1
                    _text = _text & AddCharacter

                    MyBase.Text = _text '调用基类的Text属性的setter方法，将处理后的值赋值给基类的Text属性
                    Me.Refresh() ' 在修改Text后调用Refresh方法刷新控件

                    'If _dispFlag Then
                    '    ForeColor = color_1
                    'Else
                    '    ForeColor = color_2
                    'End If
                End If

            Case Else

        End Select

    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        ' 重写OnPaint事件处理程序，这里不需要进行任何操作
        MyBase.OnPaint(e)
    End Sub

    Protected Overloads Sub show()
        MyBase.Show()
    End Sub

    Public Overrides Property Text As String
        Get
            'Return MyBase.Text '返回基类的Text属性值
            Return _text
        End Get

        Set(value As String)
            _text = value
            MyBase.Text = _text '调用基类的Text属性的setter方法，将处理后的值赋值给基类的Text属性
            Me.Refresh() ' 在修改Text后调用Refresh方法刷新控件
        End Set
    End Property


    Private Function HexStringToColor(hexString As String) As Color
        Dim val As Integer = Convert.ToInt32(hexString, 16)
        Return Color.FromArgb(val >> 16 And &HFF, val >> 8 And &HFF, val And &HFF)
    End Function

#End Region

End Class

Public Class TickCounter
    <DllImport("kernel32.dll")>
    Private Shared Function GetTickCount() As Integer
    End Function

    Public Shared _msLast As Long
    Public Shared ReadOnly Property ElapsedMilliseconds As Long
        Get
            Return CLng(GetTickCount())
        End Get
    End Property

    Public Shared Property msLast As Long
        Get
            Return _msLast
        End Get
        Set(value As Long)
            _msLast = value
        End Set
    End Property

End Class
