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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job