44 lines
2.0 KiB
PowerShell
44 lines
2.0 KiB
PowerShell
param([int]$X = 1611, [int]$Y = 460, [string]$Out = "C:\Users\admin\AppData\Local\Temp\37du-fc.png")
|
|
Add-Type -AssemblyName System.Drawing
|
|
Add-Type @"
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
public class WFC {
|
|
[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 IntPtr GetForegroundWindow();
|
|
[DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
|
|
[DllImport("user32.dll")] public static extern void mouse_event(uint f, uint dx, uint dy, uint d, int e);
|
|
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int cmd);
|
|
public struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
|
|
}
|
|
"@
|
|
$proc = Get-Process -Name electron -ErrorAction Stop | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -First 1
|
|
$hwnd = $proc.MainWindowHandle
|
|
$shell = New-Object -ComObject WScript.Shell
|
|
for ($i = 0; $i -lt 5; $i++) {
|
|
$shell.AppActivate($proc.Id) | Out-Null
|
|
[WFC]::ShowWindow($hwnd, 9) | Out-Null
|
|
[WFC]::SetForegroundWindow($hwnd) | Out-Null
|
|
Start-Sleep -Milliseconds 400
|
|
if ([WFC]::GetForegroundWindow() -eq $hwnd) { break }
|
|
}
|
|
$fg = [WFC]::GetForegroundWindow()
|
|
if ($fg -ne $hwnd) { Write-Output "focus-failed"; exit 2 }
|
|
[WFC]::SetCursorPos($X, $Y) | Out-Null
|
|
Start-Sleep -Milliseconds 250
|
|
[WFC]::mouse_event(0x0002, 0, 0, 0, 0)
|
|
[WFC]::mouse_event(0x0004, 0, 0, 0, 0)
|
|
Start-Sleep -Milliseconds 900
|
|
$rect = New-Object 'WFC+RECT'
|
|
[WFC]::GetWindowRect($hwnd, [ref]$rect) | Out-Null
|
|
$capW = $rect.Right - $rect.Left
|
|
$capH = $rect.Bottom - $rect.Top
|
|
$bmp = New-Object System.Drawing.Bitmap $capW, $capH
|
|
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
|
$g.CopyFromScreen($rect.Left, $rect.Top, 0, 0, $bmp.Size)
|
|
$g.Dispose()
|
|
$bmp.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png)
|
|
$bmp.Dispose()
|
|
Write-Output "ok clicked $X,$Y"
|