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
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...
Java has no API for this, so you have to use JNI. See eznme's answer for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With