我是JNA的新手。 我正试图得到包括最小化的所有窗口句柄。 我需要所有窗口的HWND
。 我经历了Windows的问题:如何获得所有可见窗口的列表? 这帮助我得到了Windows的列表,但它有hWnd
types为int。 我不能用com.sun.jna.platform.win32.User32
函数来请求com.sun.jna.platform.win32.WinDef.HWND
types的hWnd
。 所以,有没有办法得到所有types的com.sun.jna.platform.win32.WinDef.HWND
而不是int指针的窗口句柄? 最后,为什么int
和HWND
的区别? 它如何接受? 我有点困惑。 谢谢。
我有下面的代码(从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
类。 它工作正常。 我怀疑,为什么我们使用用户定义的接口,而不是已经存在的接口? 还有一点,用户定义的方法签名和已经存在的签名之间总是有区别的。 例如,variableswindowText
是char[]
,而Hovercraft的variables是byte[]
types。 任何人都可以解释我? 谢谢。
最新版本的JNA已经有了一些改变,应该解决这个问题(作为JNA的作者之一Luke Quinane 在这里指出)。 如果您使用最新版本并检查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); } }