| 1 | # Starts an exe, waits for its main window, saves it as PNG with PrintWindow flag 2 (CopyFromScreen is black for GPUI), stops it. |
| 2 | # Usage: powershell -ExecutionPolicy Bypass -File window-shot.ps1 -Exe C:\x\teams.exe -Out C:\x\shot.png [-Arguments "--demo"] [-WaitSeconds 6] [-Width 720 -Height 640] [-Keys "^k","plat"] [-PostKeys DOWN,ENTER] |
| 3 | # -Keys are SendKeys strings sent to the focused window before the shot. |
| 4 | # -AllWindows composes every visible top-level window of the process (toasts, pill) onto a primary-screen canvas. |
| 5 | # -PostKeys (UP, DOWN, ENTER, TAB, ESC, BACK or characters) go straight to the window as messages, no foreground needed. |
| 6 | param( |
| 7 | [Parameter(Mandatory = $true)][string]$Exe, |
| 8 | [Parameter(Mandatory = $true)][string]$Out, |
| 9 | [string]$Arguments = "", |
| 10 | [int]$WaitSeconds = 6, |
| 11 | [int]$Width = 0, |
| 12 | [int]$Height = 0, |
| 13 | [string[]]$Keys = @(), |
| 14 | [string[]]$PostKeys = @(), |
| 15 | [switch]$KeepRunning, |
| 16 | [switch]$AllWindows |
| 17 | ) |
| 18 | Add-Type -AssemblyName System.Drawing |
| 19 | Add-Type -AssemblyName System.Windows.Forms |
| 20 | Add-Type -TypeDefinition @' |
| 21 | using System; using System.Runtime.InteropServices; |
| 22 | public static class Shot { |
| 23 | [DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr window, IntPtr deviceContext, uint flags); |
| 24 | [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr window); |
| 25 | [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr window, uint message, IntPtr wParam, IntPtr lParam); |
| 26 | [DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr window, int x, int y, int width, int height, bool repaint); |
| 27 | [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr window, out RECT rect); |
| 28 | [DllImport("user32.dll")] static extern bool EnumWindows(EnumProc callback, IntPtr parameter); |
| 29 | [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr window, out uint processId); |
| 30 | [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr window); |
| 31 | public delegate bool EnumProc(IntPtr window, IntPtr parameter); |
| 32 | public static System.Collections.Generic.List<IntPtr> VisibleWindowsOf(uint processId) { |
| 33 | var found = new System.Collections.Generic.List<IntPtr>(); |
| 34 | EnumWindows((window, parameter) => { uint owner; GetWindowThreadProcessId(window, out owner); if (owner == processId && IsWindowVisible(window)) found.Add(window); return true; }, IntPtr.Zero); |
| 35 | return found; |
| 36 | } |
| 37 | [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; } |
| 38 | } |
| 39 | '@ |
| 40 | $startInfo = @{ FilePath = $Exe; PassThru = $true } |
| 41 | if ($Arguments -ne "") { $startInfo.ArgumentList = $Arguments } |
| 42 | $process = Start-Process @startInfo |
| 43 | $deadline = (Get-Date).AddSeconds(30) |
| 44 | while ($process.MainWindowHandle -eq 0 -and (Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 200; $process.Refresh() } |
| 45 | if ($process.MainWindowHandle -eq 0) { Write-Error "no window"; Stop-Process -Id $process.Id -Force; exit 1 } |
| 46 | Start-Sleep -Seconds $WaitSeconds |
| 47 | if ($Width -gt 0 -and $Height -gt 0) { |
| 48 | [void][Shot]::MoveWindow($process.MainWindowHandle, 40, 40, $Width, $Height, $true) |
| 49 | Start-Sleep -Seconds 2 |
| 50 | } |
| 51 | if ($Keys.Count -gt 0) { |
| 52 | [void][Shot]::SetForegroundWindow($process.MainWindowHandle) |
| 53 | Start-Sleep -Milliseconds 500 |
| 54 | foreach ($key in $Keys) { [System.Windows.Forms.SendKeys]::SendWait($key); Start-Sleep -Milliseconds 400 } |
| 55 | Start-Sleep -Seconds 1 |
| 56 | } |
| 57 | $virtualKeys = @{ UP = 0x26; DOWN = 0x28; ENTER = 0x0D; TAB = 0x09; ESC = 0x1B; BACK = 0x08 } |
| 58 | foreach ($key in ($PostKeys | ForEach-Object { $_ -split ',' })) { |
| 59 | if ($virtualKeys.ContainsKey($key)) { |
| 60 | [void][Shot]::PostMessage($process.MainWindowHandle, 0x100, [IntPtr]$virtualKeys[$key], [IntPtr]0x01000001) |
| 61 | [void][Shot]::PostMessage($process.MainWindowHandle, 0x101, [IntPtr]$virtualKeys[$key], [IntPtr]0xC1000001) |
| 62 | } else { |
| 63 | foreach ($character in $key.ToCharArray()) { [void][Shot]::PostMessage($process.MainWindowHandle, 0x102, [IntPtr][int]$character, [IntPtr]0) } |
| 64 | } |
| 65 | Start-Sleep -Milliseconds 400 |
| 66 | } |
| 67 | if ($PostKeys.Count -gt 0) { Start-Sleep -Seconds 1 } |
| 68 | if ($AllWindows) { |
| 69 | $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds |
| 70 | $canvas = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height |
| 71 | $canvasGraphics = [System.Drawing.Graphics]::FromImage($canvas) |
| 72 | $canvasGraphics.Clear([System.Drawing.Color]::FromArgb(15, 15, 16)) |
| 73 | $handles = [Shot]::VisibleWindowsOf([uint32]$process.Id) | Sort-Object { $_ -eq $process.MainWindowHandle } -Descending |
| 74 | foreach ($handle in $handles) { |
| 75 | $windowRect = New-Object Shot+RECT |
| 76 | [void][Shot]::GetWindowRect($handle, [ref]$windowRect) |
| 77 | $windowWidth = $windowRect.Right - $windowRect.Left; $windowHeight = $windowRect.Bottom - $windowRect.Top |
| 78 | if ($windowWidth -le 0 -or $windowHeight -le 0) { continue } |
| 79 | $part = New-Object System.Drawing.Bitmap $windowWidth, $windowHeight |
| 80 | $partGraphics = [System.Drawing.Graphics]::FromImage($part) |
| 81 | $partContext = $partGraphics.GetHdc() |
| 82 | [void][Shot]::PrintWindow($handle, $partContext, 2) |
| 83 | $partGraphics.ReleaseHdc($partContext) |
| 84 | $canvasGraphics.DrawImage($part, $windowRect.Left - $screen.Left, $windowRect.Top - $screen.Top) |
| 85 | $partGraphics.Dispose(); $part.Dispose() |
| 86 | } |
| 87 | $canvas.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png) |
| 88 | $canvasGraphics.Dispose(); $canvas.Dispose() |
| 89 | Write-Output "saved $($screen.Width)x$($screen.Height) with $($handles.Count) windows" |
| 90 | if (-not $KeepRunning) { Stop-Process -Id $process.Id -Force } |
| 91 | exit 0 |
| 92 | } |
| 93 | $rect = New-Object Shot+RECT |
| 94 | [void][Shot]::GetWindowRect($process.MainWindowHandle, [ref]$rect) |
| 95 | $width = $rect.Right - $rect.Left; $height = $rect.Bottom - $rect.Top |
| 96 | $bitmap = New-Object System.Drawing.Bitmap $width, $height |
| 97 | $graphics = [System.Drawing.Graphics]::FromImage($bitmap) |
| 98 | $deviceContext = $graphics.GetHdc() |
| 99 | [void][Shot]::PrintWindow($process.MainWindowHandle, $deviceContext, 2) |
| 100 | $graphics.ReleaseHdc($deviceContext) |
| 101 | $bitmap.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png) |
| 102 | $graphics.Dispose(); $bitmap.Dispose() |
| 103 | Write-Output "saved ${width}x${height}" |
| 104 | if (-not $KeepRunning) { Stop-Process -Id $process.Id -Force } |