HOME > 即効テクニック > AccessVBA > コントロール > 可視ウィンドウのクラス名、タイトルを列挙する

即効テクニック

コントロール

可視ウィンドウのクラス名、タイトルを列挙する

(Access 2000/2002/2003)
●概要●
宣言文 Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long)
    As Long

設定項目 内容
hwnd   ウィンドウハンドルを指定[省略不可]

引数で指定したウィンドウハンドルを持つウィンドウの可視状態を取得します。
可視状態の場合には「0」以外の値を、不可視状態の場合および失敗時には「0」を返します。
次のコードは、可視ウィンドウのクラス名とタイトルを、タブ区切りのテキストファイルとして出力するサンプルです。

●サンプル●
Private Declare Function GetWindow _
    Lib "user32" _
    (ByVal hwnd As Long _
    , ByVal wCmd As Long) _
    As Long
Private Declare Function GetClassName _
    Lib "user32" Alias "GetClassNameA" _
    (ByVal hwnd As Long _
    , ByVal lpClassName As String _
    , ByVal nMaxCount As Long) _
    As Long
Private Declare Function GetWindowText _
    Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long _
    , ByVal lpString As String _
    , ByVal cch As Long) As Long
Private Declare Function IsWindowVisible _
    Lib "user32" _
    (ByVal hwnd As Long) _
    As Long
Private Const GW_HWNDFIRST As Long = 0
Private Const GW_HWNDNEXT As Long = 2
Private Const ERROR_SUCCESS As Long = 0

'可視ウィンドウのクラス名、タイトルを列挙する
Sub Sample()
    Dim myHwnd As Long
    Dim myFixClassName As String * 255
    Dim myFixCaption As String * 255
    Dim myClassName As String
    Dim myCaption As String
    Dim myFNo As Integer, myFName As String
    Dim myBuf() As String, i As Long
    ReDim myBuf(0) As String
    'タイトル行
    myBuf(0) = "クラス名" & vbTab & "タイトル"
    '最前面のウィンドウのウィンドウハンドルを取得
    myHwnd = GetWindow(Application.hWndAccessApp, GW_HWNDFIRST)
    Do Until myHwnd = ERROR_SUCCESS
        '可視であれば処理を行う
        If CBool(IsWindowVisible(myHwnd)) Then
            'クラス名の取得
            GetClassName myHwnd, myFixClassName, Len(myFixClassName)
            myClassName = Left(myFixClassName _
                , InStr(myFixClassName, vbNullChar) - 1)
            'タイトルの取得
            GetWindowText myHwnd, myFixCaption, Len(myFixCaption)
            myCaption = Left(myFixCaption _
                , InStr(myFixCaption, vbNullChar) - 1)
            '配列に格納
            i = i + 1
            ReDim Preserve myBuf(i) As String
            myBuf(i) = myClassName & vbTab & myCaption
        End If
        '次のウィンドウのウィンドウハンドルを取得
        myHwnd = GetWindow(myHwnd, GW_HWNDNEXT)
    Loop
    'テキストファイルに出力
    myFNo = FreeFile
    myFName = CurrentProject.Path & "\WindowInfo.txt"
    Open myFName For Output As #myFNo
    Print #myFNo, Join(myBuf, vbCrLf)
    Close #myFNo
End Sub

●補足●
データベースファイルがあるのと同じフォルダに「WindowInfo.txt」が自動作成され、可視ウィンドウのクラス名とタイトルが出力されます。結果はこのテキストファイルを開いて見てみてください。