Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle external windows using java

Tags:

java

winapi

I need to check if an external window (another java program, but not controlled by the program that I'm working on) is open using the title, and if it open, then either maximize or minimize it based on the user command in Java (I know only the title of the window and nothing else) . Google only says that I can use winapi to get the window handle and manipulate it using the handle, but I'm not able to find how to do this.

I could find references on how to do it using JNI here: In Java Swing how do you get a Win32 window handle (hwnd) reference to a window?. Is it possible to do this without using JNI?

Could someone help me understand how to do this.

Thanks and Regards

like image 252
Balanivash Avatar asked Jul 12 '11 10:07

Balanivash


2 Answers

I've just added a lot of win32 related window functions into JNA. You can see the details here.

// Find and minimize a window:
WinDef.HWND hWnd = User32.INSTANCE.FindWindow("className", "windowName");
User32.INSTANCE.ShowWindow(hWnd, WinUser.SW_MINIMIZE);

You can also enumerate all windows:

final WinDef.HWND[] windowHandle = new WinDef.HWND[1];
User32.INSTANCE.EnumWindows(new WinUser.WNDENUMPROC() {
    @Override
    public boolean callback(WinDef.HWND hwnd, Pointer pointer) {
        if (matches(hwnd)) {
            windowHandle[0] = hwnd;
            return false;
        }
        return true;
    }
}, Pointer.NULL);

// Minimize or maximize windowHandle[0] here...
like image 148
Luke Quinane Avatar answered Sep 23 '22 18:09

Luke Quinane


Java has no API for this, so you have to use JNI. See eznme's answer for details.

like image 29
Aaron Digulla Avatar answered Sep 22 '22 18:09

Aaron Digulla