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

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

问题描述

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

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 类。它工作正常。我怀疑,为什么我们使用用户定义的界面而不是已经存在的界面呢?还有一件事,用户定义的方法签名和已有的方法签名之间总是存在差异。例如,变量 windowText char [] ,而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,此处。如果您使用最新版本并查看 JNA API ,您将看到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天全站免登陆