Visual Basic Tips


ウィンドウ(全て)の列挙(API)


コールバック関数を使用してウィンドウハンドルを取得し、そのハンドルを使用して、キャプション、Top,Left.Bottom,Rightを出力しています。


Private Sub Form_Load()
  
Call EnumWindows(AddressOf EnumWndProc, 0)
End Sub

'----------------------------------------------------------
' Module
'----------------------------------------------------------
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Public Declare Function GetWindowPlacement Lib "user32" (ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long

'GetWindowで使用する定数
Public Const GW_OWNER = 4

'GetWindowPlacementで使用する構造体
Public Type POINTAPI
  x
As Long
  y
As Long
End Type

Public Type RECT
  Left
As Long
  Top
As Long
  Right
As Long
  Bottom
As Long
End Type

Public Type WINDOWPLACEMENT
  Length
As Long
  flags
As Long
  showCmd
As Long
  ptMinPosition
As POINTAPI
  ptMaxPosition
As POINTAPI
  rcNormalPosition
As RECT
End Type

'コールバック関数
Public Function EnumWndProc(ByVal hWnd As Long, lParam As Long) As Boolean
  
Dim strWindowName As String * 128
  
Dim strClassName As String * 128
  
Dim WPL As WINDOWPLACEMENT
  
Dim lngRet As Long

  strWindowName = ""
  strClassName = ""
  lngRet = GetWindowText(hWnd, strWindowName, Len(strWindowName))
  
Call GetClassName(hWnd, strClassName, Len(strClassName))

  
'EnumWindowsでは不可視のウィンドウ(IME等)も列挙されてしまうため、抽出します。
  
If IsWindowVisible(hWnd) Then       '可視状態か
    
If GetWindow(hWnd, GW_OWNER) = 0 Then 'オーナーフォームか
      
If lngRet <> 0 Then         'キャプションを持っているか
        
If Left(strClassName, 7) <> "Progman" Then 'シェルでないか
          WPL.Length = Len(WPL)
          
Call GetWindowPlacement(hWnd, WPL)
          
Debug.Print "----------------------------------"
          
Debug.Print "hWnd = " & hWnd
          
Debug.Print "Window Name = " & strWindowName
          
Debug.Print "Class Name = " & strClassName
          
Debug.Print "Left = " & WPL.rcNormalPosition.Left
          
Debug.Print "Top = " & WPL.rcNormalPosition.Top
          
Debug.Print "Right = " & WPL.rcNormalPosition.Right
          
Debug.Print "Bottm = " & WPL.rcNormalPosition.Bottom
        
End If
      
End If
    
End If
  
End If
  EnumWndProc =
True
End Function


DownLoad vbtips078.lzh 2KB (VB6.0)