Add-Type -AssemblyName System.Windows.Forms Add-Type @" using System; using System.Runtime.InteropServices; public struct RECT { public int L,T,R,B; } public struct PT { public int X,Y; } public class W { [DllImport("user32.dll")] public static extern bool SetCursorPos(int x,int y); [DllImport("user32.dll")] public static extern void mouse_event(uint f,uint x,uint y,uint d,UIntPtr e); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] public static extern int GetWindowTextW(IntPtr h, System.Text.StringBuilder s, int n); [DllImport("user32.dll")] public static extern IntPtr FindWindowW(string c, string t); [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r); [DllImport("user32.dll")] public static extern bool GetClientRect(IntPtr h, out RECT r); [DllImport("user32.dll")] public static extern bool ClientToScreen(IntPtr h, ref PT p); [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h); } "@ $log = 'C:\SOTS\ui\log.txt' $out = @() $gp = Get-Process | Where-Object {$_.Name -like 'Sword*'} | Select -First 1; $hw = if ($gp) { $gp.MainWindowHandle } else { [IntPtr]::Zero } if ($hw -eq [IntPtr]::Zero) { $hw = [W]::FindWindowW('Kerberos_Mars_WndCls',$null) } if ($hw -ne [IntPtr]::Zero) { $r = New-Object RECT; [W]::GetWindowRect($hw,[ref]$r) | Out-Null $c = New-Object RECT; [W]::GetClientRect($hw,[ref]$c) | Out-Null $p = New-Object PT; [W]::ClientToScreen($hw,[ref]$p) | Out-Null $out += "game hwnd=$hw win=$($r.L),$($r.T)-$($r.R),$($r.B) client=$($c.R)x$($c.B) clientOrigin=$($p.X),$($p.Y)" } else { $out += "game window not found" } foreach ($line in Get-Content C:\SOTS\ui\cmd.txt) { $p2 = $line -split ' ',3 switch ($p2[0]) { 'fg' { if ($hw -ne [IntPtr]::Zero) { [W]::SetForegroundWindow($hw) | Out-Null; Start-Sleep -m 500 } } 'move' { [W]::SetCursorPos([int]$p2[1],[int]$p2[2]); Start-Sleep -m 300 } 'click' { [W]::SetCursorPos([int]$p2[1],[int]$p2[2]); Start-Sleep -m 600; [W]::mouse_event(2,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 250; [W]::mouse_event(4,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 700 } 'dbl' { [W]::SetCursorPos([int]$p2[1],[int]$p2[2]); Start-Sleep -m 600; 1..2 | % { [W]::mouse_event(2,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 60; [W]::mouse_event(4,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 100 } } 'key' { [System.Windows.Forms.SendKeys]::SendWait($p2[1]); Start-Sleep -m 400 } 'type' { [System.Windows.Forms.SendKeys]::SendWait(($line.Substring(5))); Start-Sleep -m 400 } 'sleep' { Start-Sleep -m ([int]$p2[1]) } } } $sb = New-Object System.Text.StringBuilder 256; [W]::GetWindowTextW([W]::GetForegroundWindow(),$sb,256) | Out-Null $out += "done $(Get-Date -Format HH:mm:ss) fg='$($sb.ToString())'" $out | Set-Content $log