January 15, 2021
Hot Topics:

Using Animated Cursors in Visual Basic

  • By Karl Moore, Sam Huggill
  • Send Email »
  • More Articles »

Here's a great way to add a little extra panache to your application — use animated cursors!

When your program is busy whizzing and whirring, why not entertain the user by turning their cursor into a spinning globe, a wiggling starfish or a floating rocket?

You can, with this piece of code. Just run the StartAnimatedCursor method, passing the filename of an animated cursor file. When you want the animated cursor to stop and the old-style cursor to reappear, just call the RestoreCursor method.

Animated cursors can be found all over the place. Do a hard drive search for all files ending in .ANI. You'll probably find a couple in Windows\Cursors.

If you don't already have it, you might also want to try installing 'Desktop Themes' (Windows 98 only) via the Control Panel, Add/Remove Programs, Windows Setup. That ships with around twenty or so animated cursors.

Note: If you only pass the StartAnimatedCursor method the animated cursor filename (ie, globe.ani) as opposed to its full path (ie, c:\windows\cursors\globe.ani), the code will attempt to find the .ANI file in the App.Path.


StartAnimatedCursor ("c:\windows\cursors\globe.ani")
MsgBox "Start long process here..."


' This code to be inserted into a module

Private Declare Function LoadCursorFromFile Lib "user32" Alias _
    "LoadCursorFromFileA" (ByVal lpFileName As String) As Long
Private Declare Function SetSystemCursor Lib "user32" _
    (ByVal hcur As Long, ByVal id As Long) As Long
Private Declare Function GetCursor Lib "user32" () As Long
Private Declare Function CopyIcon Lib "user32" (ByVal hcur As Long) As Long

Private Const OCR_NORMAL = 32512

Public lngOldCursor As Long, lngNewCursor As Long

Public Sub StartAnimatedCursor(AniFilePath As String)

    ' Create a copy of the current cursor,
    ' for Windows NT compatibility
    lngOldCursor = CopyIcon(GetCursor())
    ' Check the passed string, if it contains
    ' a solid file path, then load the cursor
    ' from file. If not, add the App.Path,
    ' *then* load cursor...
    If InStr(1, AniFilePath, "\") Then
        lngNewCursor = LoadCursorFromFile(AniFilePath)
        lngNewCursor = LoadCursorFromFile(App.Path & _
            "\" & AniFilePath)
    End If
    ' Activate the cursor
    SetSystemCursor lngNewCursor, OCR_NORMAL
End Sub

Public Sub RestoreLastCursor()

    ' Restore last cursor
    SetSystemCursor lngOldCursor, OCR_NORMAL

End Sub

Page 1 of 2

This article was originally published on November 19, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date