Solved

HELP?? :Obtaining an Object Name from a HWND

Posted on 1998-11-12
11
239 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
Comment Utility
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
Comment Utility
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
Comment Utility
Could you name one application that does what you are saying?
0
 
LVL 1

Author Comment

by:tommy_boy
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 13

Expert Comment

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

Author Comment

by:tommy_boy
Comment Utility
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
Comment Utility
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
Comment Utility
I think that you should face the fact that it is not possible.
0
 
LVL 7

Expert Comment

by:Inteqam
Comment Utility
emmmmmmmmmmm....

10 points for nothing.

:o)

0
 
LVL 7

Expert Comment

by:Inteqam
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now