Как установить фокус на приложение, отличное от Java, при встраивании в приложение Java в Windows

Я использую код для создания приложения C#, встроенного в приложение Java. Но я не могу сосредоточиться на приложении С#.

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);

person firepotato    schedule 16.09.2017    source источник
comment
Я просмотрел этот пост, но Alt+Tab просто полезен для отдельного приложения. stackoverflow.com/questions/4782231/   -  person firepotato    schedule 16.09.2017
comment
CWDOW может работать, но это зависит от стороннего исполняемого файла.   -  person firepotato    schedule 16.09.2017
comment
Наконец, он работает с использованием OS.SetForegroundWindow(hwnd); Когда приложение встроено в другое приложение, я не могу использовать OS.FindWindow(null, new TCHAR(0, title, true)); чтобы получить hwnd. Может быть, сейчас нет названия. Если я сохраняю hwnd get раньше, он работает.   -  person firepotato    schedule 16.09.2017


Ответы (1)


Чтобы установить фокус на приложение Windows, я использую следующий метод:

Необходимый импорт:

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

Метод:

/**
 * 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);
    }
}

Этот метод также расширит окно приложения, если оно было свернуто, на панель задач, а также установит на нем фокус, если, конечно, необязательному параметру windowState, который сообщает методу, что нужно сфокусироваться на приложении, но в свернутом состоянии. Если приложение не свернуто, то оно сворачивает его.

person DevilsHnd    schedule 16.09.2017