LoginSignup
0
0

More than 1 year has passed since last update.

Win32API:ウィンドウのクラス名とキャプション取得

Posted at
'ウインドウハンドルを取得する
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'ウインドウが可視かどうかを取得する
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 GetNextWindow Lib "user32" Alias "GetWindow" _
(ByVal hWnd As Long, ByVal wFlag As Long) As Long

Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2

Sub getCapClass()
    Dim i As Long
    i = 1

    Dim strClassName As String * 100
    Dim strCaption As String * 80

    Dim hWnd As Long
    hWnd = FindWindow(vbNullString, vbNullString) '引数を両方ともvbNullStringにして1つめのウインドウを取得する

    Do
    If IsWindowVisible(hWnd) Then
        GetWindowText hWnd, strCaption, Len(strCaption)
        GetClassName hWnd, strClassName, Len(strClassName)

        Cells(i, 1).Value = Left(strClassName, InStr(strClassName, vbNullChar) - 1)
        Cells(i, 2).Value = Left(strCaption, InStr(strCaption, vbNullChar) - 1)

        i = Application.WorksheetFunction.Max(Cells(60000, 1).End(xlUp).Row, Cells(60000, 2).End(xlUp).Row, Cells(60000, 3).End(xlUp).Row) + 1
    End If

    hWnd = GetNextWindow(hWnd, GW_HWNDNEXT)
    Loop Until hWnd = GetNextWindow(hWnd, GW_HWNDLAST)

End Sub

0
0
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
0
0