Windows上で開かれているWindowを列挙する
'このソースを標準モジュールに貼り付けて、GetWindowStr関数をコールしてください
Option Explicit
'Windowテキストの長さを取得する
Declare Function GetWindowTextLength Lib
"user32" Alias "GetWindowTextLengthA"
(ByVal hwnd As Long) As Long
'Windowテキスト名を取得する
Declare Function GetWindowText Lib "user32"
Alias "GetWindowTextA" (ByVal hwnd
As Long, ByVal lpString As String, _
ByVal cch As Long) As Long
'Windowを列挙する
Declare Function EnumWindows Lib "user32"
(ByVal lpEnumFunc As Long, lParam As Any)
As Long
'Windowが表示されているか
Declare Function IsWindowVisible Lib "user32"
(ByVal hwnd As Long) As Long
Dim mstrWindows() As String '現在開いているWindow一覧取得用変数
'
'Window列挙
'varStrに現在表示されているWindowの名称(配列)
'
Public Function GetWindowStr(varStr As Variant)
As Boolean
Dim lngRet As Long 'APIの戻り値
On Error GoTo ErrorHandler
GetWindowStr = False
'配列初期化
ReDim mstrWindows(0)
'Windowを列挙する
lngRet = EnumWindows(AddressOf EnumWinProc,
0&)
varStr = mstrWindows
GetWindowStr = True
Exit Function
ErrorHandler:
End Function
'
'Windowプロシージャ
'
Public Function EnumWinProc(ByVal hWndX As
Long, lParam As Long) As Boolean
On Error GoTo ErrorHandler
EnumWinProc = False
'Windowが表示されていたら文字列を取得する
If IsWindowVisible(hWndX) Then
'配列初期化
ReDim Preserve mstrWindows(UBound(mstrWindows)
+ 1)
'文字列取得
mstrWindows(UBound(mstrWindows) - 1) = GetWinTextbyhwnd(hWndX)
End If
EnumWinProc = True
Exit Function
ErrorHandler:
End Function
'ハンドルよりWindowテキストを取得する
Public Function GetWinTextbyhwnd(hWndTarget
As Long) As String
Dim lngStrLen As Long
'Window名称取得用文字列
Dim strString As String
Dim lngRet As Long
'テキストの長さを取得する
lngStrLen = GetWindowTextLength(hWndTarget)
+ 1
'テキスト取得用変数を作成
strString = String(lngStrLen, 0)
'文字列取得
lngRet = GetWindowText(hWndTarget, strString,
lngStrLen)
'余分な文字列を削除する
GetWinTextbyhwnd = Left(strString, InStr(strString,
vbNullChar) - 1)
End Function