Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find running applications are in minimized state using java?

Tags:

java

windows

jna

How to find all the running applications in windows desktop are in minimized state using java?

like image 706
Sakthivadivel Anbalagan Avatar asked Oct 08 '22 07:10

Sakthivadivel Anbalagan


1 Answers

You need to first download jna.jar and platform.jar and add them to your classpath. You can figure out the Windows system calls to make by looking at the MSDN documentation.

Here is the code to enumerate over all minimized windows:

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.WINDOWINFO;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;

public class Minimized {
    private static final int MAX_TITLE_LENGTH = 1024;
    private static final int WS_ICONIC = 0x20000000;

    public static void main(String[] args) throws Exception {
        User32.EnumWindows(new WNDENUMPROC() {
            @Override
            public boolean callback(HWND arg0, Pointer arg1) {
                WINDOWINFO info = new WINDOWINFO();
                User32.GetWindowInfo(arg0, info);

                // print out the title of minimized (WS_ICONIC) windows
                if ((info.dwStyle & WS_ICONIC) == WS_ICONIC) {
                    byte[] buffer = new byte[MAX_TITLE_LENGTH];
                    User32.GetWindowTextA(arg0, buffer, buffer.length);
                    String title = Native.toString(buffer);
                    System.out.println("Minimized window = " + title);
                }
                return true;
            }
        }, 0);
    }

    static class User32 {
        static { Native.register("user32"); }
        static native boolean EnumWindows(WNDENUMPROC wndenumproc, int lParam);
        static native void GetWindowTextA(HWND hWnd, byte[] buffer, int buflen);
        static native boolean GetWindowInfo(HWND hWnd, WINDOWINFO lpwndpl);
    }
}
like image 164
Garrett Hall Avatar answered Oct 12 '22 23:10

Garrett Hall