Solved

Using FindWindowEx to get a childwindow handle

Posted on 2000-03-22
3
327 Views
Last Modified: 2012-05-04
Using FindWindow to get a handle to the main window of an application is pretty straightforward.  I can just use the Caption of the window to search.

How do I get the name of a childwindow (like a combobox in a frame on the main window) if it doesn't have a caption?
0
Comment
Question by:rmbrown
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 100 total points
ID: 2646544
'~~~~Form Code~~~~
Private Sub Command1_Click()
    Dim lhWnd As Long
    'Target window
    lhWnd = FindWindow(vbNullString, "TestForm")
    'Loop thru the child windows
    Call EnumChildWindows(lhWnd, AddressOf EnumChildProc, 0)
    'If a combo is found ghWndCombo will be > 0
    Debug.Print Hex(ghWndCombo)
End Sub


'~~~~BAS Module Code~~~~
Option Explicit
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
    (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function EnumChildWindows Lib "user32" _
    (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Global ghWndCombo As Long

Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim sClassName As String
    Dim lRet As Long
    sClassName = String(255, Chr(0))
    lRet = GetClassName(hwnd, sClassName, 255)
    sClassName = Left(sClassName, lRet)
    If (sClassName Like "*ComboBox*") Then
        Debug.Print sClassName
        EnumChildProc = 0
        ghWndCombo = hwnd
        Exit Function
    End If
    ghWndCombo = 0
    EnumChildProc = 1
End Function
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2646845
If the ComboBox you are targeting is one of many, you may need to use a different approach.  This means you need to know the class name of the windows on the main window, and the order of these windows.

For example, to get the handle of the second combobox on a form with a Frame control and 2 ComboBoxes, use this code.  Note that the class names will be different for non-VB applications and perhaps for different versions of VB.

Private Sub Command1_Click()
    Dim lhWnd As Long
    'Find main app's handle
    lhWnd = FindWindow(vbNullString, "TestForm")
    'Get handle of frame knowing its class name
    lhWnd = FindWindowEx(lhWnd, 0, "ThunderFrame", vbNullString)
    'Get the first combobox knowing its class name
    lhWnd = FindWindowEx(lhWnd, 0, "ThunderComboBox", vbNullString)
    'Get the next window (a combo in this case)
    lhWnd = GetNextWindow(lhWnd, GW_HWNDNEXT)
End Sub
0
 

Author Comment

by:rmbrown
ID: 2650571
Thanks for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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 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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now