diff --git a/resources/desktop-picker.ps1 b/resources/desktop-picker.ps1 index b4ab039..6f4c641 100644 --- a/resources/desktop-picker.ps1 +++ b/resources/desktop-picker.ps1 @@ -15,6 +15,29 @@ if ($Mode -eq "windows") { exit 0 } +function Get-DeepestAtPoint([double]$px, [double]$py) { + $point = New-Object System.Windows.Automation.Point($px, $py) + $cur = [System.Windows.Automation.AutomationElement]::FromPoint($point) + if (-not $cur) { return $null } + for ($depth = 0; $depth -lt 30; $depth++) { + $children = $cur.FindAll([System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition) + $next = $null + $nextArea = [double]::MaxValue + foreach ($c in $children) { + try { + $r = $c.Current.BoundingRectangle + if ($r.Width -gt 0 -and $r.Height -gt 0 -and $px -ge $r.X -and $px -le ($r.X + $r.Width) -and $py -ge $r.Y -and $py -le ($r.Y + $r.Height)) { + $area = $r.Width * $r.Height + if ($area -lt $nextArea) { $next = $c; $nextArea = $area } + } + } catch {} + } + if (-not $next) { break } + $cur = $next + } + return $cur +} + $form = New-Object System.Windows.Forms.Form $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None $form.TopMost = $true @@ -35,9 +58,7 @@ $result = $null while ($true) { $ptX = [System.Windows.Forms.Cursor]::Position.X $ptY = [System.Windows.Forms.Cursor]::Position.Y - $point = New-Object System.Windows.Automation.Point($ptX, $ptY) - $el = $null - try { $el = [System.Windows.Automation.AutomationElement]::FromPoint($point) } catch {} + $el = Get-DeepestAtPoint $ptX $ptY $down = ([RpaPick.PickW32]::GetAsyncKeyState(32) -band 0x8000) -ne 0 $esc = ([RpaPick.PickW32]::GetAsyncKeyState(27) -band 0x8000) -ne 0 if ($esc) { break }