How to get opened applications list in windows(taskmanager ->application) using java code?

后端 未结 4 2073
执笔经年
执笔经年 2020-12-19 17:14

Here is the snapshot what I want exately!

\"Windows

I am trying to develop a pr

4条回答
  •  -上瘾入骨i
    2020-12-19 17:42

    Here is the solution to get titles of ALL (visible, non-visible) windows: https://stackoverflow.com/a/11067492/6401177

    If you want to get titles of opened top-level windows only (i.e. Applications taskbar), you have to check the visibility of each window (and/or check other conditions as listed here: http://vb.mvps.org/articles/ap200003.asp). Although, checking window's visibility seems sufficient.

    I just altered method "callback" in previous code like this:

    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);
            }
    

    I also added "file.encoding" so titles are shown correctly in non-english Windows environment too. I tested code in Windows XP/7/8 and it works nice. The only problem seems to be that some default internal(?) window called "Program Manager" is always included in the list.

    You need both JARs (JNA libraries) from: https://github.com/java-native-access/jna

提交回复
热议问题