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.
Re: Get List Of All Visible Windows WinTitle and Hwnd...
Originally Posted by mcoulter876
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.
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.
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.
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.
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.
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.
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.
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
Re: Get List Of All Visible Windows WinTitle and Hwnd...
Originally Posted by Victor Bravo VI
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?
Re: Get List Of All Visible Windows WinTitle and Hwnd...
Originally Posted by mcoulter876
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.
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.