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
Solved

Using FindWindowEx to get a childwindow handle

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 - Compare and highlight cell not the same 3 53
Export Data to Different .csv Files 26 114
Send outlook email from VBS Script 2 42
which modules are active in VB6 project? 6 42
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…
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 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…
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…

829 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