Microsoft & .NETVisual BasicUsing Animated Cursors in Visual Basic

Using Animated Cursors in Visual Basic

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 WindowsCursors.

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:windowscursorsglobe.ani), the code will attempt to find the .ANI file in the App.Path.

Usage

StartAnimatedCursor ("c:windowscursorsglobe.ani")
    
MsgBox "Start long process here..."
    
RestoreLastCursor

Code

' 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)
    Else
        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

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 WindowsCursors.

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:windowscursorsglobe.ani), the code will attempt to find the .ANI file in the App.Path.

Usage

StartAnimatedCursor ("c:windowscursorsglobe.ani")
    
MsgBox "Start long process here..."
    
RestoreLastCursor

Code

' 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)
Else
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

"x

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories