Imports System.ComponentModel
Imports System.Drawing.Drawing2D
<DefaultProperty("Image"), DefaultEvent("Enter")>
Public Class ImageSlider
Inherits Control
Private _timer As New System.Timers.Timer()
Private _img As Bitmap
Private _sliding As Boolean = False
Private _startTime As DateTime
Private seconds As Integer = 3
Private _speed As TimeSpan
Private _percent As Integer = 0
Private _direction As SlideDirection = SlideDirection.Left
Public Sub New()
MyBase.SetStyle(ControlStyles.ContainerControl Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.SupportsTransparentBackColor Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.OptimizedDoubleBuffer, True)
_speed = New TimeSpan(0, 0, 0, seconds, 0)
AddHandler _timer.Elapsed, AddressOf TimerTick
End Sub
<DefaultValue(GetType(SlideDirection), "Left")>
<Description("Gets or set slide direction.")>
<Category("Appearance")>
Public Property Direction As SlideDirection
Get
Return _direction
End Get
Set(value As SlideDirection)
_direction = value
Invalidate()
End Set
End Property
<DefaultValue(GetType(Bitmap), "")>
<Description("Gets or set iamge.")>
<Category("Appearance")>
Public Property Image() As Bitmap
Get
Return _img
End Get
Set(value As Bitmap)
_img = value
Me.Invalidate()
End Set
End Property
Protected Overloads Overrides ReadOnly Property DefaultSize() As Size
Get
Return New Size(150, 150)
End Get
End Property
Public Sub Slide()
Me.Slide(42)
End Sub
Private Sub Slide(interval As Integer)
If interval > 100 Then
interval = 100
End If
_timer.Interval = interval
_timer.Enabled = True
_percent = 0
_startTime = DateTime.Now
_sliding = True
Invalidate()
End Sub
Private Sub StopSliding()
_sliding = False
_timer.Enabled = False
End Sub
Protected Function DrawImage(g As Graphics, rect As Rectangle, dirction As SlideDirection)
If _img IsNot Nothing Then
Dim mx As Matrix = CType(Nothing, Matrix)
If _sliding Then
Select Case Direction
Case SlideDirection.Down
mx = New Drawing2D.Matrix(1, 0, 0, 1, 0, (rect.Height * _percent / 100) - rect.Height)
Exit Select
Case SlideDirection.Left
mx = New Drawing2D.Matrix(1, 0, 0, 1, -(rect.Width * _percent / 100) + rect.Width, 0)
Exit Select
Case SlideDirection.Right
mx = New Drawing2D.Matrix(1, 0, 0, 1, (rect.Width * _percent / 100) - rect.Width, 0)
Exit Select
Case SlideDirection.Top
mx = New Drawing2D.Matrix(1, 0, 0, 1, 0, -(rect.Height * _percent / 100) + rect.Height)
Exit Select
End Select
g.Transform = mx
g.DrawImage(_img, rect, 0, 0, _img.Width, _img.Height, GraphicsUnit.Pixel)
If mx IsNot Nothing Then
mx.Dispose()
End If
Else
g.DrawImage(_img, rect, 0, 0, _img.Width, _img.Height, GraphicsUnit.Pixel)
End If
End If
End Function
Private Sub TimerTick(source As Object, e As System.Timers.ElapsedEventArgs)
Dim span As TimeSpan = DateTime.Now - _startTime
_percent = CSng((100.0F / _speed.TotalSeconds * span.TotalSeconds))
If _percent > 100 Then
_percent = 100
End If
Invalidate()
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
Dim rect As Rectangle = ClientRectangle
Using borderPen As Pen = New Pen(If(Me.BackColor.GetBrightness < 0, SystemColors.ControlLight, SystemColors.ControlDark))
e.Graphics.DrawRectangle(borderPen, rect.X, rect.Y, rect.Width - 1, rect.Height - 1)
End Using
rect.X += 2
rect.Y += 2
rect.Width -= 4
rect.Height -= 4
Me.DrawImage(e.Graphics, rect, _direction)
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
MyBase.Dispose(disposing)
RemoveHandler _timer.Elapsed, AddressOf TimerTick
If _timer IsNot Nothing Then
_timer.Dispose()
_timer = Nothing
End If
End Sub
Public Enum SlideDirection
Left
Right
Top
Down
End Enum
End Class