Imports System.ComponentModel
Public Class ScrollLabel
Inherits Label
#Region "Private Fields"
Private _Timer As New Timer()
Private _MirrorPosition As Single = 0.0F
Private _Position As Single = 0.0F
#End Region
#Region "Properties"
''' <summary>
''' The scroll speed in pixels / second. Use negative values for scrolling to the left.
''' </summary>
Private _ScrollSpeed As Single
Public Property ScrollSpeed() As Single
Get
Return _ScrollSpeed
End Get
Set(ByVal value As Single)
_ScrollSpeed = value
End Set
End Property
<Browsable(False)> _
Public Overloads Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
''' <summary>
''' The scrolling text.
''' </summary>
Private _DisplayText As String
Public Property DisplayText() As String
Get
Return _DisplayText
End Get
Set(ByVal value As String)
_DisplayText = value
End Set
End Property
''' <summary>
''' The separation between the real text and the mirror text.
''' </summary>
Private _MirrorTextSeparation As Integer
Public Property MirrorTextSeparation() As Integer
Get
Return _MirrorTextSeparation
End Get
Set(ByVal value As Integer)
_MirrorTextSeparation = value
End Set
End Property
Public Enum VerticalAlignment
Top
Center
Bottom
End Enum
Private _TextAlignment As VerticalAlignment
Public Property TextAlignment() As VerticalAlignment
Get
Return _TextAlignment
End Get
Set(ByVal value As VerticalAlignment)
_TextAlignment = value
Me.Invalidate()
End Set
End Property
<Browsable(False)> _
Public Overloads Overrides Property TextAlign() As System.Drawing.ContentAlignment
Get
Return MyBase.TextAlign
End Get
Set(ByVal value As System.Drawing.ContentAlignment)
MyBase.TextAlign = value
End Set
End Property
#End Region
Public Sub New()
Me.DisplayText = Me.Text
Me.Text = [String].Empty
_Timer.Interval = 50
_Timer.Enabled = True
AddHandler _Timer.Tick, AddressOf _Timer_Tick
Me.AutoSize = False
Me.TextAlignment = VerticalAlignment.Center
Me.ScrollSpeed = -1.0F
Me.MirrorTextSeparation = 0
End Sub
Private Sub _Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.DesignMode Then
_Position += Me.ScrollSpeed
_MirrorPosition += Me.ScrollSpeed
Me.Invalidate()
End If
End Sub
Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim size As SizeF = e.Graphics.MeasureString(Me.DisplayText, Me.Font)
Dim y As Single = 0.0F
Select Case Me.TextAlignment
Case VerticalAlignment.Top
y = 0.0F
Case VerticalAlignment.Bottom
y = Me.Height - size.Height
Case VerticalAlignment.Center
y = (Me.Height - size.Height) / 2.0F
End Select
Dim sep As Single = Math.Max(Me.Width, size.Width) + Me.MirrorTextSeparation
Dim mirrorPosition As Single = _Position + sep
If _Position < Me.Width - sep - size.Width Then
_Position += sep
End If
e.Graphics.DrawString(Me.DisplayText, Me.Font, New SolidBrush(Me.ForeColor), _Position, y)
e.Graphics.DrawString(Me.DisplayText, Me.Font, New SolidBrush(Me.ForeColor), mirrorPosition, y)
End Sub
End Class