获取 Windows 上所有可见窗口的完整解决方案(JNA 实现)

本文详解如何使用 jna 高效获取当前系统中所有真正对用户可见的窗口(非仅 `ws_visible` 样式),并提供实时跟踪、遮挡判断与性能优化建议,适用于 java 桌面覆盖层(overlay)开发。

在 Java 中构建窗口级 Overlay(如游戏辅助、多窗口信息面板)时,仅依赖 IsWindowVisible(HWND) 判断窗口“可见性”极易导致误判——该 API 仅检测窗口是否设置了 WS_VISIBLE 样式,不反映实际屏幕可见状态:最小化窗口、被完全遮挡窗口、甚至坐标为 (0,0,0,0) 的无效窗口都可能返回 true。

✅ 正确方案:使用 JNA WindowUtils.getAllWindows()

JNA 官方扩展库已封装更可靠的窗口枚举逻辑。推荐直接使用 com.sun.jna.platform.WindowUtils:

import com.sun.jna.platform.WindowUtils;
import com.sun.jna.platform.DesktopWindow;

// 获取所有「逻辑可见」窗口(即 IsWindowVisible() == true)
List allVisibleWindows = WindowUtils.getAllWindows(true);

// 获取全部窗口(含隐藏/不可见),供后续 Z-order 分析
List allWindows = WindowUtils.getAllWindows(false);

DesktopWindow 对象包含关键字段:

  • hwnd: 原生窗口句柄(HWND)
  • title: 窗口标题(UTF-8 安全)
  • locAndSize: Rectangle 类型,含 x, y, width, height
  • filePath: 可执行文件路径(需管理员权限,部分系统受限)
⚠️ 注意:getAllWindows(true) 仍不保证窗口处于用户视野中——它只是过滤掉 IsWindowVisible()==false 的窗口(如托盘图标、后台服务窗口)。要判断是否“真正可见”,需进一步分析 Z-order 与空间重叠。

? 判断「用户实际可见性」:Z-order + 几何遮挡检测

Windows 窗口按 Z-order(堆叠顺序)管理,顶层窗口优先渲染。可通过以下步骤估算可见性:

  1. 获取窗口 Z-order 序列(推荐使用 OSHI):
    OSHI 是 JNA 生态中成熟跨平台系统信息库,其 DesktopWindow 扩展了 order 字段(从 0 开始,0 表示最顶层)和 visible(增强版可见性标记):

    import oshi.SystemInfo;
    import oshi.software.os.OperatingSystem;
    import oshi.software.os.DesktopWindow;
    
    SystemInfo si = new SystemInfo();
    OperatingSystem os = si.getOperatingSystem();
    List windows = os.getDesktopWindows(); // 已按 Z-order 升序排列(0=最前)
  2. 快速遮挡判定(角点法)
    对目标窗口 target,遍历所有 order > target.order 的窗口(即位于其上方的窗口),检查其矩形是否覆盖 target 的任意一个角点(左上、右上、左下、右下):

    public static boolean isPartiallyVisible(DesktopWindow target, List allWindows) {
        Rectangle tRect = target.getLocAndSize();
        Point[] corners = {
            new Point(tRect.x, tRect.y),
            new Point(tRect.x + tRect.width, tRect.y),
            new Point(tRect.x, tRect.y + tRect.height),
            new Point(tRect.x + tRect.width, tRect.y + tRect.height)
        };
    
        int targetOrder = target.getOrder();
        for (DesktopWindow w : allWindows) {
            if (w.getOrder() > targetOrder) { // 仅检查上方窗口
                Rectangle wRect = w.getLocAndSize();
                for (Point p : corners) {
                    if (wRect.contains(p)) {
                        return true; // 至少一个角被遮挡 → 视为部分可见(可自定义逻辑)
                    }
                }
            }
        }
        return tRect.width > 0 && tRect.height > 0; // 确保窗口尺寸有效
    }

    ? 提示:若需更高精度(如判断 50% 区域可见),可用 Area 计算交集面积;但角点法在 95% 场景下足够高效且低开销。

⚙️ 性能优化建议(实时 Overlay 场景)

  • 采样频率控制:避免每帧调用 getAllWindows()。建议 200–500ms 间隔轮询(ScheduledExecutorService),或监听 Windows EVENT_OBJECT_LOCATIONCHANGE(需 AccessibleObject 注册,复杂度高)。
  • 缓存与增量更新:首次全量获取后,后续仅比对 hwnd 和 locAndSize 变化,减少重复计算。
  • 过滤无关窗口:跳过 className 为 "Shell_TrayWnd"、"TaskManagerWindow" 或标题为空的系统窗口。
  • 异常防护:EnumWindows 可能因权限/崩溃进程失败,务必 try-catch 并降级处理(如复用上一帧数据)。

✅ 总结

方法 是否检测真实可见性 性能 推荐用途
User32.IsWindowVisible() ❌(仅样式) ⚡ 极快 快速排除完全隐藏窗口
WindowUtils.getAllWindows(true) ❌(同上) ⚡ 快 基础窗口发现
OSHI.getDesktopWindows() + Z-order 分析 ✅(可配置精度) ? 中等 Overlay 实时跟踪核心逻辑

最终,构建鲁棒的 Overlay,应组合使用:JNA WindowUtils 快速初筛 → OSHI 获取 Z-order → 自定义几何判定决定显示/隐藏。此方案兼顾准确性、可维护性与生产环境稳定性。