获取Java中的非Java Windows标题 [英] Get titles of Non-Java Windows in Java
问题描述
Window.getWindows();
没有得到所有打开的窗口,只有java窗口。有没有办法让操作系统打开所有窗口。我正在创建一个java任务栏。
Doesn't get all the open windows, just the java ones. Is there a way to get all the windows the operating system has open. I'm making a java taskbar.
推荐答案
没有使用核心Java的解决方案,但问题可以使用JNI或为了更轻松的时间,JNA。正如评论中所指出的,没有任何解决方案(我知道)将与平台无关。
There is no solution using core Java, but the problem can be solved using JNI or for an easier time, JNA. As noted in the comments, no solution (that I know of) will be platform independent.
例如,此演示程序使用JNA枚举所有带有标题的Windows Windows平台,但也包括不是顶级窗口的窗口,甚至包括不可见的窗口:
For instance, this demo program uses JNA to enumerate all Windows with title in a Windows platform, but will also include windows that are not top-level windows and even windows that aren't visible:
import java.util.ArrayList;
import java.util.List;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;
public class EnumAllWindowNames {
static interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
interface WNDENUMPROC extends StdCallCallback {
boolean callback(Pointer hWnd, Pointer arg);
}
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer userData);
int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);
Pointer GetWindow(Pointer hWnd, int uCmd);
}
public static List<String> getAllWindowNames() {
final List<String> windowNames = new ArrayList<String>();
final User32 user32 = User32.INSTANCE;
user32 .EnumWindows(new User32.WNDENUMPROC() {
@Override
public boolean callback(Pointer hWnd, Pointer arg) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();
if (!wText.isEmpty()) {
windowNames.add(wText);
}
return true;
}
}, null);
return windowNames;
}
public static void main(String[] args) {
List<String> winNameList = getAllWindowNames();
for (String winName : winNameList) {
System.out.println(winName);
}
}
}
这篇关于获取Java中的非Java Windows标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!