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);
3
  • I viewed this post, but alt+tab just useful for seperate application. stackoverflow.com/questions/4782231/… Commented Sep 16, 2017 at 12:25
  • CWDOW maybe work, but it depends on third party exe. Commented Sep 16, 2017 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. Commented Sep 16, 2017 at 13:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.