EnumWindows


Description:
This module demonstrates calling the EnumWindows and EnumChildWindows APIs by using these functions to fill a TreeView similar to Spy++'s main screen. To use it, create a form, named frmMain, and place a TreeView on that form and name the TreeView tvMain. Then call the function BeginEnum to populate the TreeView with all the windows on the system.
 
Code:
Option Explicit

Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As _
   Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
   
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, _
   ByVal lParam As Long) As Long
   
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
   ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) _
   As Long
   
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
   hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As _
   Any) As Long
   
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
   (ByVal hwnd As Long, ByVal nIndex As Long) As Long
   
Declare Function GetDesktopWindow Lib "user32" () As Long

Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal _
   hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) _
   As Long
   
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect _
   As RECT) As Long

Public Const WM_GETTEXT = &HD
Public Const GWL_HWNDPARENT = (-8)

Public Sub BeginEnum()

    frmMain.tvMain.Nodes.Clear
    ProcessEnum GetDesktopWindow, -1

    EnumWindows AddressOf EnumProc, 0

End Sub

Private Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) _
   As Long

    Dim hWndParent As Long
    hWndParent = GetWindowLong(hwnd, GWL_HWNDPARENT)
    
    If hWndParent = lParam Then
        ProcessEnum hwnd, lParam
    End If

    EnumProc = True

End Function

Private Sub ProcessEnum(hwnd As Long, hWndParent As Long)

    Dim sTreeCaption As String
    Dim sWindowClass As String
    Dim sWindowText As String
    Dim nCount As Long
    Dim rt As RECT
    
    sWindowClass = Space(80)
    nCount = GetClassName(hwnd, sWindowClass, 80)
    sWindowClass = Mid(sWindowClass, 1, nCount)
    
    sWindowText = Space(80)
    nCount = GetWindowText(hwnd, sWindowText, 80)
    sWindowText = Mid(sWindowText, 1, nCount)
    
    If sWindowText = "" Then
        sWindowText = Space(80)
        nCount = SendMessage(hwnd, WM_GETTEXT, 80, ByVal sWindowText)
        sWindowText = Mid(sWindowText, 1, nCount)
    End If
        
    GetWindowRect hwnd, rt
    
    sTreeCaption = Chr(34) & sWindowText & Chr(34) & " - " & _
       sWindowClass & ", (" & Str(rt.Left) & "," & Str(rt.Top) & "," & _
       Str(rt.Right) & "," & Str(rt.Bottom) & "), 0x" & _
       Right("00000000" & Hex(hwnd), 8)
    
    If hWndParent = -1 Then

        frmMain.tvMain.Nodes.Add , , "# 0", sTreeCaption
    
    Else

        frmMain.tvMain.Nodes.Add "#" & Str(hWndParent), tvwChild, _
           "#" & Str(hwnd), sTreeCaption
    
        EnumChildWindows hwnd, AddressOf EnumProc, hwnd
        
    End If

End Sub
 
Sample Usage:
 
n/a