2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ExcelVBAで、Windowsの全画面のhwndとClassNameを列挙(一部クラス名で絞込)

Posted at

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

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?