ExcelVBAで、Windowsの全画面のhwndとClassNameを列挙(一部クラス名で絞込)
2つの例あり
'/例1 全画面列挙
'/例2 クラス名で抽出
sample.bas
Option Explicit
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd 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 GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
'/■--------------------
Sub MAIN()
'参照設定:Microsoft Scripting Runtime
Application.ScreenUpdating = False
Dim dic As Dictionary
Set dic = New Dictionary
Call dic.Add("_", "_")
Dim ret As Long
ret = EnumWindows(AddressOf EnumWindowsProc, ObjPtr(dic))
Dim i
For Each i In dic.Keys
'/---------------------------
'/例1 全画面列挙
'Debug.Print i & " -> " & dic.Item(i)
'/---------------------------
'/例2 クラス名で抽出(ここでは「Internet Explorer_Server」のみ)
If Trim(dic.Item(i)) = "Internet Explorer_Server" Then
Debug.Print i & " -> " & dic.Item(i)
End If
'/---------------------------
Next
Application.ScreenUpdating = True
End Sub
'/■--------------------
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Object) As Long
EnumWindowsProc = True
If IsWindowVisible(hwnd) = 0 Then
Exit Function
End If
Dim strClassName As String
Dim strCaption As String
strClassName = String(255, vbNullChar)
strCaption = String(255, vbNullChar)
GetWindowText hwnd, strCaption, Len(strCaption)
GetClassName hwnd, strClassName, Len(strClassName)
strCaption = RTrim(Left(strCaption, InStr(1, strCaption, vbNullChar) - 1))
strClassName = RTrim(Left(strClassName, InStr(1, strClassName, vbNullChar) - 1))
Dim dic As Dictionary
Set dic = lParam
If dic.Exists(Hex(hwnd)) Then
Else
dic.Add Hex(hwnd), strClassName '& " " & strCaption
End If
Call EnumChildWindows(hwnd, AddressOf EnumChildWindowsProc, ObjPtr(dic))
End Function
'/■--------------------
Public Function EnumChildWindowsProc(ByVal hwnd As Long, ByVal lParam As Object) As Long
EnumChildWindowsProc = EnumWindowsProc(hwnd, lParam)
End Function
###参考
以下、参考にさせていただきました
2009-06-18 ウィンドウの列挙@VBA
https://cartooh.hatenadiary.org/entries/2009/06/18