如何在Java中获得程序窗口的x和y?

如何在Java中获得程序窗口的x和y?,java,runtime,coordinates,jna,awtrobot,Java,Runtime,Coordinates,Jna,Awtrobot,我有没有办法用java获取窗口的X和Y值?我读到我将不得不使用运行时,因为java不能直接弄乱,但是我不太确定如何做到这一点。有人能告诉我一些链接/技巧吗?要获得“任何其他无关应用程序”的x和y位置,您必须查询操作系统,这意味着可能需要使用JNI、JNA或其他脚本工具,如AutoIt(如果是Windows)。我推荐JNA或脚本实用程序,因为两者都比JNI容易使用(以我有限的经验),但要使用它们,您需要下载一些代码并将其与Java应用程序集成 编辑1 我不是JNA专家,但我确实对它进行了一些处理,

我有没有办法用java获取窗口的X和Y值?我读到我将不得不使用运行时,因为java不能直接弄乱,但是我不太确定如何做到这一点。有人能告诉我一些链接/技巧吗?

要获得“任何其他无关应用程序”的x和y位置,您必须查询操作系统,这意味着可能需要使用JNI、JNA或其他脚本工具,如AutoIt(如果是Windows)。我推荐JNA或脚本实用程序,因为两者都比JNI容易使用(以我有限的经验),但要使用它们,您需要下载一些代码并将其与Java应用程序集成

编辑1 我不是JNA专家,但我确实对它进行了一些处理,这就是我得到的一些命名窗口的窗口坐标:

import java.util.Arrays;
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.*;

public class GetWindowRect {

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

      HWND FindWindow(String lpClassName, String lpWindowName);

      int GetWindowRect(HWND handle, int[] rect);
   }

   public static int[] getRect(String windowName) throws WindowNotFoundException,
            GetWindowRectException {
      HWND hwnd = User32.INSTANCE.FindWindow(null, windowName);
      if (hwnd == null) {
         throw new WindowNotFoundException("", windowName);
      }

      int[] rect = {0, 0, 0, 0};
      int result = User32.INSTANCE.GetWindowRect(hwnd, rect);
      if (result == 0) {
         throw new GetWindowRectException(windowName);
      }
      return rect;
   }

   @SuppressWarnings("serial")
   public static class WindowNotFoundException extends Exception {
      public WindowNotFoundException(String className, String windowName) {
         super(String.format("Window null for className: %s; windowName: %s", 
                  className, windowName));
      }
   }

   @SuppressWarnings("serial")
   public static class GetWindowRectException extends Exception {
      public GetWindowRectException(String windowName) {
         super("Window Rect not found for " + windowName);
      }
   }

   public static void main(String[] args) {
      String windowName = "Document - WordPad";
      int[] rect;
      try {
         rect = GetWindowRect.getRect(windowName);
         System.out.printf("The corner locations for the window \"%s\" are %s", 
                  windowName, Arrays.toString(rect));
      } catch (GetWindowRect.WindowNotFoundException e) {
         e.printStackTrace();
      } catch (GetWindowRect.GetWindowRectException e) {
         e.printStackTrace();
      }      
   }
}

当然,要使其工作,需要下载JNA库并将其放置在Java类路径或IDE的构建路径中。

在最终用户的帮助下,这很容易做到。只需让他们点击屏幕截图中的一个点即可

例如。 典型输出
在这里参加聚会有点晚,但这会给其他人节省一点时间。如果您使用的是较新版本的JNA,那么
WindowUtils.getAllWindows()
将使这项工作更容易完成

我在以下maven站点使用了截至本文的最新稳定版本:

JNA平台-net.java.dev.JNA:JNA平台:5.2.0

jnacore-net.java.dev.JNA:JNA:5.2.0

Java 8 Lambda(编辑:rect是一个占位符,需要是final或实际上是final才能在Lambda中工作)

其他Java

//Find IntelliJ IDEA Window
Rectangle rect = null;
for (DesktopWindow desktopWindow : WindowUtils.getAllWindows(true)) {
    if (desktopWindow.getTitle().contains("IDEA")) {
         rect = desktopWindow.getLocAndSize();
    }
}
然后,在JPanel中,您可以绘制一个捕获的图像以适合(如果不同的纵横比,将拉伸图像)


窗口的屏幕位置?哪个窗口?当前Java(Swing?)应用程序(在这种情况下,getLocationOnScreen()可以工作)或另一个无关应用程序的窗口?如果是后者,那么我会考虑使用JNA来解决这个问题。关于任何其他不相关的应用程序。在Ubuntu或任何其他Linux桌面上如何?User32已经是jna平台库的一部分(至少在v5.3.1中)。如果我执行上述操作,我只会得到“此平台尚不受支持”,因为代码就是这样做的:
Point of interest: java.awt.Point[x=342,y=43]
Press any key to continue . . .
//Find IntelliJ IDEA Window
//import java.awt.Rectangle;
final Rectangle rect = new Rectangle(0, 0, 0, 0); //needs to be final or effectively final for lambda
WindowUtils.getAllWindows(true).forEach(desktopWindow -> {
    if (desktopWindow.getTitle().contains("IDEA")) {
        rect.setRect(desktopWindow.getLocAndSize());
    }
});
//Find IntelliJ IDEA Window
Rectangle rect = null;
for (DesktopWindow desktopWindow : WindowUtils.getAllWindows(true)) {
    if (desktopWindow.getTitle().contains("IDEA")) {
         rect = desktopWindow.getLocAndSize();
    }
}
//import java.awt.Robot;
g2d.drawImage(new Robot().createScreenCapture(rect), 0, 0, getWidth(), getHeight(), this);