[英]How to get list of all window handles in Java (Using JNA)?
I am novice for JNA. 我是JNA的新手。 I am trying to get handles for all the windows including minimised ones.
我试图获得所有窗口的句柄,包括最小化的窗口。 I need
HWND
of all the windows. 我需要所有窗户的
HWND
。 I have gone thro the question Windows: how to get a list of all visible windows? 我已经解决了Windows的问题:如何获取所有可见窗口的列表? which helped me to get list of windows, but it has
hWnd
type as int. 这有助于我获取窗口列表,但它有
hWnd
类型为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
. 我不能在
com.sun.jna.platform.win32.User32
函数中使用它,它要求输入类型为com.sun.jna.platform.win32.WinDef.HWND
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? 那么,有没有办法获得
com.sun.jna.platform.win32.WinDef.HWND
类型的所有窗口句柄而不是int指针? Finally, why is the difference int
and HWND
? 最后,为什么区别
int
和HWND
? How does it accept both? 它如何接受两者? I am bit confused.
我有点困惑。 Thanks.
谢谢。
I have the following code(edited from Hovercreft's answer): 我有以下代码(从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);
}
}
I tried to use only(not custom interface) the default User32
class. 我试图只使用(不是自定义界面)默认的
User32
类。 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[]
. 例如,变量
windowText
是char[]
,而Hovercraft的变量是byte[]
类型。 Can anyone explain me? 有人能解释一下吗? Thanks.
谢谢。
The latest version of JNA has had some changes that should fix this (as one of the authors of JNA, Luke Quinane, states here ). 最新版本的JNA已经有了一些应该解决的问题(作为JNA的作者之一,Luke Quinane, 在这里指出)。 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.
如果您使用最新版本并检查JNA API ,您将看到WinUser.WNDENUMPROC接口的方法实际上使用WinDef.HWND作为其参数,而不是long或int。
For example: 例如:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.