You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PowerShell获取键盘光标(高亮/制表位区域)的屏幕位置?

如何用PowerShell获取键盘光标/制表位高亮区域的屏幕位置

针对你提到的需求——通过Tab定位目标区域后,用鼠标点击替代无效的回车/空格操作,以下是两种场景的解决方案:

一、获取文本插入光标(Caret)的屏幕位置

如果目标是文本输入框里的光标,可通过GetCaretPos和ClientToScreenAPI获取其屏幕坐标:

Add-Type @"
using System;
using System.Runtime.InteropServices;

public class CaretHelper {
    [DllImport("user32.dll")]
    public static extern bool GetCaretPos(out POINT lpPoint);
    
    [DllImport("user32.dll")]
    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);
    
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
    
    public struct POINT {
        public int X;
        public int Y;
    }
    
    public static POINT GetCaretScreenPosition() {
        POINT caretPos = new POINT();
        if (GetCaretPos(out caretPos)) {
            IntPtr foregroundWnd = GetForegroundWindow();
            ClientToScreen(foregroundWnd, ref caretPos);
        }
        return caretPos;
    }
}
"@

# 获取并输出光标坐标
$caretPos = [CaretHelper]::GetCaretScreenPosition()
Write-Output "插入光标屏幕坐标:X: $($caretPos.X) | Y: $($caretPos.Y)"

二、获取Tab选中的高亮控件的屏幕位置

如果是通过Tab切换选中的按钮、方块等控件,需要先获取焦点控件的句柄,再获取其屏幕矩形(取中心作为点击位置更稳妥):

Add-Type @"
using System;
using System.Runtime.InteropServices;

public class FocusedControlHelper {
    [DllImport("user32.dll")]
    public static extern IntPtr GetFocus();
    
    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    
    public struct RECT {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
    
    public static RECT GetFocusedControlScreenRect() {
        RECT rect = new RECT();
        IntPtr focusedWnd = GetFocus();
        if (focusedWnd != IntPtr.Zero) {
            GetWindowRect(focusedWnd, out rect);
        }
        return rect;
    }
}
"@

# 获取焦点控件的中心坐标
$controlRect = [FocusedControlHelper]::GetFocusedControlScreenRect()
$centerX = ($controlRect.Left + $controlRect.Right) / 2
$centerY = ($controlRect.Top + $controlRect.Bottom) / 2
Write-Output "焦点控件中心坐标:X: $centerX | Y: $centerY"

三、完整实现:Tab定位+获取位置+模拟双击

结合你的现有代码,整合出完整的自动操作流程:

# 等待你切换到目标应用窗口
Start-Sleep 3

# 引用必要的程序集和API
Add-Type -AssemblyName System.Windows.Forms
Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;

Add-Type @"
using System;
using System.Runtime.InteropServices;

public class FocusedControlHelper {
    [DllImport("user32.dll")]
    public static extern IntPtr GetFocus();
    
    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    
    public struct RECT {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
    
    public static RECT GetFocusedControlScreenRect() {
        RECT rect = new RECT();
        IntPtr focusedWnd = GetFocus();
        if (focusedWnd != IntPtr.Zero) {
            GetWindowRect(focusedWnd, out rect);
        }
        return rect;
    }
}
"@

# 按Tab定位到目标控件
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep 0.5 # 等待控件选中

# 获取焦点控件中心位置
$controlRect = [FocusedControlHelper]::GetFocusedControlScreenRect()
$centerX = ($controlRect.Left + $controlRect.Right) / 2
$centerY = ($controlRect.Top + $controlRect.Bottom) / 2

# 移动鼠标并执行双击
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($centerX, $centerY)
[W.U32]::mouse_event(6,0,0,0,0);
Start-Sleep 0.1
[W.U32]::mouse_event(6,0,0,0,0);

内容的提问来源于stack exchange,提问作者PythonKiddieScripterX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:15:57