| 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 | # -PostKeys (UP, DOWN, ENTER, TAB, ESC, BACK or characters) go straight to the window as messages, no foreground needed. |
| 5 | param( |
| 6 | [Parameter(Mandatory = $true)][string]$Exe, |
| 7 | [Parameter(Mandatory = $true)][string]$Out, |
| 8 | [string]$Arguments = "", |
| 9 | [int]$WaitSeconds = 6, |
| 10 | [int]$Width = 0, |
| 11 | [int]$Height = 0, |
| 12 | [string[]]$Keys = @(), |
| 13 | [string[]]$PostKeys = @(), |
| 14 | [switch]$KeepRunning |
| 15 | ) |
| 16 | Add-Type -AssemblyName System.Drawing |
| 17 | Add-Type -AssemblyName System.Windows.Forms |
| 18 | Add-Type -TypeDefinition @' |
| 19 | using System; using System.Runtime.InteropServices; |
| 20 | public static class Shot { |
| 21 | [DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr window, IntPtr deviceContext, uint flags); |
| 22 | [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr window); |
| 23 | [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr window, uint message, IntPtr wParam, IntPtr lParam); |
| 24 | [DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr window, int x, int y, int width, int height, bool repaint); |
| 25 | [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr window, out RECT rect); |
| 26 | [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; } |
| 27 | } |
| 28 | '@ |
| 29 | $startInfo = @{ FilePath = $Exe; PassThru = $true } |
| 30 | if ($Arguments -ne "") { $startInfo.ArgumentList = $Arguments } |
| 31 | $process = Start-Process @startInfo |
| 32 | $deadline = (Get-Date).AddSeconds(30) |
| 33 | while ($process.MainWindowHandle -eq 0 -and (Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 200; $process.Refresh() } |
| 34 | if ($process.MainWindowHandle -eq 0) { Write-Error "no window"; Stop-Process -Id $process.Id -Force; exit 1 } |
| 35 | Start-Sleep -Seconds $WaitSeconds |
| 36 | if ($Width -gt 0 -and $Height -gt 0) { |
| 37 | [void][Shot]::MoveWindow($process.MainWindowHandle, 40, 40, $Width, $Height, $true) |
| 38 | Start-Sleep -Seconds 2 |
| 39 | } |
| 40 | if ($Keys.Count -gt 0) { |
| 41 | [void][Shot]::SetForegroundWindow($process.MainWindowHandle) |
| 42 | Start-Sleep -Milliseconds 500 |
| 43 | foreach ($key in $Keys) { [System.Windows.Forms.SendKeys]::SendWait($key); Start-Sleep -Milliseconds 400 } |
| 44 | Start-Sleep -Seconds 1 |
| 45 | } |
| 46 | $virtualKeys = @{ UP = 0x26; DOWN = 0x28; ENTER = 0x0D; TAB = 0x09; ESC = 0x1B; BACK = 0x08 } |
| 47 | foreach ($key in ($PostKeys | ForEach-Object { $_ -split ',' })) { |
| 48 | if ($virtualKeys.ContainsKey($key)) { |
| 49 | [void][Shot]::PostMessage($process.MainWindowHandle, 0x100, [IntPtr]$virtualKeys[$key], [IntPtr]0x01000001) |
| 50 | [void][Shot]::PostMessage($process.MainWindowHandle, 0x101, [IntPtr]$virtualKeys[$key], [IntPtr]0xC1000001) |
| 51 | } else { |
| 52 | foreach ($character in $key.ToCharArray()) { [void][Shot]::PostMessage($process.MainWindowHandle, 0x102, [IntPtr][int]$character, [IntPtr]0) } |
| 53 | } |
| 54 | Start-Sleep -Milliseconds 400 |
| 55 | } |
| 56 | if ($PostKeys.Count -gt 0) { Start-Sleep -Seconds 1 } |
| 57 | $rect = New-Object Shot+RECT |
| 58 | [void][Shot]::GetWindowRect($process.MainWindowHandle, [ref]$rect) |
| 59 | $width = $rect.Right - $rect.Left; $height = $rect.Bottom - $rect.Top |
| 60 | $bitmap = New-Object System.Drawing.Bitmap $width, $height |
| 61 | $graphics = [System.Drawing.Graphics]::FromImage($bitmap) |
| 62 | $deviceContext = $graphics.GetHdc() |
| 63 | [void][Shot]::PrintWindow($process.MainWindowHandle, $deviceContext, 2) |
| 64 | $graphics.ReleaseHdc($deviceContext) |
| 65 | $bitmap.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png) |
| 66 | $graphics.Dispose(); $bitmap.Dispose() |
| 67 | Write-Output "saved ${width}x${height}" |
| 68 | if (-not $KeepRunning) { Stop-Process -Id $process.Id -Force } |