Rubrik: Oberfläche · Fenster | VB-Versionen: VB6 | 03.07.09 |
Alle Child-Windows eines Elternfensters ermitteln Mit Hilfe der EnumChildWindows-API lassen sich schnell alle Child-Windows eines bestimmten Parent-Windows ermitteln. | ||
Autor: Dieter Otter | Bewertung: | Views: 14.163 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Heute stellen wir Ihnen die EnumChildWindows API-Funktion vor, mit der sich schnell alle Child-Windows eines bestimmten Parent-Windows ermitteln lassen.
Hinweis:
Es werden hier alle Fenster ermittelt, die eine hWnd-Eigenschaft besitzen, also auch alle Controls mit einem Fensterhandle!
Fügen Sie nachfolgenden Code in ein Modul ein:
Option Explicit ' benötigte API-Deklarationen Public Declare Function EnumChildWindows Lib "user32.dll" ( _ ByVal hWndParent As Long, _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long Private Declare Function GetWindowTextLength Lib "user32.dll" _ Alias "GetWindowTextLengthA" ( _ ByVal hwnd As Long) As Long Private Declare Function GetWindowText Lib "user32.dll" _ Alias "GetWindowTextA" ( _ ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal nMaxCount As Long) As Long Private Declare Function GetClassName Lib "user32.dll" _ Alias "GetClassNameA" ( _ ByVal hwnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As Long
' Diese Prozedur wird für jedes Child-Window aufgerufen Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim Retval As Long Dim WindowText As String Dim WindowClass As String On Error GoTo ErrHandler ' Fenstertext ermitteln WindowText = Space$(GetWindowTextLength(hwnd) + 1) Retval = GetWindowText(hwnd, WindowText, Len(WindowText)) WindowText = Left$(WindowText, Retval) ' Fensterklasse ermitteln WindowClass = Space(256) Retval = GetClassName(hwnd, WindowClass, Len(WindowClass)) WindowClass = Left$(WindowClass, Retval) ' Fenstertext und -klasse in der ListBox anzeigen With Form1.List1 .AddItem CStr(hwnd) & " # " & WindowClass & " # " & WindowText End With ' nächstes Child-Window ermitteln EnumProc = 1 Exit Function ErrHandler: WindowText = Space$(256) Resume Next End Function
Platzieren Sie nun auf der Form (Form1) ein ListBox-Control (List1) und fügen nachfolgenden Code in das Form_Load Event ein:
Private Sub Form_Load() ' alle Child-Windows ermitteln und in der ' ListBox anzeigen EnumChildWindows Me.hwnd, AddressOf EnumProc, 0 End Sub
Beim Starten des Projekts werden jetzt alle Child-Windows (in unserem Fall nur die ListBox) in der Liste angezeigt.