0

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);
firepotato
  • 11
  • 3
  • I viewed this post, but alt+tab just useful for seperate application. https://stackoverflow.com/questions/4782231/using-java-to-set-the-focus-to-a-non-java-application-in-windows – firepotato Sep 16 '17 at 12:25
  • CWDOW maybe work, but it depends on third party exe. – firepotato Sep 16 '17 at 13:02
  • Finally, it works by using OS.SetForegroundWindow(hwnd); When an application embeded in another application, I can't use OS.FindWindow(null, new TCHAR(0, title, true)); to get hwnd. Maybe there is no title now. If I store the hwnd get before, it works. – firepotato Sep 16 '17 at 13:34

1 Answers1

2

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.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22