Rubrik: System/Windows · Sonstiges | VB-Versionen: VB4, VB5, VB6 | 05.02.02 |
Alle laufenden Programme ermitteln Dieses Beispiel ermittelt alle laufenden Programme und zeigt diese übersichtlich in einem ListView-Control an. | ||
Autor: Thomas Bannert | Bewertung: | Views: 83.952 |
ohne Homepage | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Welche Anwendungen sind alles gestartet? Wie lauten die dazugehörigen TaskIDs bzw. Fenster-Handles? Welcher Text steht in den Titelzeilen?
All diese Fragen beantwortet unser heutiger Tipp.
Alles, was Sie zum Ausprobieren benötigen, ist eine Form, ein ListView-Control, ein CommandButton und ein Modul.
Beim Klicken auf den CommandButton wird die Windows Fensterliste durchlaufen und alle gefundenen Programme im ListView sauber angezeigt, natürlich mit Fenstertitel, TaskID und Fenster-Handle.
' Fensterliste ermitteln und anzeigen Private Sub Command1_Click() GetWindowList ListView End Sub
Sollen alle Fenster ermittelt werden, also auch diejenigen, die nicht direkt sichtbar sind, einfach im GetWindowList-Aufruf als zweiten Parameter False angeben.
Folgenden Code bitte in das Modul einfügen:
Option Explicit ' alle benötigten API's 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 GetWindowThreadProcessId Lib "user32" ( _ ByVal hWnd As Long, _ lpdwProcessId 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
Public Function GetWindowList(oLSV As ListView, _ Optional bVisible As Boolean = True) ' oLSV - Ein beliebiges ListView, das die ' Informationen aufnehmen soll ' bVisible - Wenn True werden nur sichtbare Windows ' angezeigt, ansonsten alles Dim hWnd As Long Dim sTitle As String Dim lTaskID As Long Dim lStyle As Long Dim oListItem As ListItem ' ListView-Spalten festlegen With oLSV.ColumnHeaders .Clear .Add , "hWnd", "hWnd" .Add , "TaskID", "TaskID" .Add , "Visible", "Visible" .Add , "Title", "Title" End With ' Inhalt löschen und umschalten auf "Detail"-Ansicht oLSV.ListItems.Clear oLSV.View = lvwReport ' Erstes WindowHandle holen hWnd = GetWindow(oLSV.Parent.hWnd, GW_HWNDFIRST) Do ' Handle auswerten und im ListView eintragen lStyle = GetWindowLong(hWnd, GWL_STYLE) lStyle = lStyle And (WS_VISIBLE Or WS_BORDER) sTitle = GetWindowTitle(hWnd) lTaskID = GetWindowTaskID(hWnd) If (lStyle = (WS_VISIBLE Or WS_BORDER)) = bVisible Then ' in das ListView eintragen Set oListItem = oLSV.ListItems.Add(, , hWnd) With oListItem .SubItems(1) = lTaskID .SubItems(2) = lStyle = (WS_VISIBLE Or WS_BORDER) .SubItems(3) = sTitle End With End If ' Nächses Handle hWnd = GetWindow(hWnd, GW_HWNDNEXT) Loop Until hWnd = 0 End Function
Private Function GetWindowTaskID(ByVal hWnd As Long) _ As Long ' Ermittelt die TaskID eines Windows anhand des ' Window Handle Dim lResult As Long Dim lTemp As Long lResult = GetWindowThreadProcessId(hWnd, lTemp) GetWindowTaskID = lTemp End Function
Private Function GetWindowTitle(ByVal hWnd As Long) _ As String ' Ermittelt den Namen eines Windows anhand des ' Window Handle Dim lResult As Long Dim sTemp As String lResult = GetWindowTextLength(hWnd) + 1 sTemp = Space(lResult) lResult = GetWindowText(hWnd, sTemp, lResult) GetWindowTitle = Left(sTemp, Len(sTemp) - 1) End Function
Und wenn Sie nun lediglich prüfen wollen, ob z.B. WinWord gestartet ist, können Sie dies folgendermassen tun:
Public Function WordIsRunning(oForm As Form) As Boolean ' Gibt True zurück, wenn Word geladen ist Dim hWnd As Long Dim sTitle As String Dim lStyle As Long WordIsRunning = False ' Ich alter Pessimist ;-) ' Erstes WindowHandle holen hWnd = GetWindow(oForm.hWnd, GW_HWNDFIRST) Do ' Handle auswerten lStyle = GetWindowLong(hWnd, GWL_STYLE) lStyle = lStyle And (WS_VISIBLE Or WS_BORDER) If (lStyle = (WS_VISIBLE Or WS_BORDER)) = True Then ' Titelzeile ermitteln und prüfen, ob ' das "Microsoft Word" darin enthalten ist sTitle = GetWindowTitle(hWnd) If InStr(1, sTitle, "Microsoft Word", _ vbTextCompare) > 0 Then WordIsRunning = True Exit Do End If End If ' Nächses Handle hWnd = GetWindow(hWnd, GW_HWNDNEXT) Loop Until hWnd = 0 End Function