Alex Yahkin's Animation Control in VB.NET | | In addition to working on content for this site, I help Devbuzz , a site dedicated to handheld development. Recently, someone posted a question in our forums about using System.Drawing in the Compact Framework. I referred him to Alex Yahkin's killer Article on creating an Animation control using the Compact Framework. The person then told me they were a VB.NET progammer and couldn't read the article because it was in C#. Anyway, I asked Alex if I could post a translation in VB.NET and he was kind enough to agree. Please note that this is not my work..all I did is translate Alex's C# code into VB.NET. However, I see a lot of VB.NET programmers asking for translations (for some reason you don't see many C# programmers asking for VB.NET code to be translated... but that's another issue) and figured I'd post it. BTW, if you aren't familiar with Alex's work, he's one of the reasons that the CF has succeeded to the degree it has. I encourage you to check out his site as well as OpenNetCF if you are trying to learn the Compact Framework, because the CF wouldn't be nearly as popular as it is without the work of these guys (and Ginny ;-)) . If you need the whole project, I'll have it posted at Devbuzz in our Compact Framework section later this weekend.
Anyway, here's the code...
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Public Enum FrameLayouts As Integer
Horizontal = 0
Vertical = 1
End Enum
Public Class AnimationCtl
Inherits System.Windows.Forms.Control
Private _bitmap As bitmap
Private _frameCount As Integer
Private _frameWidth As Integer
Private _frameHeight As Integer
Private _graphics As Graphics
Private _currentFrame As Integer = 0
Private _loopCount As Integer = 0
Private _loopCounter As Integer = 0
Private fTimer As Windows.Forms.Timer
Public Property Bitmap() As Bitmap
Get
Return _bitmap
End Get
Set(ByVal Value As Bitmap)
_bitmap = Value
End Set
End Property
Public AnimateCtl()
'{
' //Cache the Graphics object
' graphics = this.CreateGraphics();
' //Instanciate the Timer
' fTimer = new System.Windows.Forms.Timer();
' //Hook up to the Timer's Tick event
' fTimer.Tick += new System.EventHandler(this.timer1_Tick);
'}
Public Sub New()
_graphics = Me.CreateGraphics
fTimer = New System.Windows.Forms.Timer
AddHandler fTimer.Tick, AddressOf timer1_tick
End Sub
Private Sub timer1_tick(ByVal Sender As Object, ByVal e As System.EventArgs)
If (_loopCount - 1) Then
Me.DrawFrame()
Else
If (_loopCount = _loopCounter) Then
fTimer.Enabled = True
Else
Me.DrawFrame()
End If
End If
End Sub
Public Sub StartAnimation(ByVal frWidth As Integer, ByVal DelayInterval As Integer, ByVal LoopCount As Integer)
_frameWidth = frWidth
_loopCount = LoopCount
_frameCount = Bitmap.Width / _frameWidth
_frameHeight = Bitmap.Height
Me.Size = New Size(_frameWidth, _frameHeight)
fTimer.Interval = DelayInterval
fTimer.Enabled = True
End Sub
Public Sub StopAnimation()
fTimer.Enabled = False
End Sub
Public Sub DrawFrame()
If (_currentFrame < _frameCount - 1) Then
_currentFrame += 1
Else
_loopCounter += 1
_currentFrame = 0
End If
Draw(_currentFrame)
End Sub
Private Sub Draw(ByVal iframe As Integer)
Dim xLocation As Integer = iframe * _frameWidth
Dim rect As Rectangle = New Rectangle(xLocation, 0, _frameWidth, _frameHeight)
_graphics.DrawImage(_bitmap, 0, 0, rect, GraphicsUnit.Pixel)
End Sub
End Class
| |