windows 我可以将另一个程序的窗口移到焦点的前面吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2694365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I move another program's window to the front of focus?
提问by TwentyMiles
I am interacting with a third party application using it's API, and would like to move it to the front of focus (so that it is on top of all other open windows) when a user performs a certain action. While I can move my application up and down in the hierarchy pretty easily, there doesn't appear to be a way to interact with other windows. Is it possible to move another program's window to front with Java?
我正在使用它的 API 与第三方应用程序进行交互,并希望在用户执行某个操作时将其移动到焦点的前面(以便它位于所有其他打开的窗口之上)。虽然我可以很容易地在层次结构中上下移动我的应用程序,但似乎没有办法与其他窗口交互。是否可以使用 Java 将另一个程序的窗口移到前面?
采纳答案by Jeff Storey
You can't do it in pure Java code, but you could using JNI. See In Java Swing how do you get a Win32 window handle (hwnd) reference to a window?for how to get a handle to the window. Then you could do something like http://msdn.microsoft.com/en-us/library/ms633545to move it to the front.
你不能用纯 Java 代码来做,但你可以使用 JNI。请参阅在 Java Swing 中如何获得对窗口的 Win32 窗口句柄 (hwnd) 引用?如何获得窗口的句柄。然后你可以做一些类似http://msdn.microsoft.com/en-us/library/ms633545 的事情来把它移到前面。
Caveat: This is for windows only
警告:这仅适用于 Windows
回答by SyntaxT3rr0r
It's not possible in pure Java and on certain OSes it's not even possible to cleanly get the other windows position (for example good luck doing that under OS X 10.4: OS X 10.4 does nothave any documented mean to registered for other windows' events... There are "hacks", but they're really hackish, like requiring the user to turn on the "Allow Assistive Device"preferences, which requires the OS X admin passord).
这是不可能的纯Java和某些操作系统它甚至可以清晰地得到其他窗口位置(例如好运这样做下OS X 10.4:OS X 10.4也没有任何记录的平均值,以登记其他窗口事件。 .. 有“黑客”,但它们真的很黑客,比如要求用户打开“允许辅助设备”首选项,这需要 OS X 管理员密码)。
回答by Christian Hujer
I was looking for a solution to a very similar problem.
Until now I have found one - very fragile!- solution.
It works for my case, because my case involves running entire sessions using java.awt.Robot
without user interaction, so it may - or may not - work for your case.
我正在寻找一个非常相似的问题的解决方案。直到现在我发现了一个——非常脆弱!- 解决方案。它适用于我的案例,因为我的案例涉及在java.awt.Robot
没有用户交互的情况下运行整个会话,因此它可能 - 也可能不 - 适用于您的情况。
The solution is using java.awt.Robot
to send the key strokes like Alt+Tab
to bring the desired Window to the front.
This is fragile of course, due to multiple reasons:
解决方案是使用java.awt.Robot
发送击键,例如Alt+Tab
将所需的窗口置于最前面。由于多种原因,这当然很脆弱:
- The Robot cannot know how often
Alt+Tab
needs to be send to get the desired window to front. The window is one in many. This solution only works if it's already known which window in terms ofAlt-Tab
-count it is. - Depending on the OS, the required keystrokes might actually be something else.
- 机器人不知道
Alt+Tab
需要多久发送一次才能将所需的窗口放在前面。窗户是众多窗户之一。此解决方案仅在已经知道就Alt-Tab
-count而言是哪个窗口时才有效。 - 根据操作系统的不同,所需的击键实际上可能是别的东西。
In case the OS and the window sequence are known, i.e. the program can know upfront how many Alt-Tab
keystrokes would be required, this could sometimes be a solution.
如果操作系统和窗口序列是已知的,即程序可以预先知道需要多少次Alt-Tab
击键,这有时可能是一个解决方案。
The following Java program demonstrates how to do this. If called without arguments, it generates exactly one Alt+Tab
keystroke. If called with arguments, the program assumes the first argument is a number and it will generate as many Alt+Tab
keystrokes as specified by that number.
以下 Java 程序演示了如何执行此操作。如果不带参数调用,它只生成一次Alt+Tab
击键。如果使用参数调用,程序假定第一个参数是一个数字,它将生成与Alt+Tab
该数字指定的一样多的击键次数。
You may want to play around a bit with the timing values given in robot.delay()
and robot.setAutoDelay()
in order to deliver the best experience on your machine. Hint: at least on Linux, robot.setAutoDelay()
should certainly be less than the keyboard repeat delay, otherwise the OS would generate multiple Alt-Tab
keystrokes in the system's event queue because of key repetition.
您可能想要稍微调整一下robot.delay()
和robot.setAutoDelay()
中给出的时序值,以便在您的机器上提供最佳体验。提示:至少在Linux上,robot.setAutoDelay()
一定要小于键盘重复延迟,否则操作系统会Alt-Tab
因为按键重复而在系统的事件队列中产生多次击键。
import java.awt.AWTException;
import java.awt.Robot;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_TAB;
import static java.lang.Integer.parseInt;
public class WindowSwitcher {
public static void main(final String... args) throws AWTException {
final int repeat = args.length != 0 ? parseInt(args[0]) : 1;
final Robot robot = createRobot();
robot.keyPress(VK_ALT);
for (int i = 0; i < repeat; i++) {
robot.keyPress(VK_TAB);
robot.keyRelease(VK_TAB);
robot.delay(500);
}
robot.keyRelease(VK_ALT);
}
public static Robot createRobot() throws AWTException {
final Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(10);
return robot;
}
}