Moving The Cursor By Code

  • Follow


Access 2007

I want to be able to move the cursor using VBA code.  I put the following in
a module:

    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,
lpRect As RECT) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As
Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
ByVal y As Long) As Long
Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
    Dim lReturn As Long
    Dim pnt As POINT
    Dim rec As RECT
    lReturn = GetWindowRect(vlHwnd, rec)
    Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
    lReturn = GetCursorPos(pnt)
    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
y: " & pnt.y
    lReturn = SetCursorPos(rec.Left, rec.Top)
    Debug.Print "SetCursorPos: " & lReturn
    lReturn = GetCursorPos(pnt)
    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
y: " & pnt.y
    MoveMouseToTopLeftCorner = (0 <> lReturn)
End Function

In the click event of a button on a form I put:
    MoveMouseToTopLeftCorner Me.hwnd

I then opened the form and clicked on the button.  The cursor changed from a
hand to an arrow but it did not move.

The Debug statements produced:
GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
GetCursorPos - Return: 1, x: 824, y: 657
SetCursorPos: 1
GetCursorPos - Return: 1, x: 242, y: 379

This would seem to indicate that the cursor moved but it didn't.  If I
clicked on the button and then clicked on it again without moving the mouse
the debug statements produced:
GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
GetCursorPos - Return: 1, x: 804, y: 654
SetCursorPos: 1
GetCursorPos - Return: 1, x: 242, y: 379

GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
GetCursorPos - Return: 1, x: 804, y: 654
SetCursorPos: 1
GetCursorPos - Return: 1, x: 242, y: 379

As you can see from the above the second time the button was clicked the
system thought the cursor was back in the original position.

What did I miss?
0
Reply Stewart 5/20/2010 11:21:03 PM

What are you trying to achieve?  What is the purpose? Perhaps if you explain 
a little more, we could advise you.
-- 
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.



"Stewart Berman" wrote:

> Access 2007
> 
> I want to be able to move the cursor using VBA code.  I put the following in
> a module:
> 
>     Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,
> lpRect As RECT) As Long
>     Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As
> Long
>     Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
> ByVal y As Long) As Long
> Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
>     Dim lReturn As Long
>     Dim pnt As POINT
>     Dim rec As RECT
>     lReturn = GetWindowRect(vlHwnd, rec)
>     Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
> rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
>     lReturn = GetCursorPos(pnt)
>     Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
> y: " & pnt.y
>     lReturn = SetCursorPos(rec.Left, rec.Top)
>     Debug.Print "SetCursorPos: " & lReturn
>     lReturn = GetCursorPos(pnt)
>     Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
> y: " & pnt.y
>     MoveMouseToTopLeftCorner = (0 <> lReturn)
> End Function
> 
> In the click event of a button on a form I put:
>     MoveMouseToTopLeftCorner Me.hwnd
> 
> I then opened the form and clicked on the button.  The cursor changed from a
> hand to an arrow but it did not move.
> 
> The Debug statements produced:
> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
> GetCursorPos - Return: 1, x: 824, y: 657
> SetCursorPos: 1
> GetCursorPos - Return: 1, x: 242, y: 379
> 
> This would seem to indicate that the cursor moved but it didn't.  If I
> clicked on the button and then clicked on it again without moving the mouse
> the debug statements produced:
> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
> GetCursorPos - Return: 1, x: 804, y: 654
> SetCursorPos: 1
> GetCursorPos - Return: 1, x: 242, y: 379
> 
> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
> GetCursorPos - Return: 1, x: 804, y: 654
> SetCursorPos: 1
> GetCursorPos - Return: 1, x: 242, y: 379
> 
> As you can see from the above the second time the button was clicked the
> system thought the cursor was back in the original position.
> 
> What did I miss?
> .
> 
0
Reply Utf 5/21/2010 3:06:01 AM

This works fine for me and sets the cursor position to the top left of the 
form as intended.  You didn't post your RECT and POINT type declarations 
though so check these are correct.  Other than that there must be something 
else going on causing the effect you're getting.  Is there any other code in 
your command button?

Jon



