Link to home
Start Free TrialLog in
Avatar of tommy_boy
tommy_boy

asked on

HELP?? :Obtaining an Object Name from a HWND

I desperately need to know how to obtain an object name from a hwnd.  Ie. I have the hwnd of the active form (of another application) and now I need to loop through all controls on that form and look for a property of a particular type of control.

Some of the code is listed below:
Option Explicit
Declare Function GetForegroundWindow& Lib "user32" ()

private sub find_control()
dim ret as long
ret = getforegroundwindow()
'..now I have the hwnd of that form
'..I would like to do the following
'..dim myobj as object
'..for each myobj in formname.hwnd
'..if typeof(myobj) is textbox then
'..it is a text box
'...etc
'..endif
'..loop
end sub

Does anyone out there know of the API of VB proc that I use????  Please help!?

Tom
Avatar of Mirkwood
Mirkwood

I can be very short. It is impossible to get back from a hwnd to a control handle.
What you can do is sendmessage to the control. So for example, when it is an editbox you can send some messages to it.

The API FindWindowEx can help you. The first hwnd must be the parent window. The second hwnd is the previous found window in your name. The string parameters are the classnames of the windows. In your case "Edit". This allows you to find the window handles. Now you can send the window message.

Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Avatar of tommy_boy

ASKER

Mirkwood...

Obtaining the windows handle of any control loaded on any form in any application is not the problem, as I know these API's.  Also sending a message to the form or control is not what I require.  Obtaining the name is.  I know it must be possible as there are applications out there that do this.

Thanks anyway...

Could you name one application that does what you are saying?
Ok.. to explain the overview of what I am trying to achieve.  I need to be able to simulate MouseExit (Java, Access, VB tooltips etc).  For this example, let's say I am making a custom tooltip.  I can detect when the mouse is moved off a control, but if the user moves the mouse rapidly or the controls are very close together, more than one 'ToolTip' is displayed at the same time.  With Access or Java, if the user moves from one control to the next, only one event is fired, even if the controls are very close together.

Therefore, the only way I can be sure that only one 'ToolTip' is displayed for this type of control, is to loop through those controls and check for a 'displayed' property.  

Do you have another possible solution?  Do you know of a MouseExit API (not Desaware - I know they have a custom control that does this)?


The following function will loop thru' all the forms and controls in a project and find and return the object name

Public Function GetControlName(ByVal hWnd As Long)
    Dim intFormNum As Integer
    Dim intControlNum As Integer
    For intFormNum = 0 To Forms.Count - 1
        If Forms(intFormNum).hWnd = hWnd Then
            GetControlName = Forms(intFormNum).Name
            Exit Function
        End If
        For intControlNum = 0 To Forms(intFormNum).Controls.Count - 1
            On Error Resume Next
            If Forms(intFormNum).Controls(intControlNum).hWnd = hWnd Then
                If Err.Number = 0 Then
                    GetControlName = Forms(intFormNum).Controls(intControlNum).Name
                End If
                Exit Function
            End If
        Next intControlNum
    Next intFormNum
GetControlName = "No Control"
End Function

stick Text1 on a form and run

Private Sub Form_Load()
    MsgBox GetControlName(Text1.hWnd)
End Sub

Raygibbins: Tommy Boy was talking about in another application. So the forms collection will never work.
Yes as Mirkwood said, obtaining the name of a control inside THAT project is no problem.  Obtaining the name of a control outside that project, is.
As far as I am aware the 3rd party software uses code similar to the first answer but before displaying the tooltip there is a check and balance by using the mouse API functions to establish that the mouse is actually over the same hdc as the sendmsg struct is calling
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
emmmmmmmmmmm....

10 points for nothing.

:o)

Strange ,

I Recieved the following

Inteqam has added a new comment to the question.
If you'd like to read Inteqam's comments, enter Experts Exchange at
the following URL:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10097974 
<https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10097974 >
*      or -
https://www.experts-exchange.com/Q.10097974-2615095
<https://www.experts-exchange.com/Q.10097974-2615095>  (non-cookie
login)

for the comment above