【问题标题】:How to get list of all window handles in Java (Using JNA)?如何获取Java中所有窗口句柄的列表(使用JNA)?
【发布时间】:2012-02-01 19:56:43
【问题描述】:

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

我有以下代码(根据 Hovercreft 的回答编辑):

    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[],而气垫船的变量是byte[] 类型。谁能解释我?谢谢。

【问题讨论】:

  • 在我的回答中添加了使用 WinDef.HWND 的示例代码。

标签: java windows winapi jna


【解决方案1】:

最新版本的 JNA 进行了一些更改,应该可以解决这个问题(正如 JNA 的作者之一 Luke Quinane 所说 here)。如果您使用最新版本并查看JNA API,您会看到WinUser.WNDENUMPROC接口的方法实际上使用WinDef.HWND作为其参数,而不是long或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);
   }
}

【讨论】:

  • 非常感谢!我在我的问题中添加了我的代码(从你的代码编辑)。你能帮我理解更多吗?再次感谢。
  • HWND 被清除。为什么默认 User32 将 char[] 作为参数,但您定义的参数却是 byte[]
  • 如果访问 unicode 版本的方法(SomeFunctionW),您必须使用 char[] 作为 LPTCHAR 参数;如果使用 ascii 版本 (SomeFunctionA),则必须使用 byte[]。根据是否定义了 UNICODE,MS 标头自动将“SomeFunction”映射到“SomeFunctionW”或“SomeFunctionA”; JNA 根据传递给 Native.loadLibrary 的选项完成同样的事情。您也可以像上面的示例一样显式使用“A”或“W”后缀。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 2011-01-28
相关资源
最近更新 更多