ウインドウ一覧のクラス名とキャプション名を取得する()
WindowsAPIを用いてウインドウの一覧表を作成するVBAコード | VBA・GAS・Pythonで仕事を楽しく効率化↑
vba FindWindow - 検索
- 'ウインドウハンドルを取得する
- 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 ウインドウ一覧のクラス名とキャプション名を取得する()
-
- 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 = strClassName
- Cells(i, 2).Value = strCaption
-
- '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
VBAでWindowsAPIを用いてウインドウを取得する手順について | VBA・GAS・Pythonで仕事を楽しく効率化 ↑