Pointer location: get then move to

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
3,752
The objective is to identify and store a pointer location, then return to it by a macro call. TsTom has provided the get and DK(Dan) has provided the move to that location (thanks plenty!). I can't seem to combine this help to achieve my objective. Please help. Thanks.
' TsTom to get pointer location
Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
Dim z As POINTAPI ' Declare variable
Sub mouscoords()
GetCursorPos z
'MsgBox "Your cursor is located at: X," & z.x & " Y," & z.y (I don't need this part)
End Sub
' DK(Dan) to move pointer to location
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Sub SetMousePosition(x As Long, y As Long)
'This next line makes sure that the specified coordinates are valid.
'If you try and call an API call with invalid arguments you could cause your
'system to crash. You will need to adjust this line to take account of different
'screen resolutions. You could be clever and use the GetSystemMetrics API call to
'automatically adjust for different resolution.
'If x < 0 Or x > 1024 Or y < 0 Or y > 768
Then Exit Sub (I don't think I need this part?)
SetCursorPos x, y
end sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

I just want to make sure I understand you.

You want to run a macro which gets the mouse coordinates and stores them. Then at some time later you want to run a macro which moves the mouse pointer back to the stored coordinates. Is that right? If so, the following code should work. To get the mouse coords run the procedure GetMousePosition. Then when you want to return the pointer to the stored coors run the SetMousePosition procedure.

Code:
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" _
                                      (lpPoint As POINTAPI) As Long
Type POINTAPI
    x As Long
    y As Long
End Type

Public z As POINTAPI

Sub GetMousePosition()
    GetCursorPos z
End Sub

Sub SetMousePosition()
If x< 0 Or x > 1024 Or y< 0 Or y > 768 Then Exit Sub
    SetCursorPos z.x, z.y
End Sub

_________________<font face="Impact">Hope this helps,
Dan</font>
This message was edited by dk on 2002-12-17 19:29
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top