Rubrik: Fenster | 15.03.02 |
GetWindowThreadProcessId-Funktion Diese Funktion ermittelt den Thread-ID und Prozess-ID eines Fensters. | ||
Betriebssystem: Win95, Win98, WinNT 3.1, Win2000, WinME | Views: 21.549 |
Deklaration:
Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _ ByVal hwnd As Long, _ lpdwProcessId As Long) As Long
Beschreibung:
Diese Funktion ermittelt den Thread-ID und Prozess-ID eines Fensters.
Parameter:
hwnd | Erwartet das Fensterhandle, dessen Thread- und Prozess-ID ermittelt werden sollen. |
lpdwProcessId | Erwartet einen Long-Puffer, der mit dem ermitteltemProzess-ID gefüllt wird. |
Rückgabewert:
Ist die Funktion erfolgreich, ist der ermittelte Thread-ID die Rückgabe, andernfalls wird "0" zurückgegeben.
Beispiel:
' schreiben Sie den nachfolgenden Code in ein öffentliches Modul Private Declare Function EnumWindows Lib "user32.dll" ( _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long Private Declare Function EnumThreadWindows Lib "user32.dll" ( _ ByVal dwThreadId As Long, _ ByVal lpfn As Long, _ ByVal lParam As Long) As Long Private Declare Function EnumChildWindows Lib "user32.dll" ( _ ByVal hWndParent As Long, _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _ ByVal hwnd As Long, _ lpdwProcessId 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 Public Enum WindowEnum HAUPTFENSTER = 0 THREADFENSTER = 1 KINDFENSTER = 2 End Enum
' Ereignisse der Enum-Funktionen landen hier Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim Retval As Long, WindowText As String, WindowClass As String On Error GoTo ToLargeWindowText ' 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) ' ermittelte Daten der entsprechenden Listbox hinzufügen With Form1.List1(lParam) .AddItem CStr(hwnd) & " # " & WindowClass & " # " & WindowText .ItemData(.NewIndex) = hwnd End With ' Wert für die weitere Enumerierung zurückgeben EnumProc = 1 Exit Function ' wenn der Fenstertext länger als der maximale verfügbare Puffer ist WindowText = Space(256) Resume Next End Function
' Starten der ausgewählten Enumeration Public Function EnumAllWindows(ByVal WindowType As WindowEnum, Optional _ ParentWindow As Long = 0&) Dim ThreadID As Long, ProcessID As Long Select Case WindowType Case HAUPTFENSTER ' Hauptfenster enumerieren EnumWindows AddressOf EnumProc, WindowType Case THREADFENSTER ' Threadfenster enumerieren If ParentWindow = 0 Then Exit Function ' Thread-ID ermitteln und Enumeration starten ThreadID = GetWindowThreadProcessId(ParentWindow, ProcessID) EnumThreadWindows ThreadID, AddressOf EnumProc, WindowType Case KINDFENSTER ' Kindfenster enumerieren If ParentWindow = 0 Then Exit Function EnumChildWindows ParentWindow, AddressOf EnumProc, WindowType End Select End Function
' schreiben Sie den nachfolgenden Code in ein Fenster das 3 Listboxen (List1 mit _ ' Index 0-2) hat Private Declare Function IsWindowEnabled Lib "user32.dll" (ByVal hwnd As Long) As Long Private Declare Function EnableWindow Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByVal fEnable As Long) As Long
' starten der Enumeration und übertragen aller gefundenen Fenster in die Listbox Private Sub Form_Load() List1(0).Clear EnumAllWindows HAUPTFENSTER List1(0).ListIndex = 0 End Sub
' beim Wählen eines Fensters Kind und Thread-Fenster enumerieren Private Sub List1_Click(Index As Integer) Select Case Index Case 0 List1(1).Clear List1(2).Clear EnumAllWindows THREADFENSTER, List1(0).ItemData(List1(0).ListIndex) EnumAllWindows KINDFENSTER, List1(0).ItemData(List1(0).ListIndex) End Select End Sub
' beim Doppelklick Fenster deaktivieren (disablen) wenn es aktiviert (enabled) ist oder umgekehrt Private Sub List1_DblClick(Index As Integer) If IsWindowEnabled(List1(Index).ItemData(List1(Index).ListIndex)) = 0 Then EnableWindow List1(Index).ItemData(List1(Index).ListIndex), 1 Else EnableWindow List1(Index).ItemData(List1(Index).ListIndex), 0 End If End Sub