Tipp 0391 Fenster-Titel, -Handle und -Klassennamen
Autor/Einsender:
Datum:
  Angie
11.04.2004
Entwicklungsumgebung:   VB 6
Mit Hilfe der API-Funktionen EnumWindows und EnumChildWindows können alle laufenden Anwendungen (Top-Level-Fenster) und dessen abgeleiteten Fenster (Kindfenster) ermittelt werden. Um den Fenster-Titel/-Text, -Handle und Klassennamen ermitteln zu können, werden jedoch noch weitere API-Funktionen benötigt.
In diesem Beispiel werden der Fenster-Titel/-Text, -Handle und Klassennamen ermittelt. Bei den Stammfenstern (Top-Level-Fenstern) kann unterschieden werden, ob alle oder nur sichtbare Fenster und bei den abgeleiteten Fenstern (Kindfenster), ob nur sichtbare Fenster mit Titel und Rahmen, alle Fenster mit Titel/Text oder alle Fenster aufgelistet werden sollen.
In diesem Beispiel wird auch gezeigt, wie ein ausgewähltes Stammfenster in den Vordergrund gebracht werden kann.
Code im Modul modEnumWindows
 
Option Explicit

Public Declare Function EnumWindows Lib "user32" (ByVal _
      lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Declare Function EnumChildWindows Lib "user32" ( _
      ByVal hWndParent As Long, ByVal lpEnumFunc As Long, _
      ByVal lParam 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 GetWindowTextLength Lib "user32" _
      Alias "GetWindowTextLengthA" (ByVal hwnd 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 GetParent Lib "user32" (ByVal hwnd _
      As Long) As Long

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

Private Declare Function GetWindowLong Lib "user32" Alias _
      "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex _
      As Long) As Long

Private Const GWL_HWNDPARENT = (-8)
Private Const GWL_STYLE = (-16)

Private Const WS_VISIBLE = &H10000000
Private Const WS_BORDER = &H800000

Public Function EnumWinProc(ByVal hwnd As Long, ByVal lParam _
      As Long) As Long
  If (IsWindowVisible(hwnd) Or lParam) Then
    If GetParent(hwnd) = 0 Then
      If GetWindowLong(hwnd, GWL_HWNDPARENT) = 0 Then
        AddWndInfosToListView hwnd, lParam, _
              frmEnumWindows.lvwTopLevelWnd
      End If
    End If
  End If

  EnumWinProc = True
End Function

Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam _
      As Long) As Long
  Dim nStyle As Long

  Select Case lParam
    Case 0
      nStyle = GetWindowLong(hwnd, GWL_STYLE)
      nStyle = nStyle And (WS_VISIBLE Or WS_BORDER)
      If (nStyle = (WS_VISIBLE Or WS_BORDER)) Then
        AddWndInfosToListView hwnd, lParam, _
              frmEnumWindows.lvwChildWnd
      End If

    Case 1
      lParam = 0
      AddWndInfosToListView hwnd, lParam, _
            frmEnumWindows.lvwChildWnd

    Case 2
      AddWndInfosToListView hwnd, lParam, _
            frmEnumWindows.lvwChildWnd

    Case Else
  End Select

  EnumChildProc = True
End Function

Private Sub AddWndInfosToListView(ByVal hwnd As Long, ByVal _
      lParam As Long, ByVal lvw As ListView)

  Dim nRetVal       As Long
  Dim strClassName  As String
  Dim strWndTitle   As String

  Dim itmX          As ListItem

  strClassName = Space$(64)
  nRetVal = GetClassName(hwnd, strClassName, Len(strClassName))
  strClassName = Left$(strClassName, nRetVal)

  nRetVal = GetWindowTextLength(hwnd)
  If nRetVal > 0 Then
    If nRetVal > 256 Then nRetVal = 255
    strWndTitle = Space$(nRetVal + 1)
    GetWindowText hwnd, strWndTitle, Len(strWndTitle)
    strWndTitle = Left$(strWndTitle, nRetVal)
  End If

  If Len(strWndTitle) > 0 Or lParam <> 0 Then
    Set itmX = lvw.ListItems.Add(Text:=strWndTitle, _
          Key:=CStr(hwnd) & "h")
    itmX.SubItems(1) = CStr(hwnd)
    itmX.SubItems(2) = strClassName
  End If
End Sub
 
Weitere Links zum Thema
Fenster-Handle ohne Angabe des exakten Titels ermitteln
Fenster-Handle mit Maus-Cursor ermitteln

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (6,5 kB) Downloads bisher: [ 2422 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Dienstag, 5. Juli 2011