-
Hi.
Is there anyone out there who can help me with this litle problem I have?
I am making an app, and I need to know what control the mouse pointer is over when the button is clicked... (or rather the name of it) is this possible?
The app creates (dynamic) a whole heap of images, and the images name is the name of the file.. ex:
if an imagebox loads "firstone.jpg", the imagebox will be named "firstone". It's essential for the app that the imagebox get that name.
Thanks
-
Create a CommandButton called Command1, and many other controls as you wish! :)
Then use this code:
Code:
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Function GetTheNameOfTheControlWhereTheMousePointerIsRightNow() As String
Dim ptCursor As POINTAPI, hWndControl As Long, Control As Control
Call GetCursorPos(ptCursor)
hWndControl = WindowFromPoint(ptCursor.X, ptCursor.Y)
For Each Control In Controls
If Control.hWnd = hWndControl Then GetTheNameOfTheControlWhereTheMousePointerIsRightNow = Control.Name
Next
End Function
Private Sub Command1_Click()
Dim sName As String
sName = GetTheNameOfTheControlWhereTheMousePointerIsRightNow
If sName = vbNullString Then
Call MsgBox("The pointer is not over a control in your form!", vbExclamation)
Else
Call MsgBox("The pointer is over a control called: " & sName, vbInformation)
End If
End Sub
Errrr... You just MIGHT want to change one of the function names. ;)
Anyway, start the program, and "click" the button with your keyboard. :rolleyes:
Try putting the cursor over the various controls in your form and see the responses you'll get.
-
Thanks. Works like a dream.