Get the list of all active visual Windows in vb.net

The Following code example demonstrate how we can bind a ListBox control with all active visual windows names in vb.net. The example requires a windows form named Form1 and add the Listbox control named lstWindows and One button named btnfind.
 
The example uses Win32API callback function EnumWindows, defined using DllImport instead of Declare. It also calls various other Win32API functions to determine if it is a visual Window.
 

Full Code:

 

Imports System.Runtime.InteropServices
Imports System.Text
 
Public Class Form1
    Public Delegate Function EnumWindowsCallback(ByVal hWnd As Integer, _
                                                  ByVal lParam As Integer) As Boolean
 
    Public Declare Function EnumWindows Lib "user32.dll" _
        Alias "EnumWindows" (ByVal callback As EnumWindowsCallback, _
                             ByVal lParam As Integer) As Integer
 
    <DllImport("user32.dll", EntryPoint:="EnumWindows", SetLastError:=True, _
    CharSet:=CharSet.Ansi, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function EnumWindowsDllImport(ByVal callback As EnumWindowsCallback, _
                                                ByVal lParam As Integer) As Integer
    End Function
 
    Public Declare Sub GetWindowText Lib "user32.dll" _
       Alias "GetWindowTextA" (ByVal hWnd As Integer, _
                               ByVal lpString As StringBuilder, _
                               ByVal nMaxCount As Integer)
    Public Declare Function GetWindowLong Lib "user32.dll" _
      Alias "GetWindowLongA" (ByVal hwnd As Integer, _
                              ByVal nIndex As Integer) As Integer
    Public Declare Function GetWindow Lib "user32.dll" _
        Alias "GetWindow" (ByVal hwnd As Integer, _
                           ByVal wCmd As Integer) As Integer
    Public Declare Function IsWindowVisible Lib "user32.dll" _
    Alias "IsWindowVisible" (ByVal hwnd As Integer) As Boolean
 
    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        EnumWindowsDllImport(New EnumWindowsCallback(AddressOf FillActiveWindowsList_InListBox), 0)
    End Sub
 
    Function FillActiveWindowsList_InListBox(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean
        Dim _windowText As New StringBuilder(255)
        GetWindowText(hWnd, _windowText, 255)
        If ProcessIsActiveWindow(hWnd) Then
            lstWindows.Items.Add(_windowText)
        End If
        Return True
    End Function
 
    Public Const GWL_EXSTYLE As Integer = (-20)
    Public Const WS_EX_TOOLWINDOW As Integer = &H80
    Public Const WS_EX_APPWINDOW As Integer = &H40000
 
    Function ProcessIsActiveWindow(ByVal hWnd As Integer) As Boolean
        Dim _windowText As New StringBuilder(255)
        Dim _windowIsOwned As Boolean
        Dim _windowStyle As Integer
 
        GetWindowText(hWnd, _windowText, 255)
        _windowIsOwned = GetWindow(hWnd, 4) <> 0
        _windowStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
 
        If Not IsWindowVisible(hWnd) Then
            Return False
        End If
 
        If _windowText.ToString.Equals("") Then
            Return False
        End If
        If Win32API.GetParent(hWnd) <> 0 Then
            Return False
        End If
        If (_windowStyle And WS_EX_TOOLWINDOW) <> 0 And Not _windowIsOwned Then
            Return False
        End If
        If (_windowStyle And WS_EX_APPWINDOW) = 0 And _windowIsOwned Then
            Return False
        End If
        Return True
    End Function
End Class