In Windows, how can I view a list of all the titles of all Windows that are open.
(Preferably with some unique identifier like handle)
In Windows, how can I view a list of all the titles of all Windows that are open.
(Preferably with some unique identifier like handle)
Use the EnumWindows() Windows API function. It will invoke the application-defined callback function argument passed to it for all the top-level windows on the screen, passing it the handle of each one, until the callback returns FALSE
.
Here's a simple Python 2.x console program I wrote that uses that function (and a few others to determine which windows might actually be visible on the desktop -- many of the "top-level" windows thatEnumWindows()
enumerates over are invisible -- to accomplish what you want. It makes use of the win32gui
module included in the PyWin32 extension package to gain access to the Windows API. This can also be done at a lower, more direct, level using the built-inctypes
module, but PyWin32 is a more advanced and convenient way to go about it, IMO.
You could redirect the output to a text file or the program could be modified to display the list in a window or dialog box using several different GUI modules available, including the tk/tcl interface module calledTkinter
, which comes standard with the language.
import sys
import win32gui
def callback(hwnd, strings):
if win32gui.IsWindowVisible(hwnd):
window_title = win32gui.GetWindowText(hwnd)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
if window_title and right-left and bottom-top:
strings.append('0x{:08x}: "{}"'.format(hwnd, window_title))
return True
def main():
win_list = [] # list of strings containing win handles and window titles
win32gui.EnumWindows(callback, win_list) # populate list
for window in win_list: # print results
print window
sys.exit(0)
if __name__ == '__main__':
main()
I took the PowerShell script from Enumerating all Windows-windows with a PowerShell function and a callback function and modified it slightly.
It print the window name and handle.
<#
.Synopsis
Enumerieren der vorhandenen Fenster
#>
$TypeDef = @"
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Api
{
public class WinStruct
{
public string WinTitle {get; set; }
public int WinHwnd { get; set; }
}
public class ApiDef
{
private delegate bool CallBackPtr(int hwnd, int lParam);
private static CallBackPtr callBackPtr = Callback;
private static List<WinStruct> _WinStructList = new List<WinStruct>();
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(CallBackPtr lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private static bool Callback(int hWnd, int lparam)
{
StringBuilder sb = new StringBuilder(256);
int res = GetWindowText((IntPtr)hWnd, sb, 256);
_WinStructList.Add(new WinStruct { WinHwnd = hWnd, WinTitle = sb.ToString() });
return true;
}
public static List<WinStruct> GetWindows()
{
_WinStructList = new List<WinStruct>();
EnumWindows(callBackPtr, IntPtr.Zero);
return _WinStructList;
}
}
}
"@
Add-Type -TypeDefinition $TypeDef -Language CSharpVersion3
[Api.Apidef]::GetWindows() | Where-Object { $_.WinTitle -ne "" } | Sort-Object -Property WinTitle | Select-Object WinTitle,@{Name="Handle"; Expression={"{0:X0}" -f $_.WinHwnd}}