【问题标题】:How to switch between runnng windows applications using java?如何使用 java 在运行的 windows 应用程序之间切换?
【发布时间】:2014-08-30 12:15:29
【问题描述】:

假设我在 Eclipse 中运行我的程序,它会切换到 mozilla 窗口(它同时运行)。同样,当我们单击任务栏中的图标时。我尝试过 Robot 类来刺激点击,但这是将坐标硬编码到程序中,我不想这样做。

任何建议我如何做到这一点。谢谢。

【问题讨论】:

  • 我已经使用 JNA 或通过将我的 Java 程序与 Windows 实用程序编程语言(如 AutoIt)链接来完成此操作。

标签: java automation operating-system


【解决方案1】:

据我了解,您不能仅使用核心 Java 按名称切换到另一个正在运行的窗口。您可以通过机器人发送 alt-tab 击键来交换窗口,但这不会显示命名窗口。为此,如果这是 Windows 问题,我建议使用 JNI、JNA 或某些特定于操作系统的实用程序编程语言,例如 AutoIt。

例如,使用 JNA,您可以执行以下操作:

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;

public class SetForgroundWindowUtil {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

      interface WNDENUMPROC extends StdCallCallback {
         boolean callback(Pointer hWnd, Pointer arg);
      }

      boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);

      int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);

      int SetForegroundWindow(Pointer hWnd);

      Pointer GetForegroundWindow();
   }

   public static boolean setForegroundWindowByName(final String windowName,
         final boolean starting) {
      final User32 user32 = User32.INSTANCE;
      return user32.EnumWindows(new User32.WNDENUMPROC() {

         @Override
         public boolean callback(Pointer hWnd, Pointer arg) {
            byte[] windowText = new byte[512];
            user32.GetWindowTextA(hWnd, windowText, 512);
            String wText = Native.toString(windowText);
            // if (wText.contains(WINDOW_TEXT_TO_FIND)) {
            if (starting) {
               if (wText.startsWith(windowName)) {
                  user32.SetForegroundWindow(hWnd);
                  return false;
               }
            } else {
               if (wText.contains(windowName)) {
                  user32.SetForegroundWindow(hWnd);
                  return false;
               }
            }
            return true;
         }
      }, null);
   }

   public static void main(String[] args) {
      boolean result = setForegroundWindowByName("Untitled", true);
      System.out.println("result: " + result);
   }
}

我不知道任何与操作系统无关的解决此问题的方法。

【讨论】:

  • 谢谢,是的,你是对的,我不能使用 Alt+Tab(因为 Tab 顺序未知)。我想我必须研究一下 JNA。
【解决方案2】:

我相信您的问题可能已在此处得到解答:Active other process's window in Java

除此之外,JNA 将是您执行此操作的最佳选择,核心 java 或 java 通常不允许 直接与操作系统交互,但 JNA 可以。

我能想到的唯一其他方法是调用应用程序,例如带有类似参数的命令(如果需要的话)的 chrome

try{
    Desktop.getDesktop().open(new File("Location\\to\\the\\program.exe"));
} catch(IOException ex){
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

编辑:

使用这个方法来调用它的参数

Process process = new ProcessBuilder("Location\\to\\the\\program.exe",
          "param1","param2").start();

【讨论】:

    【解决方案3】:

    在 linux 上,试试这个(这是 kotlin 代码):

    val window = "The window name you want to show"
    Runtime.getRuntime().exec(arrayOf("/bin/bash", "-c", "wmctrl -a \"$window\""))
    

    在小学 (Loki) 上工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 2016-05-17
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多