Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Mar 12, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Create Automatic Hourglass Cursors

If you create an ActiveX control that uses a custom MousePointer property, place the control on a form, and at run time change the Screen.MousePointer property to an hourglass. You'll see that the mouse pointer reverts to the ActiveX control's custom cursor while over the control.

I discovered this when I created an ActiveX control that appeared as a hyperlink and could be placed on a VB form. I changed the MousePointer for the control to a pointing hand cursor, similar to the cursor in Internet Explorer. When I used the control in a project, the control's Click event changed the screen's MousePointer to an hourglass, but this had no effect while the mouse was over the ActiveX control.

To prevent this inconsistency, disable the form when you show the hourglass, but enable it when the hourglass is turned off. While the form is disabled, the MousePointer property of the ActiveX control no longer takes precedence over the Screen.MousePointer property. Use this generic clsHourglass class module to change the mouse pointer and disable the current window:

Option Explicit

	Private mintOldPointer As Integer
	Private mlngHwnd As Long
	Private Declare Function EnableWindow Lib "user32" _
		(ByVal hWnd As Long, ByVal fEnable As Long) As Long

	Private Sub Class_Initialize()
		On Error Resume Next
		'  Save current mouse pointer
		mintOldPointer = Screen.MousePointer
		'  Change to hourglass
		Screen.MousePointer = vbHourglass
		'  Save the window handle and disable the 
		'  current window.
		mlngHwnd = Screen.ActiveForm.hWnd
		EnableWindow mlngHwnd, 0
	End Sub

	Private Sub Class_Terminate()
		On Error Resume Next
		'  Set pointer to old pointer
		Screen.MousePointer = mintOldPointer
		'  Enable the previously visible window
		EnableWindow mlngHwnd, 1
	End Sub
Now, instead of explicitly changing the mouse pointer to an hourglass and back and manually disabling and re-enabling forms, use this code at the beginning of your event procedures:
	Dim objHourglass as clsHourglass
	Set objHourglass = New clsHourglass
With this approach, the Initialize event changes the mouse pointer to an hourglass and disables the current form when the procedure creates the objHourglass. The hourglass can't accidentally be left on because when the objHourglass variable loses scope, the Terminate event fires, which returns the mouse pointer to its original state and re-enables the form if it's still loaded.

By using the EnableWindow API call, you can't reload a form accidentally if it was unloaded during the time the objHourglass object had life. That could happen if you used the syntax "frmCurrent.Enabled = True". Multiple instances of objHourglass won't cause the mouse pointer to flicker, because the mouse pointer's current state is checked and saved before setting it to an hourglass.

If the user keeps clicking on the form while the hourglass is shown, the extra clicks are discarded because the form isn't enabled. And unlike earlier versions of VB, a form doesn't deactivate when disabled, so the title bar doesn't flicker.

Craig Randall
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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