Results 1 to 15 of 15

Thread: [RESOLVED] Get List Of All Visible Windows WinTitle and Hwnd...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Resolved [RESOLVED] Get List Of All Visible Windows WinTitle and Hwnd...

    I'd like to enumerate into a ListBox a list of all of the visible windows that are open, along with their hWnd in Windows 10. Thanks!

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Quote Originally Posted by mcoulter876 View Post
    I'd like to enumerate into a ListBox a list of all of the visible windows that are open, along with their hWnd in Windows 10. Thanks!
    Thanks?
    What have you done so far? Did you search with Google?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Yes, Eduardo... "Thanks". It's customary to thank someone before they offer any actual solution. To answer your question, I have searched all known venues. Thanks.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,064

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Quote Originally Posted by mcoulter876 View Post
    Yes, Eduardo... "Thanks". It's customary to thank someone before they offer any actual solution. To answer your question, I have searched all known venues. Thanks.
    I searched with Google and got plenty of code to do that.

  5. #5
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    So what have you tried and what are you stuck on? Can you enumerate the windows? Can you add things to a list box? Can you get a Hwnd from a window? Have you actually written any code at all?
    Last edited by Shaggy Hiker; Sep 23rd, 2018 at 01:47 PM. Reason: Removed quote of removed post.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Yes, I have written code. I'm not looking for a handout. I merely cannot seem to get what I have to work. It enumerates everything that's running, but not just the visible windows.

    Code:
    Dim DeskTophWnd As Long, WindowhWnd As Long, theWindowTitle As String
        Dim MyStr As String
        Dim Buff As String * 255, WindowsCaption() As String
        Dim WindowsHandle() As Long
        ReDim WindowsCaption(0)
        ReDim WindowsHandle(0)
        DeskTophWnd = GetDesktopWindow
        WindowhWnd = GetWindow(DeskTophWnd, 5)
        Do While (WindowhWnd <> 0)
            GetWindowText WindowhWnd, Buff, 255
            If (Trim(Buff) <> "") And (IsWindowVisible(WindowhWnd) > False) Then
                'ShowWindowAsync WindowhWnd, 0
                ReDim Preserve WindowsCaption(UBound(WindowsCaption) + 1)
                ReDim Preserve WindowsHandle(UBound(WindowsHandle) + 1)
                WindowsCaption(UBound(WindowsCaption)) = Buff
                WindowsHandle(UBound(WindowsHandle)) = WindowhWnd
            End If
            WindowhWnd = GetWindow(WindowhWnd, 2)
            theWindowTitle = GetText(WindowhWnd)
            If Len(GetText(WindowhWnd)) > 3 Then
                If (InStr(theWindowTitle, "Default IME")) Or (InStr(theWindowTitle, "MSCTFIME UI")) Then
                    f = f
                Else
                    List1.AddItem WindowhWnd & " " & Chr$(34) & GetText(WindowhWnd) & Chr$(34)
                End If
            End If
        Loop
    As you can see, I've tried to delimit running processes to only obtain visible windows. This does not seem to work. I'm not sure if it's Windows 10 or what. Thanks.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    That is where you should have started. Loads of people come on here asking others to write their code for them, which is not what this place is about, and they get the same response that you got. Since you already have code, which isn't doing quite what you want, then showing that code to start with and asking why it isn't working will get far better responses than simply asking for the code to do something.
    My usual boring signature: Nothing

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Hi mcoulter

    you will need these ....

    Code:
    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
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Thanks, Chris. I have those included already. As stated, I am able to get every process running (including visible windows), but would like to only obtain handles with visible windows. Appreciate the effort, though.

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Hi,

    I think you have to add

    Code:
    Const GWL_STYLE = (-16)
     
    Const WS_VISIBLE = &H10000000
    Const WS_BORDER = &H800000
    I don't have further Infomation on this PC, you would have to wait, or somebody else will chip in

    also a Link https://docs.microsoft.com/en-us/win...swindowvisible

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    When you say "visible windows that are open", do you mean only those windows that have a taskbar button? If so, here's an alternative implementation that nearly achieves that goal:

    Code:
    Option Explicit
    
    Private Const GWL_STYLE  As Long = -16&
    Private Const WS_CAPTION As Long = &HC00000
    
    Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowLongW Lib "user32.dll" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function GetWindowTextLengthW Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindowTextW Lib "user32.dll" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
    Private Declare Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
    
    Public Sub ListTopLevelWindows(ByRef LB As VB.ListBox)
        LB.Clear
        EnumWindows AddressOf EnumWindowsProc, VarPtr(LB)
    End Sub
    
    Private Function EnumWindowsProc(ByVal hWnd As Long, ByRef lParam As VB.ListBox) As Long
        If (GetWindowLongW(hWnd, GWL_STYLE) And WS_CAPTION) = WS_CAPTION Then
            If IsWindowVisible(hWnd) Then
                lParam.AddItem FormatHex(hWnd) & " """ & GetWindowCaption(hWnd) & """"
            End If
        End If
    
        EnumWindowsProc = -True
    End Function
    
    Private Function FormatHex(ByVal Value As Long) As String
        FormatHex = "&H" & Right$("0000000" & Hex$(Value), 8&)
    End Function
    
    Private Function GetWindowCaption(ByVal hWnd As Long) As String
        Dim cch As Long
    
        cch = GetWindowTextLengthW(hWnd)
    
        If cch Then
            SysReAllocStringLen VarPtr(GetWindowCaption), , cch
            GetWindowTextW hWnd, StrPtr(GetWindowCaption), cch + 1&
        End If
    End Function
    Attached Files Attached Files

  12. #12
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,068

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:14 AM.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Quote Originally Posted by Victor Bravo VI View Post
    When you say "visible windows that are open", do you mean only those windows that have a taskbar button? If so, here's an alternative implementation that nearly achieves that goal:

    Code:
    Option Explicit
    
    Private Const GWL_STYLE  As Long = -16&
    Private Const WS_CAPTION As Long = &HC00000
    
    Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowLongW Lib "user32.dll" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function GetWindowTextLengthW Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindowTextW Lib "user32.dll" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
    Private Declare Function IsWindowVisible Lib "user32.dll" (ByVal hWnd As Long) As Long
    Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
    
    Public Sub ListTopLevelWindows(ByRef LB As VB.ListBox)
        LB.Clear
        EnumWindows AddressOf EnumWindowsProc, VarPtr(LB)
    End Sub
    
    Private Function EnumWindowsProc(ByVal hWnd As Long, ByRef lParam As VB.ListBox) As Long
        If (GetWindowLongW(hWnd, GWL_STYLE) And WS_CAPTION) = WS_CAPTION Then
            If IsWindowVisible(hWnd) Then
                lParam.AddItem FormatHex(hWnd) & " """ & GetWindowCaption(hWnd) & """"
            End If
        End If
    
        EnumWindowsProc = -True
    End Function
    
    Private Function FormatHex(ByVal Value As Long) As String
        FormatHex = "&H" & Right$("0000000" & Hex$(Value), 8&)
    End Function
    
    Private Function GetWindowCaption(ByVal hWnd As Long) As String
        Dim cch As Long
    
        cch = GetWindowTextLengthW(hWnd)
    
        If cch Then
            SysReAllocStringLen VarPtr(GetWindowCaption), , cch
            GetWindowTextW hWnd, StrPtr(GetWindowCaption), cch + 1&
        End If
    End Function
    Thanks, Victor. Any reason why it would list things like Calculator, Calendar, and Microsoft Store when they're not visible, or is that just Windows 10 being Windows 10?

  14. #14
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: Get List Of All Visible Windows WinTitle and Hwnd...

    Quote Originally Posted by mcoulter876 View Post
    Any reason why it would list things like Calculator, Calendar, and Microsoft Store when they're not visible, ...
    If that code listed them, then they definitely are visible (at least from the OS' point of view). According to its documentation, the IsWindowVisible API considers a window visible when that window has the WS_VISIBLE window style. I don't have access to Windows 10 right now, but I suspect the reason you can't see them is either because they're off-screen or their window rectangle is reduced to [0x0]. You might want to verify that using Spy++ or similar window spy tools.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: [RESOLVED] Get List Of All Visible Windows WinTitle and Hwnd...

    After doing some research, Windows 10 seems to have a few applications loaded in the background but not visible. Not sure why they're viewed as "visible", but I believe it might have to do with Microsoft Edge. There are several things that the OS loads on start up that can be changed via Power Shell. Oh well... it does what I need it to. Thanks, Victor.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width