From 2c32a8f51d90f5ab9729c96c0a67e57561a7fbdf Mon Sep 17 00:00:00 2001 From: yu <191972707@qq.com> Date: Wed, 28 Oct 2026 15:01:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Ground=20=E8=83=BD=E5=8A=9B=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=89=98=E7=9B=98/=E9=9A=90=E8=97=8F=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E8=87=AA=E5=8A=A8=E5=94=A4=E8=B5=B7=E4=B8=8E=20Attach?= =?UTF-8?q?ThreadInput=20=E5=89=8D=E5=8F=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/capabilities/desktop-uia.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/capabilities/desktop-uia.ts b/src/main/capabilities/desktop-uia.ts index 74a6aa2..f64eb6f 100644 --- a/src/main/capabilities/desktop-uia.ts +++ b/src/main/capabilities/desktop-uia.ts @@ -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); +