Make A Spinning Picture

'Add 1 Timer Control and 4 Picture Boxes to your form.
'Set the Timer Interval property to 1. If you want to slow the animation, increase the
'value of the Interval property.
'Add pictures to Picture1 and Picture2 Picture Boxes. If you want a Background for
'the animation, Add the background picture to Picture3 PictureBox.
'All the Pictures should be at the same size.

'Insert the following code to your form:

Dim dAngle As Double
Const NUM_TURNS = 36
Const PI = 3.14159265358979
Const CENTER_X = 4000
Const SRCCOPY = &HCC0020
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As _
Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal _
nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As _
Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Sub Form_Load()
Picture1.AutoSize = True
Picture2.AutoSize = True
Picture3.Width = Picture1.Width
Picture3.Height = Picture1.Height
Picture4.Width = Picture1.Width
Picture4.Height = Picture1.Height
Picture3.Visible = False
Picture1.Visible = False
Picture2.Visible = False
Picture3.AutoRedraw = True
Picture1.AutoRedraw = True
Picture2.AutoRedraw = True
Picture4.AutoRedraw = True
Picture1.BorderStyle = 0
Picture2.BorderStyle = 0
Picture3.BorderStyle = 0
Picture4.BorderStyle = 0
End Sub

Private Sub Timer1_Timer()
Picture3.Cls
If Cos(dAngle * PI / 180) >= 0 Then
Call StretchBlt(Picture3.hdc, (Picture1.Width - Abs(Cos(dAngle * PI / 180) * _
Picture1.Width)) / (2 * Screen.TwipsPerPixelX), 0, Abs(Cos(dAngle * PI / 180) * _
Picture1.Width) / Screen.TwipsPerPixelX, Picture1.Height / Screen.TwipsPerPixelY, _
Picture1.hdc, 0, 0, Picture1.Width / Screen.TwipsPerPixelX, Picture1.Height / _
Screen.TwipsPerPixelY, SRCCOPY)
ElseIf Cos(dAngle * PI / 180) < 0 Then
Call StretchBlt(Picture3.hdc, (Picture2.Width - Abs(Cos(dAngle * PI / 180) _
* Picture2.Width)) / (2 * Screen.TwipsPerPixelX), 0, Abs(Cos(dAngle * PI / 180) _
* Picture2.Width) / Screen.TwipsPerPixelX, Picture2.Height / Screen.TwipsPerPixelY, _
Picture2.hdc, 0, 0, Picture2.Width / Screen.TwipsPerPixelX, Picture2.Height / _
Screen.TwipsPerPixelY, SRCCOPY)
End If
Call BitBlt(Picture4.hdc, 0, 0, Picture3.Width / Screen.TwipsPerPixelX, _
Picture3.Height / Screen.TwipsPerPixelY, Picture3.hdc, 0, 0, SRCCOPY)
Picture4.Refresh
dAngle = dAngle + 360 / NUM_TURNS
dAngle = dAngle Mod 360
End Sub

Go Back