What is wrong with this VBA code: userForm get hand ico for pointer on hover

ammer

New Member
Joined
Feb 27, 2024
Messages
6
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
Salutes,
GDI / GDIP / OpenGL, windows API, and I still find it hard tackling with. Search results for changing cursor icon to hand on hovering on a link_label didn't afford except this solution came up by jaafar-tribak .

I get this error from compiler :
Compiler Error
The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with PtrSafe attribute.

1709088446926.png


● If someone could help converting it into 64-bit, with many thanks
The code is picked from this Thread on this Forum, right at the end of the thread (Userform Hand Mouse Icon when scroll over buttons)

--------------------------

VBA Code:
Option Explicit
 
Public WithEvents cmb As CommandButton

Private Type POINTAPI
    X As Long
    Y As Long
End Type
 
Private Type MSG
    hwnd As Long
    message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type
 
Private Declare Function PeekMessage Lib "user32" _
Alias "PeekMessageA" _
(ByRef lpMsg As MSG, ByVal hwnd As Long, _
ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long, _
ByVal wRemoveMsg As Long) As Long
 
Private Declare Function WaitMessage Lib "user32" _
() As Long
 
Private Declare Function PostMessage Lib "user32.dll" _
Alias "PostMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
 
Private Declare Function GetCursorPos Lib "user32.dll" _
(ByRef lpPoint As POINTAPI) As Long
 
Private Declare Function SetCursor Lib "user32" _
(ByVal hCursor As Long) As Long
 
Private Declare Function LoadCursor Lib "user32.dll" _
Alias "LoadCursorA" _
(ByVal hInstance As Long, _
ByVal lpCursorName As Long) As Long
 
Private Declare Function FindWindow Lib "user32.dll" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function WindowFromPoint Lib "user32" _
(ByVal xPoint As Long, ByVal yPoint As Long) As Long

Private Const PM_NOREMOVE As Long = &H0
Private Const WM_SETCURSOR As Long = &H20

Private bMouseHooked As Boolean
Private bStop As Boolean
Private bCurOverButton As Boolean

Private lCur As Long
Private oFrm As UserForm

Public Sub ChangeCurOf(frm As UserForm, ByVal Button As CommandButton, ByVal Cur As Long)
    Dim tMsg As MSG
    Dim tPt As POINTAPI
    Dim lCurID As Long
    Dim hwnd As Long
    If bMouseHooked Then Exit Sub
    bMouseHooked = True
    bStop = False
    lCur = Cur
    Set oFrm = frm
    hwnd = _
    FindWindow(vbNullString, frm.Caption)
    If Not bCurOverButton Then Exit Sub
    Do
        GetCursorPos tPt
        If WindowFromPoint(tPt.X, tPt.Y) <> hwnd Then bStop = True
        SetCursor LoadCursor(0, Cur)
        WaitMessage
        If PeekMessage _
        (tMsg, hwnd, _
        WM_SETCURSOR, WM_SETCURSOR, PM_NOREMOVE) Then
            PostMessage hwnd, WM_SETCURSOR, 0, 0
        End If
        DoEvents
    Loop Until bStop
End Sub


Private Sub cmb_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    bStop = True
    bMouseHooked = False
    bCurOverButton = True
    Call ChangeCurOf(oFrm, cmb, lCur)
End Sub
 
You guys do know that userform controls have a MouseIcon property? ;)
Hi Rory,

Yes definitely. Once we set the MousePointer Property to fmMousePointerCustom, we are free to use our own icons.

I have always found it strange and rather inconvinient that MsForms controls don't provide a Hand icon among its set of cursors. The missing hand icon is definitely more on demand than a SizeNESW icon !

Still, I prefer using code. It is more fun (and makes the code more portable) :)
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Yes. that's called 'defensive programming' .

I am gald I could be of help and thanks for the feedbak ammer.
In my opinion
=============
● One should be cautious using Do… while loop
● On Error Resume Next , is not recommended in production, since you don’t when and where an Err is triggered.
● DoEvents, should not be used except during testing, since users might press Esc during an actual Runtime processing is in progrees.
● On Error Goto 0 , statement is totally avoided by programmers.

Maybe there has to be a different way rather being risking these bulletins !
★ If I reached to something I’ll inform.
 
Upvote 0
In my opinion
=============
● One should be cautious using Do… while loop
● On Error Resume Next , is not recommended in production, since you don’t when and where an Err is triggered.
● DoEvents, should not be used except during testing, since users might press Esc during an actual Runtime processing is in progrees.
● On Error Goto 0 , statement is totally avoided by programmers.
Maybe there has to be a different way rather being risking these bulletins !
★ If I reached to something I’ll inform.

Overall, I agree with every point you made above. However, there are times when using loops for a short period of time can be very beneficial and won't have much negative impact on performance.

The loop I used here is short lived. It only runs while the mouse pointer is hovering over the hyperlink-label control and exits as soon as the mouse pointer is moved away from the control. I would certainly not use a loop that would run for a longer time.

Calling DoEvents is necessary here to allow user interaction and to allow other windows messages to be processed. I could have used an asynchronous timer instead but I think that would be overkill. There are easy ways around the issue of when pressing the ESC key, such as setting the excel EnableCancelKey Property or using the RegisterHotKey api function.

On Error Resume Next is ok and can somtimes be our friend and be turned to our advantage as long as it is coded properly in small portions of the code and placed in strategic locations whenever we expect some runtime error to happen.

On Error Goto 0 , statement is totally avoided by programmers.
On Error Goto 0 is used to disable the error handler. Otherwise we run the risk of masking unintended errors in all susequent code.
 
Upvote 0
Overall, I agree with every point you made above. However, there are times when using loops for a short period of time can be very beneficial and won't have much negative impact on performance.

The loop I used here is short lived. It only runs while the mouse pointer is hovering over the hyperlink-label control and exits as soon as the mouse pointer is moved away from the control. I would certainly not use a loop that would run for a longer time.

Calling DoEvents is necessary here to allow user interaction and to allow other windows messages to be processed. I could have used an asynchronous timer instead but I think that would be overkill. There are easy ways around the issue of when pressing the ESC key, such as setting the excel EnableCancelKey Property or using the RegisterHotKey api function.

On Error Resume Next is ok and can somtimes be our friend and be turned to our advantage as long as it is coded properly in small portions of the code and placed in strategic locations whenever we expect some runtime error to happen.


On Error Goto 0 is used to disable the error handler. Otherwise we run the risk of masking unintended errors in all susequent code.
.....................
Thanks, @Jaafar Tribak
I took these notes into consideration updating my list of experience vocabulary. For meanwhile I’ll disappear for a while to give some progress in working hours, so that to feel satisfied spending some time freely in forums reviewing extra additions. These issues gave the chance meeting new fellows, and that’s the most important thing after all.

By the way I updated my dashboard with a logo I created (Stealth Proof). I really liked it, after all this long time spending resolving hidden unknown crash problems encountered by excel individuals.

I’ll be in touch.​
 
Upvote 0
You guys do know that userform controls have a MouseIcon property? ;)
I was thinking exactly the same thing :)
That said, I learn so much from Jaafar's solutions.
 
Upvote 0

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top