Quantcast
Channel: VBForums - API
Viewing all articles
Browse latest Browse all 207

[RESOLVED] No Windows Explorer in Enumerated Window List in Listbox

$
0
0
I have this working code (below) that gets a list of all opened windows.
However, it doesn't show 2 Windows Explorer windows I have open on the desktop.
What do I use to make those to show up in the list??
Thanks in advance. I would really appreciate your input.

Code:

Imports System.IO
Imports System.Runtime.InteropServices

<DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function GetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
    End Function

    Public Const SW_SHOWMINIMIZED As Integer = 2
    Public Const SW_HIDE As Integer = 0

Private Sub frmScriptEditor_Load(sender As Object, e As EventArgs) Handles Me.Load
        'API
        lstActiveWindows.Items.Clear()

        ' Get the current process ID
        Dim currentProcessId As Integer = Process.GetCurrentProcess().Id

        ' Get all running processes
        Dim processes As Process() = Process.GetProcesses()

        ' Iterate through each process
        For Each process As Process In processes
            If process.Id = currentProcessId Then Continue For

            ' Check if the process has a main window handle
            If process.MainWindowHandle <> IntPtr.Zero Then
                ' Check if the main window is visible, not minimized, and not an ApplicationFrameHost process
                ' Get the title of the main window
                Dim titleLength As Integer = GetWindowTextLength(process.MainWindowHandle)
                Dim titleBuilder As New System.Text.StringBuilder(titleLength + 1)
                GetWindowText(process.MainWindowHandle, titleBuilder, titleBuilder.Capacity)
                Dim title As String = titleBuilder.ToString()

                If String.IsNullOrEmpty(title) Then Continue For

                ' Print the title to the console or display it in a ListBox or any other control
                If title = "NVIDIA GeForce Overlay" Or title = "Microsoft Text Input Application" Or title = "Program Manager" Then
                    'skip
                Else
                    lstActiveWindows.Items.Add(title)
                End If

            End If
        Next

    End Sub

Private Structure WINDOWPLACEMENT
        Public Length As Integer
        Public Flags As Integer
        Public ShowCmd As Integer
        Public MinPosition As System.Drawing.Point
        Public MaxPosition As System.Drawing.Point
        Public NormalPosition As System.Drawing.Rectangle
    End Structure

    Private Function IsMinimized(ByVal hWnd As IntPtr) As Boolean
        Dim wp As New WINDOWPLACEMENT()
        wp.Length = Marshal.SizeOf(wp)
        If GetWindowPlacement(hWnd, wp) Then
            Return wp.ShowCmd = SW_SHOWMINIMIZED OrElse wp.ShowCmd = SW_HIDE
        End If
        Return False
    End Function

    Private Function IsSpecialProcess(ByVal process As Process) As Boolean
        Dim processName As String = process.ProcessName.ToLowerInvariant()
        Return processName = "applicationframehost" OrElse processName = "textinputhost" OrElse processName = "systemsettings"
    End Function


Viewing all articles
Browse latest Browse all 207

Trending Articles