Get Cursor Location - API call

J

John T Ingato

Can someone please tell me how to get the current screen coord for the
pointer from windows. I am trying to identify the location of the mouse as
it passes over an open excel window. My gaol is to have a rectangulart shape
follow the mouse pointer.

I have tried looking on MSDN but am not finding the referemce to the API
Call.
 
K

Karl E. Peterson

John said:
Can someone please tell me how to get the current screen coord for the
pointer from windows.

Use the GetCursorPos API...

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

' Win32 API Structures
Private Type POINTAPI
x As Long
y As Long
End Type

Sub Whatever()
Static pt As POINTAPI
Call GetCursorPos(pt)
Debug.Print pt.x, pt.y
End Sub

See http://vb.mvps.org/samples/WndPick, or Cntl-F for it on
http://vb.mvps.org/samples/apixref.asp, to see examples of it in action.
I am trying to identify the location of the
mouse as it passes over an open excel window. My gaol is to have a
rectangulart shape follow the mouse pointer.

Sounds cool. How are you finding the coordinates for each cell?

Later... Karl
 
J

John T Ingato

The basis of what I am doing is a composite lookup for zip codes and such on
the fly. When the seach finds more then one match, a user form prompts the
user with the options in a list box. When an option is picked they are
prompted again for a cell reference with a form much like inputbox type: 8.
That is where I will loop; get the cursor position and having their choice
follow them to the cell of choice. It sounded like fun to try.

Thank you for the code Karl. Below is what I came up with using the API
call "getSystemMetrics": I like your code much better; actually passing a
type "POINTAPI" to Windows instead of making 2 calls. I haven't tried
either yet, so I may just try both and see which is more efficient. Any
Comments?


Declare Function GetSystemMetrics Lib "user32" (ByVal Index As Long) As Long

Function something()
Dim cp as POINTAPI
POINTAPI.x = GetSystemMetrics(SM_CXCURSOR)
POINTAPI.y = GetSystemMetrics(SM_CYCURSOR)
End Function
 
K

Karl E. Peterson

Hi John --
The basis of what I am doing is a composite lookup for zip codes and
such on the fly. When the seach finds more then one match, a user
form prompts the user with the options in a list box. When an option
is picked they are prompted again for a cell reference with a form
much like inputbox type: 8. That is where I will loop; get the cursor
position and having their choice follow them to the cell of choice.
It sounded like fun to try.

It'll be interesting, if you figure out how to make it work.
Thank you for the code Karl. Below is what I came up with using the
API call "getSystemMetrics": I like your code much better; actually
passing a type "POINTAPI" to Windows instead of making 2 calls. I
haven't tried either yet, so I may just try both and see which is
more efficient. Any Comments?

Declare Function GetSystemMetrics Lib "user32" (ByVal Index As Long)
As Long

Function something()
Dim cp as POINTAPI
POINTAPI.x = GetSystemMetrics(SM_CXCURSOR)
POINTAPI.y = GetSystemMetrics(SM_CYCURSOR)
End Function

Umm, yeah. Those calls are doc'd to return: "Width and height, in pixels, of a
cursor. The system cannot create cursors of other sizes." Not what you're looking
for.

Later... Karl
 

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