Mouse Set and Restore - December 2003

I published a few tips on CompuServe in 1997 and 1998. Mouse Set was one of those tips. Click here to view other tips published during that period. I recently revisited the code - made a few improvements, created a VB ..net version and publish it as the December 2003 tip-of-the-month.

Visual Basic does not provide a means to "push" and "pop" the screen mouse pointer. It is not unusual to set the mouse pointer to an hourglass and find that it has not been reset after the "wait" process completes. No doubt you have observed an application that fails to restore the mouse pointer. You see an hourglass instead of an arrow. I was inspired by an article in Inside VB, December 1994, Page 15, from Harry Gilbert. I developed functions which you may find useful.

Function MouseSet

In each of the following examples the first applies to VB6 and the second to VB .Net.

Function MouseSet "pushes" the current Screen Mouse Pointer onto a stack then returns the setting supplied as the only argument. The prototype:

Screen.MousePointer = MouseSet(newMouse)
Cursor.Current = MouseSet(newCursor)

An example:

Screen.MousePointer = MouseSet(vbHourGlass)
Cursor.Current = MouseSet(Cursors.WaitCursor)

Function MouseRestore

Function MouseRestore "pops" the last Screen Mouse Pointer from the stack. There are no arguments. The prototype and an example:

Screen.MousePointer = MouseRestore()
Cursor.Current = MouseRestore()

MouseSet with no arguments also restores the mouse setting, for example:

Screen.MousePointer = MouseSet()
Cursor.Current = MouseSet()

Clear the Stack

In the VB6 version, use global variable gciMouseClearStack with MouseSet to force the stack to be cleared, for example:

MouseSet gciMouseClearStack

In the VB .Net version, call the following subroutine to clear the stack - erase the array:

MouseClearStack

How it Works

VB6 Version - Function MouseSet converts the current screen mouse pointer index to a one byte string and concatenates it to a 'stacking' string. It then sets the screen mouse pointer to the passed argument. Function MouseRestore reverses the process. It removes the right most character from the string, converts it to an integer and returns that value.

VB .Net Version - Function MouseSet adds the cursor to a Cursor array. MouseRestore retrieves the last Cursor from the array and reduces the array's upper bound by one. A 'try catch' routine returns the default cursor if there are any errors.

MouseDmo

MouseDmo.vbp [VB6] or MouseDmo.sln [VB .Net] demonstrates setting the screen mouse pointer to all valid values then restores them.

Mouse Set Demo
The Size_NE_SW Mouse cursor is captured in this screen shot.

Using MouseSet

Add modMouseSet.Bas for VB6 or modMouseSet.vb for VB .net to your application. Add MouseSet and MouseRestore functions [or MouseSet with no arguments] to the start and end of processes. Examples:

VB6
Sub MyProcess()
    Screen.MousePointer = MouseSet(vbHourGlass)
    MySecondProcess 'start another process
    Screen.MousePointer = MouseSet()
End Sub

Sub MySecondProcess()
    Screen.MousePointer = MouseSet(vbHourGlass)
    '... a second process ...
    Screen.MousePointer = MouseSet()
End Sub
VB .Net
Sub MyProcess()
    Cursor.Current = MouseSet(Cursors.WaitCursor)
    MySecondProcess  'start another process
    Cursor.Current = MouseSet()
End Sub

Sub MySecondProcess
    Cursor.Current = MouseSet(Cursors.WaitCursor)
    '... a second process ...
    Cursor.Current = MouseSet()
End Sub

Download VB Code

Download Source Code
VB6 Source - MouseDmoVB6.Zip [7k]
VB .Net Source - MouseDmoVB7.zip [14k] .net

Previous Tip   Tip Index    Next Tip

Home

Updated 2003/12/19