我是 JNA 的新手。我正在尝试获取所有窗口(包括最小化窗口)的句柄。我需要HWND
所有的窗户。我已经解决了这个问题Windows:如何获取所有可见窗口的列表? https://stackoverflow.com/questions/3188484/windows-how-to-get-a-list-of-all-visible-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
and HWND
?它如何接受两者?我有点困惑。谢谢。
我有以下代码(根据 Hovercraft 的答案编辑):
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
is char[]
,而 Hovercraft 的变量类型为byte[]
。谁能给我解释一下吗?谢谢。
JNA 的最新版本进行了一些更改,应该可以解决此问题(正如 JNA 的作者之一 Luke Quinane 所说,here https://stackoverflow.com/a/6672729/522444)。如果您使用最新版本并检查JNA API http://twall.github.com/jna/3.3.0/javadoc/index.html?index-all.html,您将看到 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)