How to get x and y coordinates of cursor..

In a module, I have:

Type PointApi
  x As Long
  y As Long
End Type

Declare Function GetCursorPos Lib "user32" (IpPoint As PointApi) As Long
‘’’’’’’’’’’’’’
In a cmdButton_Click, I have

Private Button1_Click()
Dim p As PointApi
Dim xp As Long
Dim yp As Long
xp = GetCursorPos(p)
yp = GetCursorPos(p)
MsgBox xp & ", " & yp          ‘<---  no matter where I click, I get 1, 1

End Sub

Also, I would like to get the Button1.Top and Button1.Left as well.  Thank you in advance for your input.

Mike
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
ShauliConnect With a Mentor Commented:
I dont know what you want to acheive with the command button, however, using the MouseDown event you can get the precise location of the mouse using the integrated X and Y variables:

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'where X and Y are the mouse location
'do your stuff
End Sub


S
0
 
ImbueConnect With a Mentor Commented:
Try using this code in the command button.


Dim p As PointApi

Call GetCursorPos(p)
MsgBox p.x & ", " & p.y
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Shauli,
Thank you very much for good directions I have been receiving from you.  

Imbue,  
Thank you for your solution.

Regards,

Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.