Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet information about a Window's child hierarchy from its hWnd
DescriptionThis example shows how to get information about a Window's child hierarchy from its hWnd in Visual Basic 6.
Keywordswindow, hWnd, FindWindow, class, window class
CategoriesAPI
 
This program gets information about the classes that make up a root window with a title containing some target text. For example, you can look for a form with a title containing the text "Notepad."

When you enter the text to find and click the Find Windows button, the program saves the target text in the global variable g_Contains and then calls the EnumWindows API function to enumerate the system's windows.

 
' Start the enumeration.
Private Sub cmdFindWindows_Click()
    g_Contains = txtContains.Text
    EnumWindows AddressOf EnumProc, 0
End Sub
 
Subroutine EnumProc examines the windows. It uses the GetWindowText API function to get the window's title and uses InStr to see if the title contains the target text. If it does, the routine uses function GetWindowStructure to get information about the window's children.
 
Public Function EnumProc(ByVal app_hwnd As Long, ByVal _
    lParam As Long) As Boolean
Dim buf As String * 1024
Dim title As String
Dim length As Long
Dim txt As String

    ' Get the window's title.
    length = GetWindowText(app_hwnd, buf, Len(buf))
    title = Left$(buf, length)

    ' See if the title contains the target text.
    If InStr(title, g_Contains) > 0 Then
        ' Display the window's structure.
        frmWindowList.txtResults.Text = _
            GetWindowStructure(app_hwnd)

        ' Stop searching.
        EnumProc = 0
    Else
        ' Continue searching til find it.
        EnumProc = 1
    End If
End Function
 
Function GetWindowStructure uses the GetClassName API function to get the window's class name. If the class is Edit, then it also calls function WindowText to get the text the window is displaying.

The function then makes a list of the window's children and recursively calls itself for each.

 
' Search this window and display information about its
' children.
Public Function GetWindowStructure(window_hwnd As Long, _
    Optional ByVal Indent As String = "") As String
Dim txt As String
Dim buf As String
Dim buflen As Long
Dim child_hwnd As Long
Dim children() As Long
Dim num_children As Integer
Dim i As Integer

    ' Get the class name.
    buflen = 256
    buf = Space$(buflen - 1)
    buflen = GetClassName(window_hwnd, buf, buflen)
    buf = Left$(buf, buflen)

    ' Display the window's class.
    txt = Indent & buf

    ' See if we found an Edit object.
    If buf = "Edit" Then txt = txt & " (" & _
        WindowText(window_hwnd) & ")"
    txt = txt & vbCrLf

    ' Search the children.
    ' Make a list of the child windows.
    num_children = 0
    child_hwnd = GetWindow(window_hwnd, GW_CHILD)
    Do While child_hwnd <> 0
        num_children = num_children + 1
        ReDim Preserve children(1 To num_children)
        children(num_children) = child_hwnd

        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
    Loop

    ' Get information on the child windows.
    For i = 1 To num_children
        txt = txt & GetWindowStructure(children(i), Indent _
            & "    ")
    Next i

    GetWindowStructure = txt
End Function
 
Function WindowText uses the SendMessage API function to get a window's text.
 
Public Function WindowText(window_hwnd As Long) As String
Dim txtlen As Long
Dim txt As String

    WindowText = ""
    If window_hwnd = 0 Then Exit Function
    
    txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, _
        0)
    If txtlen = 0 Then Exit Function
    
    txtlen = txtlen + 1
    txt = Space$(txtlen)
    txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, _
        ByVal txt)
    WindowText = Left$(txt, txtlen)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated