JNA user32.ShowWindow 与 java.util.Scanner 不工作
JNA user32.ShowWindow with java.util.Scanner doesnt work
你好~我正在使用 JNA,我想让另一个程序在屏幕上显示焦点,但它不起作用。
这是我的代码。
import java.util.Scanner;
导入com.sun.jna.platform.win32.User32;
导入com.sun.jna.platform.win32.WinDef;
public class 主要 {
public static void main(String args[]) {
System.out.println("test");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
sc.close();
System.out.println(number);
setFocusToWindowsApp("점포관리", number);
System.exit(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)) {
user32.ShowWindow(hWnd, state); // .SW_SHOW);
user32.SetForegroundWindow(hWnd);
user32.SetFocus(hWnd);
}
}
}
此代码仅用于测试...没有扫描仪,它工作得很好。
但是使用扫描仪,只有 SW_SHOWMAXIMIZED 有效。否则,程序就不会出现在屏幕上。它只是在图标栏上闪烁。我认为 Scanner 与系统调用或其他任何东西有关,所以它与 user32 混淆。我认为。我尝试了文档上的每个标志。(SW_SHOWNORMAL、SW_SHOWMAXIMIZED 等...)但只有 SW_SHOWMAXIMIZED 有效。我不希望 window 最大化。
如有任何帮助,我们将不胜感激。谢谢!!
正如您正确假设的那样,问题的原因在于 扫描仪。
但问题不在于 showWindow() 函数,而在于 SetForegroundWindow() 函数。
文档说明如下:
An application cannot force a window to the foreground while the user
is working with another window. Instead, Windows flashes the taskbar
button of the window to notify the user.
因此,如果您需要输入,则不能将此屏幕从您的应用程序带到前台。
该解决方案有点麻烦,但只需先将屏幕最小化,然后恢复到普通视图,您就可以将其强制显示在前台。
修改后的方法如下所示
public static void setFocusToWindowsApp(String applicationTitle, int windowState) {
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;
HWND hWnd = user32.FindWindowA(null, applicationTitle);
if (user32.IsWindowVisible(hWnd)) {
if (state != User32.SW_SHOWMINIMIZED) {
user32.ShowWindow(hWnd, User32.SW_SHOWMINIMIZED);
}
user32.ShowWindow(hWnd, state);
user32.SetFocus(hWnd);
}
}
你好~我正在使用 JNA,我想让另一个程序在屏幕上显示焦点,但它不起作用。
这是我的代码。
import java.util.Scanner;
导入com.sun.jna.platform.win32.User32;
导入com.sun.jna.platform.win32.WinDef; public class 主要 {
public static void main(String args[]) {
System.out.println("test");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
sc.close();
System.out.println(number);
setFocusToWindowsApp("점포관리", number);
System.exit(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)) {
user32.ShowWindow(hWnd, state); // .SW_SHOW);
user32.SetForegroundWindow(hWnd);
user32.SetFocus(hWnd);
}
}
}
此代码仅用于测试...没有扫描仪,它工作得很好。 但是使用扫描仪,只有 SW_SHOWMAXIMIZED 有效。否则,程序就不会出现在屏幕上。它只是在图标栏上闪烁。我认为 Scanner 与系统调用或其他任何东西有关,所以它与 user32 混淆。我认为。我尝试了文档上的每个标志。(SW_SHOWNORMAL、SW_SHOWMAXIMIZED 等...)但只有 SW_SHOWMAXIMIZED 有效。我不希望 window 最大化。
如有任何帮助,我们将不胜感激。谢谢!!
正如您正确假设的那样,问题的原因在于 扫描仪。
但问题不在于 showWindow() 函数,而在于 SetForegroundWindow() 函数。
文档说明如下:
An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.
因此,如果您需要输入,则不能将此屏幕从您的应用程序带到前台。
该解决方案有点麻烦,但只需先将屏幕最小化,然后恢复到普通视图,您就可以将其强制显示在前台。
修改后的方法如下所示
public static void setFocusToWindowsApp(String applicationTitle, int windowState) {
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;
HWND hWnd = user32.FindWindowA(null, applicationTitle);
if (user32.IsWindowVisible(hWnd)) {
if (state != User32.SW_SHOWMINIMIZED) {
user32.ShowWindow(hWnd, User32.SW_SHOWMINIMIZED);
}
user32.ShowWindow(hWnd, state);
user32.SetFocus(hWnd);
}
}