Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

tools/lab/capwin.ps1: find the window by process ID, never bring it forward (#125)

Ahmed Nefzaoui0c8143e12c2c
diff --git a/tools/lab/capwin.ps1 b/tools/lab/capwin.ps1
index 7fe1a709..bc61c1cb 100644
--- a/tools/lab/capwin.ps1
+++ b/tools/lab/capwin.ps1
@@ -1,46 +1,56 @@
-# Screenshots one window (never the whole screen): by -Title prefix (any visible top-level
-# window), else the main window of -ProcName.
-param([string]$ProcName = "IDMan", [string]$Title = "", [Parameter(Mandatory)][string]$Out)
+# Screenshots one window (never the whole screen) of one process: the visible top-level window of
+# -Pid whose title starts with -Title (wildcards allowed), or its only one when -Title is left out.
+# Several copies of an app share a window title, so the process ID is required; no window, or more
+# than one, is an error that lists what the process shows. PrintWindow lets the window draw itself,
+# so it works off-screen and behind other windows: nothing is shown, restored, activated or brought
+# forward (a minimized window can't be drawn and is an error).
+param(
+ [Parameter(Mandatory)][Alias("Pid")][int]$ProcessId,
+ [string]$Title = "",
+ [Parameter(Mandatory)][string]$Out
+)
Add-Type -AssemblyName System.Drawing
Add-Type @"
-using System; using System.Text; using System.Runtime.InteropServices;
-public static class W {
+using System; using System.Text; using System.Collections.Generic; using System.Runtime.InteropServices;
+public static class CapWin {
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr h, IntPtr hdc, uint f);
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr h, int a, out RECT r, int n);
- [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h);
- [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int c);
- public delegate bool EnumProc(IntPtr h, IntPtr l);
- [DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc f, IntPtr l);
- [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);
- [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
+ [DllImport("user32.dll")] public static extern bool IsIconic(IntPtr h);
+ [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
+ [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint p);
+ [DllImport("user32.dll", CharSet=CharSet.Unicode)] static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
+ delegate bool EnumProc(IntPtr h, IntPtr l);
+ [DllImport("user32.dll")] static extern bool EnumWindows(EnumProc f, IntPtr l);
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int L, T, R, B; }
+ public static string Title(IntPtr h) { var sb = new StringBuilder(512); GetWindowText(h, sb, 512); return sb.ToString(); }
+ // Visible, titled top-level windows of one process.
+ public static List<IntPtr> Of(uint pid) {
+ var o = new List<IntPtr>();
+ EnumWindows((h, l) => { uint p; GetWindowThreadProcessId(h, out p); if (p == pid && IsWindowVisible(h) && Title(h).Length > 0) o.Add(h); return true; }, IntPtr.Zero);
+ return o;
+ }
}
"@
-$script:h = [IntPtr]::Zero
-if ($Title) {
- [W]::EnumWindows({ param($w, $l)
- $sb = New-Object System.Text.StringBuilder 256; [W]::GetWindowText($w, $sb, 256) | Out-Null
- if ([W]::IsWindowVisible($w) -and $sb.ToString() -like "$Title*") { $script:h = $w; return $false }
- return $true }, [IntPtr]::Zero) | Out-Null
-} else {
- $p = Get-Process $ProcName -ErrorAction Stop | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -First 1
- if ($p) { $script:h = $p.MainWindowHandle }
+if (-not (Get-Process -Id $ProcessId -ErrorAction SilentlyContinue)) { throw "no process $ProcessId" }
+$all = [CapWin]::Of([uint32]$ProcessId)
+$found = @($all | Where-Object { [CapWin]::Title($_) -like "$Title*" })
+if ($found.Count -ne 1) {
+ $titles = ($all | ForEach-Object { " '$([CapWin]::Title($_))'" }) -join "`n"
+ $what = if ($found.Count -eq 0) { "no window" } else { "$($found.Count) windows" }
+ throw "$what of process $ProcessId titled '$Title*'; it shows:`n$titles"
}
-if ($script:h -eq [IntPtr]::Zero) { throw "window not found" }
-$h = $script:h
-[W]::ShowWindow($h, 5) | Out-Null; [W]::SetForegroundWindow($h) | Out-Null
-Start-Sleep -Milliseconds 500
-$r = New-Object W+RECT; [W]::GetWindowRect($h, [ref]$r) | Out-Null
+$h = $found[0]
+if ([CapWin]::IsIconic($h)) { throw "'$([CapWin]::Title($h))' is minimized; PrintWindow can't draw it" }
+$r = New-Object CapWin+RECT; [CapWin]::GetWindowRect($h, [ref]$r) | Out-Null
$bmp = New-Object System.Drawing.Bitmap ($r.R - $r.L), ($r.B - $r.T)
$g = [System.Drawing.Graphics]::FromImage($bmp); $hdc = $g.GetHdc()
-[W]::PrintWindow($h, $hdc, 2) | Out-Null
+[CapWin]::PrintWindow($h, $hdc, 2) | Out-Null # PW_RENDERFULLCONTENT
$g.ReleaseHdc($hdc); $g.Dispose()
# Keep the visible bounds (DWMWA_EXTENDED_FRAME_BOUNDS: no invisible resize border), less the 1 px
# window border Windows 11 draws round them, which PrintWindow paints black.
-$v = New-Object W+RECT; [W]::DwmGetWindowAttribute($h, 9, [ref]$v, 16) | Out-Null
+$v = New-Object CapWin+RECT; [CapWin]::DwmGetWindowAttribute($h, 9, [ref]$v, 16) | Out-Null
$crop = New-Object System.Drawing.Rectangle ($v.L - $r.L + 1), ($v.T - $r.T + 1), ($v.R - $v.L - 2), ($v.B - $v.T - 2)
$shot = $bmp.Clone($crop, $bmp.PixelFormat); $bmp.Dispose()
$shot.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png); $shot.Dispose()
-$sb = New-Object System.Text.StringBuilder 256; [W]::GetWindowText($h, $sb, 256) | Out-Null
-"$($sb.ToString()) -> $Out"
+"$([CapWin]::Title($h)) (process $ProcessId) -> $Out"
diff --git a/tools/lab/download-flow.ps1 b/tools/lab/download-flow.ps1
index 3ecd9e95..61d31b37 100644
--- a/tools/lab/download-flow.ps1
+++ b/tools/lab/download-flow.ps1
@@ -42,7 +42,8 @@ function Wait-New([string]$like, [int]$sec) {
}
throw "no window like '$like' within $sec s"
}
-function Shot($h, $name) { Start-Sleep -Milliseconds 700; & "$PSScriptRoot\capwin.ps1" -Title ([DF]::Title($h)) -Out (Join-Path $OutDir $name) }
+# By a title pattern, not the title read now: the progress window's title changes every second.
+function Shot([string]$like, $name) { Start-Sleep -Milliseconds 700; & "$PSScriptRoot\capwin.ps1" -Pid $idmPid -Title $like -Out (Join-Path $OutDir $name) }
function Press($h, [string]$caption) {
foreach ($c in @($caption, "&$caption")) {
$b = [DF]::FindWindowEx($h, [IntPtr]::Zero, "Button", $c)
@@ -58,13 +59,13 @@ try {
Start-Sleep -Milliseconds 800
& "C:\Program Files (x86)\Internet Download Manager\IDMan.exe" /d "http://127.0.0.1:$Port/gen/$Size/$file" /p $dl /f $file
$info = Wait-New "Download File Info*" 20
- Shot $info "file-info.png"
+ Shot "Download File Info*" "file-info.png"
Press $info "Start Download"
$prog = Wait-New "*$file*" 20
Start-Sleep -Seconds 3
- Shot $prog "progress.png"
+ Shot "*$file*" "progress.png"
$done = Wait-New "Download complete*" 600
- Shot $done "complete.png"
+ Shot "Download complete*" "complete.png"
[DF]::PostMessage($done, 0x111, [IntPtr]2, [IntPtr]::Zero) | Out-Null # IDCANCEL = Close
} finally {
Stop-Process $srv.Id -ErrorAction SilentlyContinue
diff --git a/tools/lab/showcase.ps1 b/tools/lab/showcase.ps1
index b46e7ec9..2dc233e7 100644
--- a/tools/lab/showcase.ps1
+++ b/tools/lab/showcase.ps1
@@ -34,7 +34,7 @@ $srv = Start-Process -PassThru -WindowStyle Hidden "$root\target\release\labserv
$env:MDM_DATA_DIR = "$work\data"
try {
Start-Sleep -Milliseconds 800
- Start-Process $exe
+ $app = Start-Process -PassThru $exe
Start-Sleep 3
& "$PSScriptRoot\to-vscode-monitor.ps1" -Title "Mister Download Manager" -DX 185 -DY 570 -Width 929 -Height 465 | Out-Null
$files = @(
@@ -55,7 +55,7 @@ try {
# The progress dialog of the ISO: double-click its row (last row).
& "$PSScriptRoot\probe-own.ps1" -X 300 -Y 259 -Double -NoEscape
Start-Sleep 2
- & "$PSScriptRoot\capwin.ps1" -Title "*ubuntu-26.04*" -Out "$root\$OutDir\progress-dark.png"
+ & "$PSScriptRoot\capwin.ps1" -Pid $app.Id -Title "*ubuntu-26.04*" -Out "$root\$OutDir\progress-dark.png"
Close-Titled "*ubuntu-26.04*"
# Light mode: View → Dark Mode support.
& "$PSScriptRoot\probe-own.ps1" -X 190 -Y 40 -HoverX 210 -HoverY 174 -HoverClick -NoEscape
@@ -63,7 +63,7 @@ try {
& "$PSScriptRoot\cap-own.ps1" -Out "$root\$OutDir\main-light.png"
& "$PSScriptRoot\probe-own.ps1" -X 300 -Y 259 -Double -NoEscape
Start-Sleep 2
- & "$PSScriptRoot\capwin.ps1" -Title "*ubuntu-26.04*" -Out "$root\$OutDir\progress-light.png"
+ & "$PSScriptRoot\capwin.ps1" -Pid $app.Id -Title "*ubuntu-26.04*" -Out "$root\$OutDir\progress-light.png"
} finally {
Get-Process misterdownloadmanager -ErrorAction SilentlyContinue | Stop-Process
Stop-Process $srv.Id -ErrorAction SilentlyContinue