PC Review


Reply
Thread Tools Rate Thread

Moving The Cursor By Code

 
 
Stewart Berman
Guest
Posts: n/a
 
      21st May 2010
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?
 
Reply With Quote
 
 
 
 
Daniel Pineault
Guest
Posts: n/a
 
      21st May 2010
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?
> .
>

 
Reply With Quote
 
 
 
 
Jon Lewis
Guest
Posts: n/a
 
      21st May 2010
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?



 
Reply With Quote
 
Stewart Berman
Guest
Posts: n/a
 
      21st May 2010
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" <(E-Mail Removed)> 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" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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?

>

 
Reply With Quote
 
Stewart Berman
Guest
Posts: n/a
 
      21st May 2010
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" <(E-Mail Removed)> 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" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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?

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apartment Moving, Office Moving, Commercial Moving, Moving Companiesin your area. linkswanted Microsoft C# .NET 0 6th Jan 2008 05:46 AM
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted Microsoft ASP .NET 0 6th Jan 2008 05:45 AM
cursor problem. My cursor is moving ad hoc around my sreen. This . =?Utf-8?B?dGVyZXNhMjk=?= Microsoft Frontpage 8 8th Oct 2004 08:11 PM
Code for moving cursor one cell to the right Kate Microsoft Excel Programming 0 25th Aug 2004 08:54 PM
How do i reset my default cursor to a different cursor Sue feng yang Windows XP Customization 1 5th Jul 2003 03:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:50 AM.