Files
bili-downloader-web/tools/list_windows.ps1
T

45 lines
1.7 KiB
PowerShell

Add-Type @"
using System;
using System.Text;
using System.Runtime.InteropServices;
public class WinEnum {
public delegate bool EnumProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr lParam);
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder s, int n);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out RECT r);
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int L, T, R, B; }
}
"@
$found = New-Object System.Collections.ArrayList
$cb = [WinEnum+EnumProc]{
param($h, $l)
$len = [WinEnum]::GetWindowTextLength($h)
if ($len -gt 0 -and [WinEnum]::IsWindowVisible($h)) {
$sb = New-Object System.Text.StringBuilder ($len + 2)
[void][WinEnum]::GetWindowText($h, $sb, $sb.Capacity)
$t = $sb.ToString()
$r = New-Object WinEnum+RECT
[void][WinEnum]::GetWindowRect($h, [ref]$r)
[void]$found.Add([pscustomobject]@{
Title = $t
W = $r.R - $r.L
H = $r.B - $r.T
X = $r.L
Y = $r.T
})
}
return $true
}
[void][WinEnum]::EnumWindows($cb, [IntPtr]::Zero)
Write-Output "=== 可见顶层窗口中标题含 BiliDownloader 的 ==="
$found | Where-Object { $_.Title -like "*BiliDownloader*" } |
Select-Object Title, W, H, X, Y | Format-Table -AutoSize | Out-String
Write-Output "=== 前 12 个可见窗口(对照)==="
$found | Sort-Object -Property W -Descending | Select-Object -First 12 Title, W, H |
Format-Table -AutoSize | Out-String