Mouse Events - seeking explanation

Nikolis

New Member
Joined
Sep 12, 2022
Messages
27
Office Version
  1. 2007
Platform
  1. Windows
I know how to use the MouseMove event, understand the logic behind the MouseDown/Up events. However, there seems to be a more advanced level, e.g. combining those 3 events to create a Drag&Drop opportunity for controls which don't initially have it (e.g. ListBox does). And I need to understand the structure of those events a bit deeper.

So, I'm trying to understand the 4 parameters in the title of those events sub ; Button, Shift, X, Y. There seems to be enough documentation for the first two but no explanation for X and Y, e.g. passing two numerical values as X and Y, what will they do exactly ? Are those values directly intended to be linked with the Left & Top properties of the controls or is this just one of the possibilities ?

I would appreciate your feedback. Thanks in advance.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I seem that the Microsoft's documentation, too often neglected, have the answers to these questions: MouseMove event

I tested it with a userform with two TextBoxes and one commandbutton, plus this code in its vba module:
VBA Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Me.TextBox1 = X & "- Button: " & Button
Me.TextBox2 = Y & "- Shift: " & Shift
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'X = 15
Me.TextBox1 = X & "- Button: " & Button
Me.TextBox2 = Y & "- Shift: " & Shift
End Sub
The X & Y represents the coordinates of the control pointed by the mouse; they can be written but I don't see any effect
 
Upvote 0
Dear Anthony, thank you !!

Just one little definition ; the "coordinates of the control" are its Left & Top properties ?
Meaning that the control's actual surface is a rectangle whose corners have coordinates (X, Y)-like ; (Left, Top), (Left+Width, Top), (Left, Top+Height), (Left+Width, Top+Height) ?

So, even if the control is (visually) a circular image, code-wise still it is imported as a square ... right ?
 
Upvote 0
X & Y are the coordinates within the object, beeing 0,0 the topleft corner of the object whom the MouseMove event refers to (the userform itself or the commandbutton, in my suggested test bed).

I am not able to draw a non rectangular object on a form, so I guess that x,y refers to the topleft corner of the rectangle that encompasses the object, and I guess that the event triggers only when you are over the real object (not its encompassing rectangle); but am not able to test it
 
Upvote 0
Solution
Fair enough, I will try to check it myself, later tonight. If I found out anything solid I will return the feedback as well.
Nevertheless, your guidance has helped me tremendously. Many many thanks for your support !
 
Upvote 0
Time ago, for jocking, I created a not rectangular userform; using that I can confirm that the event triggers only when you are on the object, not its encompassing rectangle.

The demo workbook can be downloaded here: Form_Forma_M-F.xlsm
The two buttons draw a userform, the MouseMove event macro is referred to the userform, and write in A20/B20 the X,Y coordinates when the event starts
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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
Back
Top