Option Explicit
Public Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
Public Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function IsWindowEnabled Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Public Const MAX_PATH = 260
Public Function EnumWindowProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sTitle As String
Dim sClass As String
'eliminate windows that are not top-level.
If GetParent(hwnd) = 0& And _
IsWindowVisible(hwnd) And _
IsWindowEnabled(hwnd) Then
GetWindowInfo hwnd, sTitle, sClass
'add the items to the list
Form1.List1.AddItem sTitle & " (" & sClass & ")"
Form1.List1.ItemData(Form1.List1.NewIndex) = hwnd
End If
'To continue enumeration, return True
'To stop enumeration return False (0).
'When 1 is returned, enumeration continues
'until there are no more windows left.
EnumWindowProc = 1
End Function
Public Sub GetWindowInfo(hwnd As Long, Optional sTitle As String, Optional sClass As String)
'set up the strings to receive the class and window text
sTitle = Space$(MAX_PATH)
sClass = Space$(MAX_PATH)
Call GetClassName(hwnd, sClass, MAX_PATH)
Call GetWindowText(hwnd, sTitle, MAX_PATH)
'strip the trailing chr$(0)'s from the strings returned above
sClass = TrimNull(sClass)
sTitle = TrimNull(sTitle)
End Sub
Public Function TrimNull(s As String)
'remove string before the terminating null(s)
Dim pos As Long
pos = InStr(s, Chr$(0))
If pos Then
TrimNull = Left$(s, pos - 1)
Else
TrimNull = s
End If
End Function
Public Sub InitEnumWindows()
EnumWindows AddressOf EnumWindowProc, &H0
End Sub