?
Solved

Using FindWindowEx to get a childwindow handle

Posted on 2000-03-22
3
Medium Priority
?
364 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

777 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