feat: Ground 能力支持托盘/隐藏窗口自动唤起与 AttachThreadInput 前台化
This commit is contained in:
parent
753508b500
commit
2c32a8f51d
@ -60,8 +60,16 @@ function Find-RpaWindow([string]$winName) {
|
||||
if (-not $winName) { return $root }
|
||||
$cond = New-Object System.Windows.Automation.PropertyCondition([System.Windows.Automation.AutomationElement]::NameProperty, $winName)
|
||||
$win = $root.FindFirst([System.Windows.Automation.TreeScope]::Children, $cond)
|
||||
if (-not $win) { throw "window_not_found:$winName" }
|
||||
return $win
|
||||
if ($win) { return $win }
|
||||
Add-RpaUser32
|
||||
$hwnd = [WinRpa.RpaUser32Click]::FindWindowW([IntPtr]::Zero, $winName)
|
||||
if ($hwnd -ne [IntPtr]::Zero) {
|
||||
[WinRpa.RpaUser32Click]::ShowWindow($hwnd, 9) | Out-Null
|
||||
Start-Sleep -Milliseconds 700
|
||||
$win = $root.FindFirst([System.Windows.Automation.TreeScope]::Children, $cond)
|
||||
if ($win) { return $win }
|
||||
}
|
||||
throw "window_not_found:$winName"
|
||||
}
|
||||
function Find-RpaElement($parent, [string]$name, [string]$id, [string]$type, [string]$className, [int]$index) {
|
||||
$conds = @()
|
||||
@ -80,7 +88,7 @@ function Find-RpaElement($parent, [string]$name, [string]$id, [string]$type, [st
|
||||
return $all[$pick - 1]
|
||||
}
|
||||
function Add-RpaUser32 {
|
||||
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")] public static extern void mouse_event(uint flags, uint dx, uint dy, uint data, UIntPtr extra); [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);' -Name RpaUser32Click -Namespace Win32Rpa | Out-Null
|
||||
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")] public static extern void mouse_event(uint flags, uint dx, uint dy, uint data, UIntPtr extra); [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindowW(string lpClassName, string lpWindowName); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); [DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();' -Name RpaUser32Click -Namespace Win32Rpa | Out-Null
|
||||
}
|
||||
`;
|
||||
|
||||
@ -156,7 +164,18 @@ if (-not $offscreen) {
|
||||
if ($h -ne 0) { $hwnd = [IntPtr]$h; break }
|
||||
$ancestor = $walker.GetParent($ancestor)
|
||||
}
|
||||
if ($hwnd -ne [IntPtr]::Zero) { [WinRpa.RpaUser32Click]::SetForegroundWindow($hwnd) | Out-Null; Start-Sleep -Milliseconds 250 }
|
||||
if ($hwnd -ne [IntPtr]::Zero) {
|
||||
$fgHwnd = [WinRpa.RpaUser32Click]::GetForegroundWindow()
|
||||
if ($fgHwnd -ne $hwnd) {
|
||||
$fgPid = [uint32]0
|
||||
$fgThread = [WinRpa.RpaUser32Click]::GetWindowThreadProcessId($fgHwnd, [ref]$fgPid)
|
||||
$thisThread = [WinRpa.RpaUser32Click]::GetCurrentThreadId()
|
||||
[WinRpa.RpaUser32Click]::AttachThreadInput($thisThread, $fgThread, $true) | Out-Null
|
||||
[WinRpa.RpaUser32Click]::SetForegroundWindow($hwnd) | Out-Null
|
||||
[WinRpa.RpaUser32Click]::AttachThreadInput($thisThread, $fgThread, $false) | Out-Null
|
||||
}
|
||||
Start-Sleep -Milliseconds 250
|
||||
}
|
||||
$x = [int]($rect.X + $rect.Width / 2)
|
||||
$y = [int]($rect.Y + $rect.Height / 2)
|
||||
[WinRpa.RpaUser32Click]::SetCursorPos($x, $y) | Out-Null
|
||||
@ -320,3 +339,4 @@ register("Element_WaitElementGround", waitElementGround);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user