Mouse Move Event

R

Roger

I use the X & Y values in the mouse move event to make a comments box follow
the mouse pointer - how can I pass the X & Y values to a different sub - I
have declared them as public to no avail - they only work when they are used
in the actual mouse move routine

I could reduce the amount of code I am using by a significant amount if this
is possible - thanks in anticipation
 
T

Tim Zych

This worked for me.

' In a regular module
Public gX As Single, gY As Single

' In a userform
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
gX = X
gY = Y
Debug.Print CStr("Does " & CStr(X) & " = " & CStr(gX) & "?")
Debug.Print CStr("Does " & CStr(Y) & " = " & CStr(gY) & "?")
End Sub
 
G

Gary''s Student

Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long


Type POINTAPI
x As Long
y As Long
End Type


Sub ShowPos()
Dim lRetVal As Long
Dim Pos As POINTAPI
lRetVal = GetCursorPos(Pos)
MsgBox Pos.x & ", " & Pos.y
End Sub

This will retrieve and display the coordinates.
 
T

Tim Zych

That was an example. Here is how I might implement this (not using globals).

' In a regular module
Sub DisplayMouseCoordinates(ByVal X As Single, ByVal Y As Single)
Debug.Print "X = " & CStr(X)
Debug.Print "Y = " & CStr(Y)
End Sub

' In a userform:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Call DisplayMouseCoordinates(X, Y)
End Sub
 
R

Roger

I have implemented your solution with great effect - I have reduced the
amount of code required considerably - thankyou for your help
 

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