如何用java切换程序窗口?

类似alt + tab, 但是不是用快捷键实现,可以切换指定的程序(如:qq, 浏览器...)。


回答:

设置当前某窗口为当前窗口,有几个步骤要做:
1.得到窗口句柄FindWindow
2.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
3.最后SetForegroundWindow

public class TryWithHWND {

public static void main(String args[]) {

setFocusToWindowsApp("微信", 0);

}

public static void setFocusToWindowsApp(String applicationTitle, int windowState) {

//int state = User32.SW_SHOWNORMAL; // default window state (Normal)

int state = windowState;

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

if (state != User32.SW_SHOWMINIMIZED) {

user32.ShowWindow(hWnd, User32.SW_SHOWMINIMIZED);

}

user32.ShowWindow(hWnd, state);

user32.SetForegroundWindow(hWnd);

}

}

}

以上是 如何用java切换程序窗口? 的全部内容, 来源链接: utcz.com/p/944752.html

回到顶部