Create Automatic Hourglass Cursors

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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several