Solved

Using FindWindowEx to get a childwindow handle

Posted on 2000-03-22
3
346 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
[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
  • 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

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

738 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