Automatically position mouse cursor above CommandButton or control in UserForm

  • This is very cool



    and in the userform activate event


    Code
    '// Pass userform (Needed to get handle) and the
         '// control. This works for any controi with a
         '// Left/Top property
        CenterMouseOver Me, ComboBox1


    note
    the code must go in the Activate event rather than the Initialize event.


    It seems the userform does not really exist as an object (as far as Windows is concerned, at least) until after the Initialize event has completed - so the code will not work correctly in the Initialize event.


    Once you get under the hood with the API you run into loads of little issues like this... All part of the way VBA is structured.

  • Re: Automatically position mouse cursor above CommandButton or control in UserForm


    A slight revision - revised to include aTrue/False parameter to determine where the mouse was.


    If True, the pointer is moved in steps to the control - the cursor moving across the screen is very noticable. Only 1 little issue. If Mouse trails are turned on, sometimes 'phantom' pointers ar eleft on screen. These disappear immediately the mouse is moved.

  • Re: Automatically position mouse cursor above CommandButton or control in UserForm


    For the above mentioned code aslo add

    Code
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  • Re: Automatically position mouse cursor above CommandButton or control in UserForm


    Thanks Pike - lots of good stuff in that... will check it out closer a little later on... I'm on 64 bit, so I had to update the function calls with "ptrsafe". A royal pain the M$ neck.

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Automatically position mouse cursor above CommandButton or control in UserForm


    Might be a good time to mention conditional compilation:


    Code
    #If Win64 Then
        Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPtr
        Public Declare PtrSafe Sub Sleep Lib "kernel32" Alias "sleep" (ByVal dwMilliseconds As LongPtr)
        '// ....rest of declarations
    #Else
        Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
        '// ....rest of declarations
    #End If


    There's an MSDN article here about this type of conditional compilation should anyone wish to read up on it. :)

  • Re: Automatically position mouse cursor above CommandButton or control in UserForm


    Neither of those actually need LongPtr though:

    Code
    #If Win64 Then    
    Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
        Public Declare PtrSafe Sub Sleep Lib "kernel32" Alias "sleep" (ByVal dwMilliseconds As Long)
         '// ....rest of declarations
    #Else
        Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
         '// ....rest of declarations
    #End If

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!