这是我非常想要的快照!

我正在尝试开发一个java程序,可以得到所有打开的任务栏中的应用程序。我尝试了许多链接,但没有一个对我有帮助。Ganesh Rangarajan in July 2013也问了同样的问题,但没有人回答他。这是他的question。
发布于 2016-05-31 01:42:11
下面是获取所有(可见的,不可见的)窗口标题的解决方案:https://stackoverflow.com/a/11067492/6401177
如果您只想获取打开的顶级窗口的标题(即应用程序任务栏),则必须检查每个窗口的可见性(和/或检查此处列出的其他条件:http://vb.mvps.org/articles/ap200003.asp)。不过,检查窗口的可见性似乎就足够了。
我只是在前面的代码中修改了方法"callback“,如下所示:
String wText = Native.toString(windowText, System.getProperty("file.encoding")).trim();
com.sun.jna.platform.win32.WinDef.HWND hwnd_1 = new WinDef.HWND(hWnd);
boolean b = com.sun.jna.platform.win32.User32.INSTANCE.IsWindowVisible(hwnd_1);
if (!wText.isEmpty() && b) {
windowNames.add(wText);
}我还添加了"file.encoding“,以便标题在非英语视窗环境中也能正确显示。我在Windows XP/7/8中测试了代码,它工作得很好。唯一的问题似乎是一些默认的内部(?)名为"Program Manager“的窗口始终包含在列表中。
您需要这两个JAR (JNA库),可从:https://github.com/java-native-access/jna获得
发布于 2016-05-14 20:28:29
这个回复对你来说太晚了,但可能会帮助正在面临类似问题的其他人。
我只是写了一个类似的应用程序来做这件事,但只能打开CMD
并且您可以将listCommand替换为
powershell -command "get-Process | format-table mainwindowtitle"(注意在java中使用它)来获取所有打开的应用程序。
public String[] getEnginesFromTaskManger()
{
// listCommand is a command to get all opened CMD program like (batches,.......)
// you can test it in your CMD as powershell -command "get-Process cmd | format-table mainwindowtitle"
String listCommand = "powershell -command \"get-Process cmd | format-table mainwindowtitle\"";
try
{
String line;
// since line length for powershell output is 79
int outLen = 79;
Process p = Runtime.getRuntime().exec(listCommand);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
line = input.readLine();
System.out.println("line: " + line + "\t" + line.length());
EnginesListFromTaskManeger = new ArrayList<String>();
int i = 0;
/*
I used this outLen > 0 condition to make sure that this method will close automatically
in case of no running CMD applications and you running this from your IDE's (Eclipse, Netbeans , ......)
the powershell will not stopped so i used it. */
while(line != null && outLen > 0)
{
System.out.println("line: " + line + "\t" + line.length());
line = input.readLine().trim().toLowerCase();
outLen = line.length();
EnginesListFromTaskManeger.add(i, line);
System.out.println(EnginesListFromTaskManeger.get(i));
// EnginesListFromTaskManeger[i]=(String)input.readLine().trim();
// System.out.println("EnginesListFromTaskManeger"+ EnginesListFromTaskManeger[i]);
i++;
}
input.close();
}catch(Exception err)
{
err.printStackTrace();
}
ListFromTaskManeger = new String[EnginesListFromTaskManeger.size()];
ListFromTaskManeger = EnginesListFromTaskManeger.toArray(ListFromTaskManeger);
return ListFromTaskManeger;
}发布于 2016-03-23 12:14:04
"ps -e“命令的进程列表:
try {
String line;
Process p = Runtime.getRuntime().exec("ps -e");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}在windows中,请参见下面使用的代码
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");希望这些信息能帮上忙!
https://stackoverflow.com/questions/35956816
复制相似问题