简体   繁体   中英

Handle external windows using java

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...

You can use the Windows API to get a handle to the window and then resize it:

EnumWindows:
http://msdn.microsoft.com/en-us/library/ms633497%28v=VS.85%29.aspx

GetNextWindow:
http://msdn.microsoft.com/en-us/library/ms633509%28v=VS.85%29.aspx

GetWindowText:   (to decide whether that is the window you want; if title matches)
http://msdn.microsoft.com/en-us/library/ms633520%28v=VS.85%29.aspx

FindWindow:  (for this you need the exact title beforehand, not just part of it)
http://msdn.microsoft.com/en-us/library/ms633499%28v=vs.85%29.aspx

ShowWindow:
http://msdn.microsoft.com/en-us/library/ms633548%28v=VS.85%29.aspx

Typical WinAPI interface, a little clunky to use but powerful.

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

As stated, this is not possible in pure Java. An example of doing it natively (which you would need to translate to JNI):

HWND hwnd = FindWindow( _T("classname"), _T("windowname") );

// Minimize
ShowWindow( hwnd, SW_MINIMIZE );

// Maximize
ShowWindow( hwnd, SW_MAXIMIZE );

You already have the window name but you should also supply a classname since this allows you to more accurately get the window. If there were multiple windows with the same windowname then your program might do something undesirable.

You can use something like Spy++ to get the class name. Or also simply through code. Ensure that your window is currently the only window with a matching windowname then you can do:

TCHAR lpClassName[256] = {0};

HWND hwnd = FindWindow( _T("classname"), _T("windowname") );
GetClassName( hwnd, lpClassName, _countof( lpClassName ) );
MessageBox( NULL, _T("Class Name"), lpClassName, MB_OK );

If it is possible to get the HWND from the Java process itself with the code here then you should pass that to the native part since it will be guaranteed to be reliable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM