rs485-lpc/ui.ps1

93 lines
3.6 KiB
PowerShell
Raw Normal View History

param(
[string]$Click,
[string]$Move,
[string]$DblClick,
[string]$Drag,
[string]$RClick,
[string]$Keys,
[switch]$Enter,
[switch]$MaximizeWindow,
[string]$WindowTitle = 'Designer',
[int]$SleepMs = 800,
[string]$Out
)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UI32 {
[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public const uint LEFTDOWN = 0x0002; public const uint LEFTUP = 0x0004;
public const uint RIGHTDOWN = 0x0008; public const uint RIGHTUP = 0x0010;
public static void RClick(int x, int y) {
SetCursorPos(x, y); System.Threading.Thread.Sleep(100);
mouse_event(RIGHTDOWN, 0, 0, 0, UIntPtr.Zero); System.Threading.Thread.Sleep(60);
mouse_event(RIGHTUP, 0, 0, 0, UIntPtr.Zero);
}
public static void Click(int x, int y) {
SetCursorPos(x, y); System.Threading.Thread.Sleep(100);
mouse_event(LEFTDOWN, 0, 0, 0, UIntPtr.Zero); System.Threading.Thread.Sleep(60);
mouse_event(LEFTUP, 0, 0, 0, UIntPtr.Zero);
}
public static void Dbl(int x, int y) { Click(x, y); System.Threading.Thread.Sleep(80); Click(x, y); }
public static void Drag(int x1, int y1, int x2, int y2) {
SetCursorPos(x1, y1); System.Threading.Thread.Sleep(200);
mouse_event(LEFTDOWN, 0, 0, 0, UIntPtr.Zero); System.Threading.Thread.Sleep(200);
int steps = 20;
for (int i = 1; i <= steps; i++) {
SetCursorPos(x1 + (x2 - x1) * i / steps, y1 + (y2 - y1) * i / steps);
System.Threading.Thread.Sleep(30);
}
System.Threading.Thread.Sleep(200);
mouse_event(LEFTUP, 0, 0, 0, UIntPtr.Zero);
}
}
"@
if ($MaximizeWindow) {
$p = Get-Process pharos_designer -ErrorAction SilentlyContinue
if ($p -and $p.MainWindowHandle -ne [IntPtr]::Zero) {
[UI32]::ShowWindow($p.MainWindowHandle, 3) | Out-Null
[UI32]::SetForegroundWindow($p.MainWindowHandle) | Out-Null
Start-Sleep -Milliseconds 800
}
}
if ($Move) {
$p = $Move -split ','; [UI32]::SetCursorPos([int]$p[0], [int]$p[1])
}
if ($Click) {
$p = $Click -split ','; [UI32]::Click([int]$p[0], [int]$p[1])
}
if ($DblClick) {
$p = $DblClick -split ','; [UI32]::Dbl([int]$p[0], [int]$p[1])
}
if ($RClick) {
$p = $RClick -split ','; [UI32]::RClick([int]$p[0], [int]$p[1])
}
if ($Drag) {
$p = $Drag -split ','; [UI32]::Drag([int]$p[0], [int]$p[1], [int]$p[2], [int]$p[3])
}
if ($Keys) {
Start-Sleep -Milliseconds 300
[System.Windows.Forms.SendKeys]::SendWait($Keys)
}
if ($Enter) {
Start-Sleep -Milliseconds 200
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
}
Start-Sleep -Milliseconds $SleepMs
if ($Out) {
$vs = [System.Windows.Forms.SystemInformation]::VirtualScreen
$bmp = New-Object System.Drawing.Bitmap($vs.Width, $vs.Height)
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.CopyFromScreen($vs.Left, $vs.Top, 0, 0, $bmp.Size)
$g.Dispose()
$bmp.Save($Out, [System.Drawing.Imaging.ImageFormat]::Png)
$bmp.Dispose()
Write-Host ("shot {0}" -f $Out)
}