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 and its children
Description
Keywordswindow information, children, GetWindow, SendMessage, GetClassName
CategoriesAPI
 
The WindowInfo function calls the GetClassName API function to get the window's class name and it uses subroutine WindowText to get any associated text. It uses the GetWindow API function to list the window's child windows and then it recursively calls itself to list information about the child windows.

Subroutine WindowText uses SendMessage to send the WM_GETTEXTLENGTH message to the window to get the length of its associated text. It allocates space for the text and calls SendMessage to send the WM_GETTEXT message to the window to get the text.

 
Private Declare Function SendMessage Lib "user32" Alias _
    "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd _
    As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias _
    "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName _
    As String, ByVal nMaxCount As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE

' Return information about this window.
Public Function WindowInfo(window_hwnd As Long) As String
Dim txt As String
Dim buf As String
Dim buflen As Long
Dim child_hwnd As Long
Dim children As Collection
Dim i As Integer

    ' Get the class name.
    buflen = 256
    buf = Space$(buflen - 1)
    buflen = GetClassName(window_hwnd, buf, buflen)
    buf = Left$(buf, buflen)
    txt = "Class: " & buf & vbCrLf

    ' hWnd.
    txt = txt & "    hWnd: " & _
        Format$(window_hwnd) & vbCrLf
        
    ' Associated text.
    txt = txt & "    Text: [" & _
        WindowText(window_hwnd) & "]" & vbCrLf

    ' Make a list of the child windows.
    Set children = New Collection
    child_hwnd = GetWindow(window_hwnd, GW_CHILD)
    Do While child_hwnd <> 0
        children.Add child_hwnd
        child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
    Loop
    
    ' Get information on the child windows.
    For i = 1 To children.Count
        txt = txt & WindowInfo(children(i))
    Next i

    WindowInfo = txt
End Function

' Return the text associated with the window.
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