'------------- Anfang Projektdatei Project1.vbp -------------
'--------- Anfang Formular "Form1" alias Form1.frm ---------
' Steuerelement: Schaltfläche "Command1"
' Steuerelement: Schaltfläche "Command2"
' Steuerelement: Kontrollkästchen-Steuerelement "Check1"
' Steuerelement: Listen-Steuerelement "List1" (Index von 0 bis 2)
' Steuerelement: Optionsfeld-Steuerelement "Option2"
' Steuerelement: Optionsfeld-Steuerelement "Option1"
' Steuerelement: Beschriftungsfeld "Label3"
' Steuerelement: Beschriftungsfeld "Label2"
' Steuerelement: Beschriftungsfeld "Label1"
Option Explicit
Private Declare Function GetDesktopWindow Lib "user32" () _
As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd _
As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx 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
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal _
dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal _
hProcess As Long, ByVal uExitCode As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5
Const GW_MAX = 5
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Const PROCESS_TERMINATE = &H1
Private Sub Check1_Click()
Call EnumWindows
End Sub
Private Sub Command1_Click()
Call EnumWindows
End Sub
Private Sub Command2_Click()
Dim Result&, Task&
If List1(2).ListIndex > -1 Then
Result = CLng(List1(2).List(List1(2).ListIndex))
Task = OpenProcess(PROCESS_TERMINATE, 0&, Result)
Result = TerminateProcess(Task, 1&)
Result = CloseHandle(Task)
End If
End Sub
Private Sub Form_Load()
Call EnumWindows
End Sub
Private Sub List1_Click(Index As Integer)
Static Flag, x%
If Flag Then Exit Sub
Flag = True
For x = 0 To List1.UBound
List1(x).ListIndex = List1(Index).ListIndex
List1(x).TopIndex = List1(Index).TopIndex
Next x
Flag = False
End Sub
Private Sub EnumWindows()
Dim hWnd&
List1(0).Clear
List1(1).Clear
List1(2).Clear
'Auch der Desktop ist ein Fenster
hWnd = GetDesktopWindow
Call GetWindowInfo(hWnd)
'Einstieg
hWnd = GetWindow(Me.hWnd, GW_HWNDFIRST)
'Alle vorhandenen Fenster abklappern
Do
Call GetWindowInfo(hWnd)
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop Until hWnd = 0
End Sub
Private Sub GetWindowInfo(ByVal hWnd&)
Dim Parent&, Task&, Result&, x&, Style&, Title$
'Darstellung des Fensters
Style = GetWindowLong(hWnd, GWL_STYLE)
Style = Style And (WS_VISIBLE Or WS_BORDER)
'Title des Fenster auslesen
Result = GetWindowTextLength(hWnd) + 1
Title = Space$(Result)
Result = GetWindowText(hWnd, Title, Result)
Title = Left$(Title, Len(Title) - 1)
'In Abhängigkeit der Optionen die Ausgabe erstellen
If (Title <> "" Or (Check1.Value = vbChecked)) And _
(Style = (WS_VISIBLE Or WS_BORDER) Or Option2.Value) Then
List1(0).AddItem CStr(hWnd)
List1(1).AddItem Title
'Elternfenster ermitteln
Parent = hWnd
Do
Parent = GetParent(Parent)
Loop Until Parent = 0
'Task Id ermitteln
Result = GetWindowThreadProcessId(hWnd, Task)
List1(2).AddItem Task
End If
End Sub
Private Sub Option1_Click()
Call EnumWindows
End Sub
Private Sub Option2_Click()
Call EnumWindows
End Sub
'---------- Ende Formular "Form1" alias Form1.frm ----------