Mouse position

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm trying to identify the coordinates of the mouse pointer while it is
moving or hovering over a map on an Access form. Here is an example of
what I'm trying to acheive (this is not the map - just an example).

How can this be done?
 
Andrew said:
Hi,

I'm trying to identify the coordinates of the mouse pointer while it
is moving or hovering over a map on an Access form. Here is an
example of what I'm trying to acheive (this is not the map - just an
example).

How can this be done?

Most controls have a MouseMove event, which fires whenever the mouse
pointer moves over the control. The event procedure for the MouseMove
event receives a number of useful arguments, including the X and Y
coordinates of the pointer.
 
Hi Dirk,

Thanks for your reply - I've seen your replies before and noticed you're
very helpful.

Before logging my question I'd seen a few postings about the 'mouse move
event' but I couldn't see how to get the x & y coordinates. Can you please
explain it for me a little?? All I could see was how to kick off a procedure
(on mouse move)

I kept on searching and after about 6 hours of trying everything I FINALLY
got it by modifying the following post by Dave Peterson. That was when I
received your reply!!!!!

A little search of google came up with a couple of API's that worked ok for
me:

In a general module:

Option Explicit
Public Type POINTAPI
x As Long
y As Long
End Type

Declare Function SetCursorPos Lib "User32" _
(ByVal x As Long, ByVal y As Long) As Long
Declare Function GetCursorPos Lib "User32" _
(lpPoint As POINTAPI) As Long


Behind the form:

Option Explicit
Private Sub CommandButton1_Click()
Dim myPointAPI As POINTAPI
GetCursorPos myPointAPI
Call SetCursorPos(myPointAPI.x - 300, myPointAPI.y)
End Sub

(I used 300 with my button, but you could experiment and use whatever you
needed.)
 
Andrew said:
Hi Dirk,

Thanks for your reply - I've seen your replies before and noticed
you're very helpful.

Before logging my question I'd seen a few postings about the 'mouse
move event' but I couldn't see how to get the x & y coordinates. Can
you please explain it for me a little?? All I could see was how to
kick off a procedure (on mouse move)

If you create an event procedure for the mousemove event of a control or
section, the declaration of that procedure will look something like
this:

Private Sub ControlName_MouseMove(Button As Integer, Shift As Integer, X
As Single, Y As Single)

(Note: that was entered all on one line, but it will have been broken
onto two or more lines by the newsreader.)

The arguments are as follows (quoted from the Access 97 help file, since
the later versions' help files either don't contain this detail or
aren't indexed so that I can find it):

Button -
The state of the mouse buttons when the event occurs.
If you need to test for the Button argument, you can use
one of the following intrinsic constants as bit masks:

acLeftButton -
The bit mask for the left mouse button.
acRightButton -
The bit mask for the right mouse button.
acMiddleButton -
The bit mask for the middle mouse button.

Shift -
The state of the SHIFT, CTRL, and ALT keys
when the button specified by the Button argument
was pressed or released. If you need to test for the
Shift argument, you can use one of the following
intrinsic constants as bit masks:

acShiftMask -
The bit mask for the SHIFT key.
acCtrlMask -
The bit mask for the CTRL key.
acAltMask -
The bit mask for the ALT key.

X, Y -
The x and y coordinates of the current location
of the mouse pointer. The X and Y arguments are
always expressed in twips.

Thus, if you create a MouseMove event procedure for the control in
question, you can check the values of the X and Y arguments that are
passed to that procedue to find out where the mouse pointer is.

Here's a very simple example:

'----- start of code -----
Private Sub Image1_MouseMove( _
Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single)

SysCmd acSysCmdSetStatus, "The mouse is at (" & X & ", " & Y & ")."

End Sub
'----- end of code -----

As mentioned abovem the X and Y coordinates are expressed in twips (1
twip = 1/1440 inch). I believe they are given relative to the top left
corner of the control.
 

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

Back
Top