Results 1 to 3 of 3

Thread: click on what?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    10
    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


    martin

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    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.
    Try putting the cursor over the various controls in your form and see the responses you'll get.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    10

    Wink

    Thanks. Works like a dream.

    martin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width