From 9c973d9bad89004a6a6a0fe656598bee52549fa6 Mon Sep 17 00:00:00 2001 From: yu <191972707@qq.com> Date: Wed, 28 Oct 2026 15:11:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AA=97=E5=8F=A3=E5=94=A4=E8=B5=B7?= =?UTF-8?q?=E6=94=B9=E7=94=A8=20EnumWindows=20=E8=B7=A8=E8=99=9A=E6=8B=9F?= =?UTF-8?q?=E6=A1=8C=E9=9D=A2=E6=9F=A5=E6=89=BE=E5=B9=B6=E7=A7=BB=E5=9B=9E?= =?UTF-8?q?=E4=B8=BB=E5=B1=8F=E5=8F=AF=E8=A7=86=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/capabilities/desktop-uia.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/capabilities/desktop-uia.ts b/src/main/capabilities/desktop-uia.ts index f64eb6f..5788f8f 100644 --- a/src/main/capabilities/desktop-uia.ts +++ b/src/main/capabilities/desktop-uia.ts @@ -62,10 +62,23 @@ function Find-RpaWindow([string]$winName) { $win = $root.FindFirst([System.Windows.Automation.TreeScope]::Children, $cond) 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 + $script:hitHwnd = [IntPtr]::Zero + $cb = [WinRpa.RpaUser32Click+EnumProc]{ param($h, $l) + $sb = New-Object System.Text.StringBuilder 256 + [WinRpa.RpaUser32Click]::GetWindowTextW($h, $sb, 256) | Out-Null + if ($sb.ToString() -eq $winName) { $script:hitHwnd = $h; return $false } + return $true + } + [WinRpa.RpaUser32Click]::EnumWindows($cb, [IntPtr]::Zero) | Out-Null + if ($script:hitHwnd -ne [IntPtr]::Zero) { + [WinRpa.RpaUser32Click]::ShowWindow($script:hitHwnd, 9) | Out-Null + Start-Sleep -Milliseconds 500 + $sw2 = [WinRpa.RpaUser32Click]::GetSystemMetrics(0) + $sh2 = [WinRpa.RpaUser32Click]::GetSystemMetrics(1) + $w2 = [Math]::Min(1400, $sw2 - 120) + $h2 = [Math]::Min(820, $sh2 - 120) + [WinRpa.RpaUser32Click]::SetWindowPos($script:hitHwnd, [IntPtr]::Zero, 60, 60, $w2, $h2, 0x0040) | Out-Null + Start-Sleep -Milliseconds 600 $win = $root.FindFirst([System.Windows.Automation.TreeScope]::Children, $cond) if ($win) { return $win } } @@ -88,7 +101,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); [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 + 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")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr after, int X, int Y, int cx, int cy, uint flags); [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(); [DllImport("user32.dll")] public static extern int GetSystemMetrics(int nIndex); [DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc cb, IntPtr lp); public delegate bool EnumProc(IntPtr hWnd, IntPtr lp); [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowTextW(IntPtr hWnd, System.Text.StringBuilder sb, int max);' -Name RpaUser32Click -Namespace Win32Rpa | Out-Null } `; @@ -340,3 +353,4 @@ register("Element_WaitElementGround", waitElementGround); +