What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleAnimate a picture moving
DescriptionThis example shows how to animate a picture moving in Visual Basic 6. It uses a Timer to move the picture a little at a time until it reaches its desired destination.
Keywordsanimate, animation, BitBlt
Subroutine DrawPicture uses the BitBlt API function to draw an image on a background. It first redraws the part of the background currently covered by the image. Then it draws a mask of the image using the raster operation MERGEPAINT and draws the image itself using the SRCAND raster operation.
' Draw the picture at (CurX, CurY).
Private Sub DrawPicture()
    ' Fix the part of the image that was covered.
    BitBlt picCanvas.hDC, _
        OldX, OldY, PicWid, PicHgt, _
        picHidden.hDC, OldX, OldY, SRCCOPY
    OldX = CurX
    OldY = CurY

    ' Paint on the new image.
    BitBlt picCanvas.hDC, _
        CurX, CurY, PicWid, PicHgt, _
        picXMask.hDC, 0, 0, MERGEPAINT
    BitBlt picCanvas.hDC, _
        CurX, CurY, PicWid, PicHgt, _
        picX.hDC, 0, 0, SRCAND

    ' Update the display.
End Sub
When the user clicks on the form, the MouseUp event handler starts moving the image to its new location. First it calculates where the image's upper left corner needs to move. It determines the total distance the image must move from its current position and the incremental X and Y distances it should move for each Timer event. It then enables the tmrMove Timer.
' Start moving the image.
Private Sub picCanvas_MouseUp(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
Dim dist As Single

    ' See where to move the image.
    NewX = x - PicWid / 2
    NewY = y - PicHgt / 2
    If NewX < 0 Then NewX = 0
    If NewX > Xmax Then NewX = Xmax
    If NewY < 0 Then NewY = 0
    If NewY > Ymax Then NewY = Ymax

    ' Calculate the moving offsets.
    Dx = NewX - CurX
    Dy = NewY - CurY
    DistToMove = Sqr(Dx * Dx + Dy * Dy)
    Dx = Dx / DistToMove * MOVE_OFFSET
    Dy = Dy / DistToMove * MOVE_OFFSET

    ' Enable the move timer.
    tmrMove.Enabled = True
End Sub
The tmrMove control's Timer event handler subtracts the increment move distance from the total distance to move. If the result is <= 0, then the program moves the image to its final position and disables the Timer. If there is still some distance to move, then the program moves the image a bit.

In either case, the event handler calls DrawPicture to draw the image at its new location.

' Move the image closer to its destination.
Private Sub tmrMove_Timer()
    DistToMove = DistToMove - MOVE_OFFSET
    If DistToMove <= 0 Then
        CurX = NewX
        CurY = NewY
        tmrMove.Enabled = False
        CurX = CurX + Dx
        CurY = CurY + Dy
    End If

End Sub
My book Visual Basic Graphics Programming describes lots of other graphical techniques including several other methods for animating images.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.