Files

53 lines
2.4 KiB
PowerShell

# Снимок окна VMware Workstation в файл PNG.
# Использование: powershell -File shot-vm.ps1 -Out C:\PF\USBInstaller\test\vm-shot.png
# vmrun captureScreen не работает (нет VMware Tools), поэтому снимаем окно с экрана.
param([string]$Out = 'C:\PF\USBInstaller\test\vm-shot.png')
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
Add-Type -Namespace W32 -Name Native -MemberDefinition @'
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h);
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int cmd);
[DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr h, int x, int y, int w, int ht, bool repaint);
public struct RECT { public int Left, Top, Right, Bottom; }
'@
[void][W32.Native]::SetProcessDPIAware()
# ждём окно VMware до 30 секунд
$p = $null
for ($i = 0; $i -lt 60; $i++) {
$p = Get-Process -Name 'vmware' -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -First 1
if ($p) { break }
Start-Sleep -Milliseconds 500
}
if (-not $p) { throw 'Окно VMware не найдено' }
$h = $p.MainWindowHandle
# Окно ВМ всегда прижимаем к правому краю: владелец видит и окно OpenCode слева.
[void][W32.Native]::ShowWindow($h, 9) # 9 = восстановить, если было свёрнуто/развёрнуто
$scr = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$winW = [int]($scr.Width * 0.5)
if ($winW -lt 1100) { $winW = [int]($scr.Width * 0.6) }
$winX = $scr.Width - $winW
[void][W32.Native]::MoveWindow($h, $winX, 0, $winW, $scr.Height, $true)
Start-Sleep -Milliseconds 400
[void][W32.Native]::SetForegroundWindow($h)
Start-Sleep -Milliseconds 500
$r = New-Object W32.Native+RECT
[void][W32.Native]::GetWindowRect($h, [ref]$r)
$w = $r.Right - $r.Left
$ht = $r.Bottom - $r.Top
$bmp = New-Object System.Drawing.Bitmap $w, $ht
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.CopyFromScreen($r.Left, $r.Top, 0, 0, $bmp.Size)
$bmp.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png)
$g.Dispose(); $bmp.Dispose()
"снимок: $Out ${w}x${ht}"