Solved

Using FindWindowEx to get a childwindow handle

Posted on 2000-03-22
3
333 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

895 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

16 Experts available now in Live!

Get 1:1 Help Now