Visual Basic Programming Code Examples Visual Basic > Graphics Games Programming Code Examples Animating the movement of forms Animating the movement of forms The following code demonstrates how to animate the movement of VB forms. Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function SetRect Lib "User32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Private Declare Function DrawAnimatedRects Lib "User32" (ByVal hWnd As Long, ByVal idAni As Long, lprcFrom As RECT, lprcTo As RECT) As Long 'Purpose : Animates the movement of a form from one position to another 'Inputs : oForm The form to move. ' lStartLeft The starting left position of the animation sequence. ' lStartTop The starting top position of the animation sequence. ' lDestinationLeft The ending left position of the animation sequence. ' lDestinationTop The ending top position of the animation sequence. 'Outputs : N/A 'Notes : All coordinates refer to the top left hand corner of the form Sub FormAnimateWindow(oForm As Form, lStartLeft As Long, lStartTop As Long, lDestinationLeft As Long, lDestinationTop As Long) Dim tStartPos As RECT, tDestinationPos As RECT Dim lScreenWidth As Long, lScreenHeight As Long Const IDANI_OPEN = &H1 Const IDANI_CLOSE = &H2 Const IDANI_CAPTION = &H3 'Set the source/destination rectangles SetRect tStartPos, lStartLeft, lStartTop, lStartLeft + oForm.Width / Screen.TwipsPerPixelX, lStartTop + oForm.Height / Screen.TwipsPerPixelY SetRect tDestinationPos, lDestinationLeft, lDestinationTop, lDestinationLeft + oForm.Width / Screen.TwipsPerPixelX, lDestinationTop + oForm.Height / Screen.TwipsPerPixelY 'Draw the animatation DrawAnimatedRects oForm.hWnd, IDANI_OPEN Or IDANI_CAPTION, tStartPos, tDestinationPos 'Move the form to the new position oForm.Move lDestinationLeft * Screen.TwipsPerPixelX, lDestinationTop * Screen.TwipsPerPixelY, oForm.Width, oForm.Height End Sub 'Demonstration routine (place inside a VB form) Private Sub Form_Click() Dim lScreenWidth As Long, lScreenHeight As Long Static bUpToDown As Boolean lScreenWidth = Screen.Width / Screen.TwipsPerPixelX lScreenHeight = Screen.Height / Screen.TwipsPerPixelY If bUpToDown Then 'Move a form from the top left of the screen to the bottom right FormAnimateWindow Me, 0, 0, lScreenWidth - 150, lScreenHeight - 150 bUpToDown = False Else 'Move a form from the bottom right of the screen to the top left FormAnimateWindow Me, lScreenWidth, lScreenHeight, 0, 0 bUpToDown = True End If End Sub