Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

get a list of open windows in taskbar

Is there a way to get a list of the open windows in the task bar?

I can use "System.Diagnostics.Process.GetProcesses" to get a list of all processes on the curent machine, but I want to get just the windows that are open in the taskbar.

thanks!

brokeMyLegBiking
0
brokeMyLegBiking
Asked:
brokeMyLegBiking
1 Solution
 
Bob LearnedCommented:
Imports System.Runtime.InteropServices

Public Class WindowsEnumerator

  Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer

  Private Declare Function EnumWindows Lib "user32" _
   (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

  Private Declare Function GetWindowTextLength _
   Lib "user32" Alias "GetWindowTextLengthA" _
   (ByVal hwnd As Integer) As Integer

  Private Declare Function GetWindowText _
   Lib "user32" Alias "GetWindowTextA" _
   (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer

  Private Declare Function GetClassName _
   Lib "user32" Alias "GetClassNameA" _
   (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer

  Private Declare Function IsWindowVisible Lib "user32" _
   (ByVal hwnd As Integer) As Integer

  Private Declare Function GetParent Lib "user32" _
   (ByVal hwnd As Integer) As Integer


  Private m_listWindows As New ArrayList


  Public Function GetList() As String()

    EnumWindows(AddressOf EnumWindowProc, &H0)

    Return m_listWindows.ToArray(GetType(String))

  End Function  'GetList


  Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

    Dim size As Integer
    Dim [class] As String

    ' Eliminate windows that are not top-level.
    If GetParent(hwnd) = 0 AndAlso IsWindowVisible(hwnd) Then

      ' Get the window title / class name.
      Dim title As String = GetWindowIdentification(hwnd, [class])
      m_listWindows.Add(title)

      Debug.WriteLine(title)

    End If

    ' To continue enumeration, return True
    ' To stop enumeration return False (0).
    ' When 1 is returned, enumeration continues
    ' until there are no more windows left.
    Return 1

  End Function   'EnumWindowProc


  Private Function GetWindowIdentification(ByVal hwnd As Integer, ByRef sClass As String) As String

    Dim sTitle As String = ""

    'get the size of the string required
    'to hold the window title
    Dim nSize As Integer = GetWindowTextLength(hwnd)

    'if the return is 0, there is no title
    If nSize > 0 Then

      sTitle = Space(nSize + 1)

      GetWindowText(hwnd, sTitle, nSize + 1)

    End If

    sClass = Space(64)

    GetClassName(hwnd, sClass, 64)

    sClass = TrimNull(sClass)

    Return TrimNull(sTitle)

  End Function   'GetWindowIdentification


  Private Function TrimNull(ByVal start As String) As String

    Dim pos As Integer = start.IndexOf(vbNullChar)

    If pos > -1 Then
      Return start.Remove(pos, 1)
    End If

    ' If this far, there was
    ' no null character, so return the string.
    Return start

  End Function   'TrimNull

End Class

Sample usage:
    Dim enumerator As New WindowsEnumerator
    Dim list As String() = enumerator.GetList

    Dim builder As New System.Text.StringBuilder
    For Each window As String In list
      builder.Append(window & vbCrLf)
    Next
    MessageBox.Show("Windows=" & builder.ToString)


Bob
0
 
brokeMyLegBikingAuthor Commented:
ok, I'll try that. thx
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now