Solved

HELP?? :Obtaining an Object Name from a HWND

Posted on 1998-11-12
11
243 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

820 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