"Stewart Berman" <saberman@nospam.nospam> wrote in message 
news:jdgbv5tka5f7cjmvmrii1387en5gi08vf9@4ax.com...
> Access 2007
>
> I want to be able to move the cursor using VBA code.  I put the following 
> in
> a module:
>
>    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As 
> Long,
> lpRect As RECT) As Long
>    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) 
> As
> Long
>    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
> ByVal y As Long) As Long
> Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
>    Dim lReturn As Long
>    Dim pnt As POINT
>    Dim rec As RECT
>    lReturn = GetWindowRect(vlHwnd, rec)
>    Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
> rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
>    lReturn = GetCursorPos(pnt)
>    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
> y: " & pnt.y
>    lReturn = SetCursorPos(rec.Left, rec.Top)
>    Debug.Print "SetCursorPos: " & lReturn
>    lReturn = GetCursorPos(pnt)
>    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
> y: " & pnt.y
>    MoveMouseToTopLeftCorner = (0 <> lReturn)
> End Function
>
> In the click event of a button on a form I put:
>    MoveMouseToTopLeftCorner Me.hwnd
>
> I then opened the form and clicked on the button.  The cursor changed from 
> a
> hand to an arrow but it did not move.
>
> The Debug statements produced:
> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
> GetCursorPos - Return: 1, x: 824, y: 657
> SetCursorPos: 1
> GetCursorPos - Return: 1, x: 242, y: 379
>
> This would seem to indicate that the cursor moved but it didn't.  If I
> clicked on the button and then clicked on it again without moving the 
> mouse
> the debug statements produced:
> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
> GetCursorPos - Return: 1, x: 804, y: 654
> SetCursorPos: 1
> GetCursorPos - Return: 1, x: 242, y: 379
>
> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
> GetCursorPos - Return: 1, x: 804, y: 654
> SetCursorPos: 1
> GetCursorPos - Return: 1, x: 242, y: 379
>
> As you can see from the above the second time the button was clicked the
> system thought the cursor was back in the original position.
>
> What did I miss? 


0
Reply Jon 5/21/2010 1:23:09 PM

The definitions are:
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type POINT
        x As Long
        y As Long
End Type

If it works for you then it is probably my environment that is causing the
problem:
Windows Ultimate X64 running in a VirtualBox virtual machine.

I will pursue it with Oracle.

"Jon Lewis" <jon.lewis@cutthespambtinternet.com> wrote:

>This works fine for me and sets the cursor position to the top left of the 
>form as intended.  You didn't post your RECT and POINT type declarations 
>though so check these are correct.  Other than that there must be something 
>else going on causing the effect you're getting.  Is there any other code in 
>your command button?
>
>Jon
>
>
>
>"Stewart Berman" <saberman@nospam.nospam> wrote in message 
>news:jdgbv5tka5f7cjmvmrii1387en5gi08vf9@4ax.com...
>> Access 2007
>>
>> I want to be able to move the cursor using VBA code.  I put the following 
>> in
>> a module:
>>
>>    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As 
>> Long,
>> lpRect As RECT) As Long
>>    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) 
>> As
>> Long
>>    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
>> ByVal y As Long) As Long
>> Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
>>    Dim lReturn As Long
>>    Dim pnt As POINT
>>    Dim rec As RECT
>>    lReturn = GetWindowRect(vlHwnd, rec)
>>    Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
>> rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
>>    lReturn = GetCursorPos(pnt)
>>    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
>> y: " & pnt.y
>>    lReturn = SetCursorPos(rec.Left, rec.Top)
>>    Debug.Print "SetCursorPos: " & lReturn
>>    lReturn = GetCursorPos(pnt)
>>    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
>> y: " & pnt.y
>>    MoveMouseToTopLeftCorner = (0 <> lReturn)
>> End Function
>>
>> In the click event of a button on a form I put:
>>    MoveMouseToTopLeftCorner Me.hwnd
>>
>> I then opened the form and clicked on the button.  The cursor changed from 
>> a
>> hand to an arrow but it did not move.
>>
>> The Debug statements produced:
>> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
>> GetCursorPos - Return: 1, x: 824, y: 657
>> SetCursorPos: 1
>> GetCursorPos - Return: 1, x: 242, y: 379
>>
>> This would seem to indicate that the cursor moved but it didn't.  If I
>> clicked on the button and then clicked on it again without moving the 
>> mouse
>> the debug statements produced:
>> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
>> GetCursorPos - Return: 1, x: 804, y: 654
>> SetCursorPos: 1
>> GetCursorPos - Return: 1, x: 242, y: 379
>>
>> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
>> GetCursorPos - Return: 1, x: 804, y: 654
>> SetCursorPos: 1
>> GetCursorPos - Return: 1, x: 242, y: 379
>>
>> As you can see from the above the second time the button was clicked the
>> system thought the cursor was back in the original position.
>>
>> What did I miss? 
>
0
Reply Stewart 5/21/2010 3:25:03 PM

