How to set the focus to a non Java application in embeded in Java application on Windows

861 views Asked by At

I use the code to make a C# application embeded in Java application. But I can't set the focus on C# application.

hwnd = OS.FindWindow(null, new TCHAR(0, title, true));

int oldStyle = OS.GetWindowLong(hwnd, OS.GWL_STYLE);
OS.SetWindowLong(hwnd, OS.GWL_STYLE, oldStyle & ~OS.WS_BORDER);

OS.SetParent(hwnd, composite.handle);
OS.SendMessage(hwnd, OS.WM_SYSCOMMAND, OS.SC_MAXIMIZE, 0);

OS.SetForegroundWindow(PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell().handle);
1

There are 1 answers

0
DevilsHnd - 退した On

To set focus to a Windows application I use the following method:

Required Imports:

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

The method:

/**
 * Sets focus to a running windows application Windows Application. If the 
 * handle of the desired application is not found then nothing happens.<pre>
 * 
 * <b>Example Usage 1:</b>
 * 
 *      setFocusToWindowsApp("Untitled - Notepad"); 
 * 
 * <b>Example Usage 2:</b>
 * 
 *      setFocusToWindowsApp("Untitled - Notepad", 1);</pre>
 * 
 * @param applicationTitle (String) The title contained upon the application's 
 * Window Title Bar.<br>
 * 
 * @param windowState (Optional - Integer - Default is 0) By default when the
 * desired application window is set to show to the forefront on screen it is 
 * displayed in its Normal window state (not maximized or minimized). If a value
 * of 1 is supplied then the windows is brought to the forefront in its Maximized
 * state. If 2 is supplied then the window is set to is Minimized state.<pre>
 * 
 *      0   Normal (default)
 *      1   Maximized
 *      2   Minimized</pre><br>
 * 
 * A value supplied that is less that 0 or greater than 2 is automatically changed 
 * to 0 (Normal State).
 */
public void setFocusToWindowsApp(String applicationTitle, int... windowState) {
    int state = User32.SW_SHOWNORMAL; //default window state (Normal)
    if (windowState.length > 0) {
        state = windowState[0];
        switch(state) {
            default:
            case 0:
                state = User32.SW_SHOWNORMAL;
                break;
            case 1:
                state = User32.SW_SHOWMAXIMIZED;
                break;
            case 2:
                state = User32.SW_SHOWMINIMIZED;
                break;
        }
    }

    User32 user32 = User32.INSTANCE;  
    WinDef.HWND hWnd = user32.FindWindow(null, applicationTitle);
    if (user32.IsWindowVisible(hWnd)) { 
        user32.ShowWindow(hWnd, state); //.SW_SHOW); 
        user32.SetForegroundWindow(hWnd);  
        user32.SetFocus(hWnd);
    }
}

This method will also expand the application window should it have been minimized onto the task bar as well as set focus upon it unless of course the integer value of 2 is supplied to the optional windowState parameter which tells the method to gain focus on the application but in its minimized state. If the application is not minimized then it minimizes it.