Mouse events in VBA

Darril

New Member
Joined
Nov 15, 2005
Messages
10
Hi all
I wish to write my own digitizing program where I paste a scanned graph into an Excel worksheet and then use mouse events like Mousmove and Mousedown get the coordinates of any point on the scanned image.

Please can somebody supply a simple example in VBA showing me how obtain the coordinates of the mouse cursor.

I refer to the following MSDN web page http://msdn.microsoft.com/library/d...vtMouseMove.asp

Many Thanks
Darril
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End Type



Sub myCurMousePos()
'Sample of how to use the mouse pointer function!
Dim lngCurPos As POINTAPI

GetCursorPos lngCurPos

MsgBox "X = " & lngCurPos.x & ", " & "Y = " & lngCurPos.y

End Sub
 

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,270
Darril said:
Please can somebody supply a simple example in VBA showing me how obtain the coordinates of the mouse cursor.
Along the lines of JW's post, if you are interested in seeing the coordinates in a cell on a constant basis such as in cell A1, run the macro named PositionXY and move the mouse around.

To exit the procedure, double click any cell and then either hit Esc, Enter, or select any cell.

Goes into a standard module:


Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End Type

Sub PositionXY()
Dim lngCurPos As POINTAPI
Do
GetCursorPos lngCurPos
Range("A1").Value = "X: " & lngCurPos.x & " Y: " & lngCurPos.y
DoEvents
Loop
End Sub
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539

ADVERTISEMENT

If you use Tom's code: Note, you will need to hit: Ctrl-Break to exit the code!

Or you can add back-door code: this sample will stop the Sub from running and return control to your sheet if you enter anything into Cell: A3.

It also shows you how to limit the point range of operation, if needed.

Sub GetTheMouseCursorPosition()
'Get Mouse position as x,y coordinates.
'Note: Once run you must hit "Ctrl-Break" to stop,
'with this test code or add a value to A3!

Dim lngCurPos As POINTAPI

Do
GetCursorPos lngCurPos
'Display the "X" position in cell "A1."
'Display the "Y" position in cell "A2."
Cells(1, 1).Value = "X = " & lngCurPos.x
Cells(2, 1).Value = "Y = " & lngCurPos.y

'Govern the reporting range?
If lngCurPos.x = 337 And lngCurPos.y = 419 Then GoTo myWin Else

'Back-Door sheet stop to end code!
If [A3] <> "" Then GoTo myStop Else
DoEvents
Loop

myWin:
Range("A3").Select
End

myStop:
End Sub
 

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,270
Joe Was said:
If you use Tom's code: Note, you will need to hit: Ctrl-Break to exit the code!
No you don't, which was why I wrote in my post:
"To exit the procedure, double click any cell and then either hit Esc, Enter, or select any cell."
as a quick and convenient way to exit the procedure, given that their hand is already on the mouse moving over cells, though your suggestion for termination is also fine and just as viable.
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209

ADVERTISEMENT

I think you may be better served by loading the image up into an ActiveX control such as the image control. Just place it in a worksheet.
Use Tom or Joes code amended in the mouse move event of the control.
To dynamically resize the image takes a bit more API code.
 

Darril

New Member
Joined
Nov 15, 2005
Messages
10
Mouse coordinates - assistance greatly appreciated

Thanks to all of you - just brilliant
There are parts of the code which I have never seen before and I would never have been able to achieve without your expert help.

I need to learn more about the code that has been put in the public function declaration ie POINTAPI and commands like "user32" This is certainly an advanced level of programming.

For me, the next part of the process would be to learn how to capture the coordinates when I press a mouse button - I will do some research and have another look at the MSDN site again using the tips you have supplied.

Your assistance has been much appreciated
Darril Aronowitz
South Africa
 

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
Thanks Tom,
I did not know you could also break running code using other key combinations besides "Ctrl-Break" like: "To exit the procedure, double click any cell and then either hit Esc, Enter, or select any cell."

I always used Ctrl-Break [Mainframe days] and never thought to explore other combinations. Should have know, there is always more than one way to do something!

Darril,

You have two Events within the Sheet Module that you can capture Mouse Clicks with:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

End Sub


Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

End Sub

Note: You do not have a Left Click Button Event, to get that functionallity you use:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

Which works for all physical selection methods [depending on the code used a selection made by other code may not trigger this event, hence the "physical." More often than not though the problem is more "code re-triggering the selection Event, throwing it into a loop!], not just the mouse selection [key board and cursor].

You can also trap on a UserForm [Using an API that Ivan mentioned]:


'Standard Module declarations, like: Module1.
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_MBUTTONDOWN = &H207
Public Const WM_MBUTTONUP = &H208
Public Const WM_MBUTTONDBLCLK = &H209



Private Sub UserForm_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'UserForm module code!
Dim MyX As Long

[B1] = ActiveWindow.PointsToScreenPixelsX(X)
MyX = [B1]
[B1] = ActiveWindow.PointsToScreenPixelsX(X)
MyX = [B1]
Select Case MyX
Case WM_MOUSEMOVE
UserForm1.Caption = "MouseMove!"
Case WM_LBUTTONDOWN
UserForm1.Caption = "Left MouseDown"
Case WM_LBUTTONUP
UserForm1.Caption = "Left MouseUp"
Case WM_LBUTTONDBLCLK
UserForm1.Caption = "Left DoubleClick"
Case WM_RBUTTONDOWN
UserForm1.Caption = "Right MouseDown"
Case WM_RBUTTONUP
UserForm1.Caption = "Right MouseUp"
Case WM_RBUTTONDBLCLK
UserForm1.Caption = "Right DoubleClick"
End Select
End Sub
 

jryan15

Board Regular
Joined
Jan 27, 2005
Messages
158
i've been trying to use the code above (Public Const WM_LBUTTONDOWN = &H201 ) to find a way to trap mouse buttons clicks using the worksheet selection change event.

i see that WM_LBUTTONDOWN is a constant representing mouse left button down, but i don't how to condition code off of it.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,003
Messages
5,834,825
Members
430,324
Latest member
bosphoruskid

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
Top