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