如何获取 Java 中所有窗口句柄的列表(使用 JNA)? [英] How to get list of all window handles in Java (Using JNA)?

查看:62
本文介绍了如何获取 Java 中所有窗口句柄的列表(使用 JNA)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JNA 的新手.我正在尝试获取所有窗口的句柄,包括最小化的窗口.我需要所有窗口的 HWND.我已经解决了Windows:如何获得所有可见窗口的列表? 帮助我获取窗口列表,但它的 hWnd 类型为 int.我不能将它与要求 com.sun.jna.platform 类型的 hWndcom.sun.jna.platform.win32.User32 函数一起使用.win32.WinDef.HWND.那么,有没有办法获得 com.sun.jna.platform.win32.WinDef.HWND 类型的所有窗口句柄而不是 int 指针?最后,为什么 intHWND 有区别?它如何接受两者?我有点困惑.谢谢.

I am novice for JNA. I am trying to get handles for all the windows including minimised ones. I need HWND of all the windows. I have gone thro the question Windows: how to get a list of all visible windows? which helped me to get list of windows, but it has hWnd type as int. I can't use it with com.sun.jna.platform.win32.User32 functions which asks for hWnd of type com.sun.jna.platform.win32.WinDef.HWND. So, Is there any way to get all the window handles of type com.sun.jna.platform.win32.WinDef.HWND rather than int pointer? Finally, why is the difference int and HWND? How does it accept both? I am bit confused. Thanks.

我有以下代码(从 Hovercreft 的答案中编辑):

I have the following code(edited from Hovercreft's answer):

    import com.sun.jna.Native;
    import com.sun.jna.Pointer;
    import com.sun.jna.platform.win32.User32;
    import com.sun.jna.platform.win32.WinDef.HWND;
    import com.sun.jna.platform.win32.WinDef.RECT;
    import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;

    public class TryWithHWND {

    public static void main(String[] args) {
        final User32 user32 = User32.INSTANCE;
        user32.EnumWindows(new WNDENUMPROC() {
            int count = 0;
            public boolean callback(HWND hWnd, Pointer arg1) {
                char[] windowText = new char[512];
                user32.GetWindowText(hWnd, windowText, 512);
                String wText = Native.toString(windowText);
                RECT rectangle = new RECT();
                user32.GetWindowRect(hWnd, rectangle);
                // get rid of this if block if you want all windows regardless
                // of whether
                // or not they have text
                // second condition is for visible and non minimised windows
                if (wText.isEmpty() || !(User32.INSTANCE.IsWindowVisible(hWnd)
                        && rectangle.left > -32000)) {
                    return true;
                }
                System.out.println("Found window with text " + hWnd
                        + ", total " + ++count + " Text: " + wText);
                return true;
            }
        }, null);
    }
}

我尝试只使用(不是自定义界面)默认的 User32 类.它工作正常.我有疑问,为什么我们使用用户定义的接口而不是现有的接口?还有一件事,用户定义的方法签名和已经存在的方法签名之间总是有区别的.例如,变量windowTextchar[],而Hovercraft 的变量是byte[] 类型.谁能给我解释一下?谢谢.

I tried to use only(not custom interface) the default User32 class. It is working fine. I have doubt, why we are using userdefined interface instead of already existing one? One more thing, There is always difference between userdefined method signature and already existing ones. For instance, the variable windowText is char[], whereas Hovercraft's variable is of type byte[]. Can anyone explain me? Thanks.

推荐答案

最新版本的 JNA 进行了一些更改,应该可以解决这个问题(作为 JNA 的作者之一,Luke Quinane,表示 此处).如果您使用最新版本并检查 JNAAPI,你会看到WinUser.WNDENUMPROC接口的方法实际上使用WinDef.HWND作为参数,不是long或int.

The latest version of JNA has had some changes that should fix this (as one of the authors of JNA, Luke Quinane, states here). If you use the latest version and check the JNA API, you'll see that the WinUser.WNDENUMPROC interface's method actually uses WinDef.HWND as its parameter, not long or int.

例如:

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;

public class TryWithHWND {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
      int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
   }

   public static void main(String[] args) {
      final User32 user32 = User32.INSTANCE;
      user32.EnumWindows(new WNDENUMPROC() {
         int count = 0;
         @Override
         public boolean callback(HWND hWnd, Pointer arg1) {
            byte[] windowText = new byte[512];
            user32.GetWindowTextA(hWnd, windowText, 512);
            String wText = Native.toString(windowText);

            // get rid of this if block if you want all windows regardless of whether
            // or not they have text
            if (wText.isEmpty()) {
               return true;
            }

            System.out.println("Found window with text " + hWnd + ", total " + ++count
                  + " Text: " + wText);
            return true;
         }
      }, null);
   }
}

这篇关于如何获取 Java 中所有窗口句柄的列表(使用 JNA)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