Moving The Cursor By Code

S

Stewart Berman

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?
 
J

Jon Lewis

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
 
S

Stewart Berman

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

Stewart Berman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top