geöffnete Fenster auflisten

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Gigalix.

    geöffnete Fenster auflisten

    Ich möchte alle geöffneten Fenster auflisten. Derzeit verwende ich diesen Code:

    VB.NET-Quellcode

    1. Dim pl As Process() = Process.GetProcesses
    2. For Each p As Process In pl
    3. If p.MainWindowTitle <> "" Then
    4. Me.ListBox1.Items.Add(p.MainWindowTitle)
    5. End If
    6. Next


    Wenn mehrere Fenster zum gleichen Prozess gehören, wird damit aber nur ein Fenster aufgelistet (z.B. 2 geöffnete Internet Explorer, dann wird nur der zuletzt aktive in die Liste aufgenommen).
    Wie bekomme ich alle Fenster?
    Hi
    Ich habe den Code gefunden.
    Funktioniert super

    VB.NET-Quellcode

    1. Public Class Form1
    2. Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As _
    3. EnumWindowsProc, ByVal lParam As Int32) As Int32
    4. Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As _
    5. IntPtr) As Boolean
    6. Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam _
    7. As Int32) As Boolean
    8. Public Declare Function GetWindowText Lib "user32.dll" Alias _
    9. "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch _
    10. As Int32) As Int32
    11. Public Declare Function GetWindowTextLength Lib "user32.dll" Alias _
    12. "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32
    13. Public Declare Function GetWindowLong Lib "user32.dll" Alias _
    14. "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32
    15. Public Declare Function GetParent Lib "user32.dll" (ByVal intptr As IntPtr) _
    16. As IntPtr
    17. Public Const GWL_HWNDPARENT As Int32 = -8
    18. Private newwindowlist As List(Of String)
    19. Private Function EnumWinProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) _
    20. As Boolean
    21. If IsWindowVisible(hwnd) Then
    22. If GetParent(hwnd) = IntPtr.Zero Then
    23. If GetWindowLong(hwnd, GWL_HWNDPARENT) = 0 Then
    24. Dim str As String = String.Empty.PadLeft( _
    25. GetWindowTextLength(hwnd) + 1)
    26. GetWindowText(hwnd, str, str.Length)
    27. If Not String.IsNullOrEmpty(str.Substring(0, str.Length - _
    28. 1)) Then newwindowlist.Add(str.Substring(0, str.Length - _
    29. 1))
    30. End If
    31. End If
    32. End If
    33. EnumWinProc = True
    34. End Function
    35. Private Sub RefreshWindowList()
    36. newwindowlist = New List(Of String)
    37. EnumWindows(AddressOf EnumWinProc, CInt(True))
    38. End Sub
    39. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
    40. System.EventArgs) Handles Button1.Click
    41. RefreshWindowList()
    42. ListBox1.Items.Clear()
    43. For Each item As String In newwindowlist
    44. ListBox1.Items.Add(item)
    45. Next
    46. End Sub
    47. End Class