Force third window to the front from Java

828 views Asked by At

I am developing a Java application, and I want to take and save an screenshot from a concrete window which I dont have access to the code. To do so, I have the following function:

    public void nextStep(){
        final MyUser32 user32 = MyUser32.INSTANCE;
        user32.EnumWindows(new MyUser32.WNDENUMPROC() {

            public boolean callback(HWND hwnd, Pointer userData) {
                byte[] windowText = new byte[512];
                user32.GetWindowTextA(hwnd, windowText, 512);
                String wText = Native.toString(windowText);

            if(wText.contains(MyString)){
                System.out.println("Window hdwn: " + hwnd + " - Text: " + wText);
                User32.INSTANCE.ShowWindow(hwnd, WinUser.SW_MAXIMIZE);
                User32.INSTANCE.SetForegroundWindow(hwnd);
               //I ve tried with setFocus as well and does not work either
                capture();
            }
            return true;
        }
    }, null);
}

The capture() method is the following:

public void capture(){
    Robot robot;
    try {
        robot = new Robot();
    } catch (AWTException e){
        throw new IllegalArgumentException("No robot");
    }
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_UP);
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_ALT);
    try {
        capt = (BufferedImage) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.imageFlavor);
        ImageIO.write(capt, "png", new File("captura.png"));
    } catch (HeadlessException | UnsupportedFlavorException | IOException e) {
        e.printStackTrace();
    }
}

And MyUser32 class is the following:

public interface MyUser32 extends StdCallLibrary {
    MyUser32 INSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class);
    interface WNDENUMPROC extends StdCallCallback {
        boolean callback(HWND hwnd, Pointer arg);
    }
    boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
    int GetWindowTextA(HWND hwnd, byte[] lpString, int nMaxCount);
}

If the variable MyString has a value, lets say, "paint" what this part of the code would do is:

-List all the active windows

-For each one containing the string "paint" in the title:

*Print its information, like the title and the hwnd

*Bring it Maximized to the front

*Set it as the Foreground Window (again, yes)

*Apply the capture() method, which does:

First presses alt+up, which maximizes the focused window, and then presses alt+Capt. Screen, which captures only the focused window and saves it in the clipboard. Finally, it saves the capture as "captura.png"

It works well for the most of the windows, like paint or google chrome, but it doesnt work well for the window I am interested in capturing. Sometimes it brings it to the front and sometimes it doesnt, without any apparent reason, but it never gets maximized or captured, even though alt+up and alt+captScreen work perfectly fine from my physical keyboard, and the information of the window gets printed well always.

Does someone know how can I force that third window to get the focus? I guess the problem is there, but I might be wrong. Also, I dont have any other window running that is keeping the focus forcedly.

Thanks a lot!

0

There are 0 answers