It turned out to be the way the guest add-ins (integration features) work in
VirtualBox. It is a security issue since the mouse in the VM is the same as
the mouse on the host.  This is when running in the VirtualBox GUI on the
host.  It is not an issue when using remote desktop to connect the VM and
the code works in that environment. 

"Jon Lewis" <jon.lewis@cutthespambtinternet.com> wrote:

>This works fine for me and sets the cursor position to the top left of the 
>form as intended.  You didn't post your RECT and POINT type declarations 
>though so check these are correct.  Other than that there must be something 
>else going on causing the effect you're getting.  Is there any other code in 
>your command button?
>
>Jon
>
>
>
>"Stewart Berman" <saberman@nospam.nospam> wrote in message 
>news:jdgbv5tka5f7cjmvmrii1387en5gi08vf9@4ax.com...
>> Access 2007
>>
>> I want to be able to move the cursor using VBA code.  I put the following 
>> in
>> a module:
>>
>>    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As 
>> Long,
>> lpRect As RECT) As Long
>>    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) 
>> As
>> Long
>>    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long,
>> ByVal y As Long) As Long
>> Public Function MoveMouseToTopLeftCorner(ByVal vlHwnd As Long) As Boolean
>>    Dim lReturn As Long
>>    Dim pnt As POINT
>>    Dim rec As RECT
>>    lReturn = GetWindowRect(vlHwnd, rec)
>>    Debug.Print "GetWindowRect - Bottom: " & rec.Bottom & ", Left: " &
>> rec.Left & ", Right: " & rec.Right; ", Top: " & rec.Top
>>    lReturn = GetCursorPos(pnt)
>>    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
>> y: " & pnt.y
>>    lReturn = SetCursorPos(rec.Left, rec.Top)
>>    Debug.Print "SetCursorPos: " & lReturn
>>    lReturn = GetCursorPos(pnt)
>>    Debug.Print "GetCursorPos - Return: " & lReturn & ", x: " & pnt.x & ",
>> y: " & pnt.y
>>    MoveMouseToTopLeftCorner = (0 <> lReturn)
>> End Function
>>
>> In the click event of a button on a form I put:
>>    MoveMouseToTopLeftCorner Me.hwnd
>>
>> I then opened the form and clicked on the button.  The cursor changed from 
>> a
>> hand to an arrow but it did not move.
>>
>> The Debug statements produced:
>> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
>> GetCursorPos - Return: 1, x: 824, y: 657
>> SetCursorPos: 1
>> GetCursorPos - Return: 1, x: 242, y: 379
>>
>> This would seem to indicate that the cursor moved but it didn't.  If I
>> clicked on the button and then clicked on it again without moving the 
>> mouse
>> the debug statements produced:
>> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
>> GetCursorPos - Return: 1, x: 804, y: 654
>> SetCursorPos: 1
>> GetCursorPos - Return: 1, x: 242, y: 379
>>
>> GetWindowRect - Bottom: 866, Left: 242, Right: 1193, Top: 379
>> GetCursorPos - Return: 1, x: 804, y: 654
>> SetCursorPos: 1
>> GetCursorPos - Return: 1, x: 242, y: 379
>>
>> As you can see from the above the second time the button was clicked the
>> system thought the cursor was back in the original position.
>>
>> What did I miss? 
>
0
Reply Stewart 5/21/2010 9:24:51 PM

4 Replies
8018 Views

(page loaded in 0.098 seconds)

Similiar Articles:













8/1/2012 6:28:05 AM


Reply: