获取 Windows 上所有可见窗口的完整指南(基于 JNA)

本文详解如何使用 jna 高效获取当前系统中所有真正对用户可见的窗口(非仅 `ws_visible` 样式),涵盖 `windowutils.getallwindows()` 的正确用法、可见性判断的深层逻辑、z-order 与遮挡检测策略,并提供生产级实践建议。

在 Java 中开发桌面覆盖层(overlay)时,实时跟踪目标窗口的位置与可见状态是核心需求。你已正确选用 JNA 与 Windows API 交互,但需注意:User32.IsWindowVisible(HWND) 仅检查窗口及其祖先是否设置了 WS_VISIBLE 样式,而非用户实际能否看到该窗口——即使窗口被完全遮挡、最小化或坐标为 (0,0,0,0),该函数仍可能返回 true。

✅ 正确获取“用户可见”窗口的方法

JNA 官方平台库(jna-platform)已封装更实用的接口:

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

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

// 获取全部窗口(含隐藏/不可见)
List allWindows = WindowUtils.getAllWindows(false);

每个 DesktopWindow 实例包含关键信息:

  • hwnd: 原生窗口句柄(可用于后续 GetWindowRect、IsIconic 等调用)
  • title: 窗口标题(UTF-8 安全,已自动处理空指针)
  • locAndSize: java.awt.Rectangle,表示屏幕坐标系下的位置与尺寸(x, y, width, height)
  • filePath: 可选,进程可执行文件路径(需管理员权限,慎用)

⚠️ 注意:getAllWindows(true) 仍不等价于“用户当前可见”。它仅过滤掉 IsWindowVisible == false 的窗口(如任务栏、系统托盘、隐藏工具窗口),但无法识别以下情况:

  • 窗口被其他窗口完全覆盖
  • 窗口已最小化(IsIconic(hwnd) == true)
  • 窗口位于屏幕外(如多显示器环境中的负坐标)

? 进阶:判断“用户实际可见性”

要实现 overlay 的智能显隐,需结合 Z-order 与几何分析。推荐分两步:

1. 排除明显不可见状态

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;

public static boolean isTrulyVisible(HWND hwnd) {
    if (hwnd == null || hwnd.equals(User32.INSTANCE.GetDesktopWindow())) 
        return false;

    // 检查是否最小化或无有效矩形
    Rectangle rect = getWindowRect(hwnd); // 自定义封装,调用 GetWindowRect
    if (rect.width <= 0 || rect.height <= 0 || 
        User32.INSTANCE.IsIconic(hwnd)) 
        return false;

    // 检查 WS_VISIBLE 样式(基础过滤)
    if (!User32.INSTANCE.IsWindowVisible(hwnd)) 
        return false;

    return true;
}

2. Z-order 遮挡检测(轻量级方案)

利用 GetWindow(hwnd, GW_HWNDPREV) 或 EnumWindows + GetWindowPlacement 获取窗口层级,再对比 locAndSize 重叠区域。不建议逐像素检测(性能差),推荐角点检测法:

public static boolean isPartiallyVisible(HWND hwnd, List topWindows) {
    Rectangle target = getWindowRect(hwnd);
    if (target == null) return false;

    // 只检查 Z-order 在其上方的窗口(按 EnumWindows 逆序或 OSHI 的 order 字段)
    for (DesktopWindow top : topWindows) {
        if (top.getHWnd().equals(hwnd)) continue;
        if (top.getOrder() <= getZOrder(hwnd)) continue; // 仅比目标层级高的窗口

        Rectangle topRect = top.getLocAndSize();
        if (target.intersects(topRect)) {
            // 计算重叠面积占比,若 >95% 被覆盖则视为不可见
            Rectangle overlap = target.intersection(topRect);
            double coverage = (double) overlap.width * overlap.height 
                            / (target.width * target.height);
            if (coverage > 0.95) return false;
        }
    }
    return true;
}
? 提示:OSHI 库(new SystemInfo().getOperatingSystem().getDesktopWindows())返回的 DesktopWindow 已含 order 字段(Z-order 降序),且跨平台兼容,适合生产环境直接集成。

⚙️ 性能优化建议

  • 采样频率控制:避免高频轮询(如
  • 缓存窗口句柄:对目标窗口 FindWindow 后缓存 HWND,避免重复字符串匹配开销。
  • 增量更新:首次全量枚举后,后续仅监听 EVENT_OBJECT_CREATE/DESTROY(通过 AccessibleObjectFromEvent 或 SetWinEventHook),减少 EnumWindows 调用。
  • 线程安全:getAllWindows() 是线程安全的,但结果列表需在 Swing/AWT 线程中更新 UI(使用 SwingUtilities.invokeLater)。

✅ 总结

方法 是否满足“用户可见” 适用场景 备注
User32.IsWindowVisible() ❌ 仅样式可见 快速排除隐藏窗口 易误判(遮挡/最小化)
WindowUtils.getAllWindows(true) ⚠️ 半满足 批量获取候选窗口 需二次过滤最小化与遮挡
OSHI.getDesktopWindows() ✅ 推荐 生产级应用 含 Z-order、进程 ID、跨平台
自定义 Z-order + 几何分析 ✅ 精确控制 Overlay 智能显隐 平衡精度与性能

最终,一个健壮的 overlay 跟踪逻辑应组合:句柄缓存 → 样式可见性初筛 → 最小化状态校验 → Z-order 层级排序 → 关键角点重叠判定。这样既规避了 IsWindowVisible 的语义陷阱,又在性能与准确性间取得最佳平衡。