Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HELP?? :Obtaining an Object Name from a HWND

Posted on 1998-11-12
11
Medium Priority
?
246 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:tommy_boy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1444691
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

0
 
LVL 1

Author Comment

by:tommy_boy
ID: 1444692
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...

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1444693
Could you name one application that does what you are saying?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:tommy_boy
ID: 1444694
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)?


0
 
LVL 1

Expert Comment

by:raygibbins
ID: 1444695
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

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1444696
Raygibbins: Tommy Boy was talking about in another application. So the forms collection will never work.
0
 
LVL 1

Author Comment

by:tommy_boy
ID: 1444697
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.
0
 

Expert Comment

by:KRV
ID: 1444698
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
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 200 total points
ID: 1444699
I think that you should face the fact that it is not possible.
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 2615095
emmmmmmmmmmm....

10 points for nothing.

:o)

0
 
LVL 7

Expert Comment

by:Inteqam
ID: 2615698
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:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10097974 
<http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10097974 >
*      or -
http://www.experts-exchange.com/Q.10097974-2615095
<http://www.experts-exchange.com/Q.10097974-2615095>  (non-cookie
login)

for the comment above
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question