如何在 Java 中获取程序窗口的 x 和 y? [英] How to get the x and y of a program window in Java?

查看:24
本文介绍了如何在 Java 中获取程序窗口的 x 和 y?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让我在 java 中获取窗口的 X 和 Y 值?我读到我必须使用运行时,因为 java 不能直接混乱,但是我不太确定如何做到这一点.任何人都可以向我指出一些有关如何获取此内容的链接/提示吗?

Is there a way for me to get the X and Y values of a window in java? I read that I'll have to use runtime, since java can't mess directly, however I am not so sure of how to do this. Can anyone point me some links/tips on how to get this?

推荐答案

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

To get the x and y position of "any other unrelated application" you're going to have to query the OS and that means likely using either JNI, JNA or some other scripting utility such as AutoIt (if Windows). I recommend either JNA or the scripting utility since both are much easier to use than JNI (in my limited experience), but to use them you'll need to download some code and integrate it with your Java application.

编辑 1我不是 JNA 专家,但我确实摆弄了一些,这就是我获得某些命名窗口的窗口坐标的方法:

EDIT 1 I'm no JNA expert, but I do fiddle around with it some, and this is what I got to get the window coordinates for some named window:

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 的构建路径中.

Of course for this to work, the JNA libraries would need to be downloaded and placed on the Java classpath or in your IDE's build path.

这篇关于如何在 Java 中获取程序窗口的 x 和 y?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