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

有没有办法让我在java中获取窗口的X和Y值?我读到我将不得不使用运行时,因为java不能直接混乱,但是我不太清楚如何做到这一点。谁能指出一些如何获得这个的链接/提示?



呼唤远方
浏览 930回答 3
3回答

繁星淼淼

要获得“任何其他无关应用程序”的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的构建路径中。

12345678_0001

在最终用户的帮助下,这很容易做到。让他们点击屏幕截图中的一个点。例如import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;/** Getting a point of interest on the screen.Requires the MotivatedEndUser API - sold separately. */class GetScreenPoint {    public static void main(String[] args) throws Exception {        Robot robot = new Robot();        final Dimension screenSize = Toolkit.getDefaultToolkit().            getScreenSize();        final BufferedImage screen = robot.createScreenCapture(            new Rectangle(screenSize));        SwingUtilities.invokeLater(new Runnable() {            public void run() {                JLabel screenLabel = new JLabel(new ImageIcon(screen));                JScrollPane screenScroll = new JScrollPane(screenLabel);                screenScroll.setPreferredSize(new Dimension(                    (int)(screenSize.getWidth()/2),                    (int)(screenSize.getHeight()/2)));                final Point pointOfInterest = new Point();                JPanel panel = new JPanel(new BorderLayout());                panel.add(screenScroll, BorderLayout.CENTER);                final JLabel pointLabel = new JLabel(                    "Click on any point in the screen shot!");                panel.add(pointLabel, BorderLayout.SOUTH);                screenLabel.addMouseListener(new MouseAdapter() {                    public void mouseClicked(MouseEvent me) {                        pointOfInterest.setLocation(me.getPoint());                        pointLabel.setText(                            "Point: " +                            pointOfInterest.getX() +                            "x" +                            pointOfInterest.getY());                    }                });                JOptionPane.showMessageDialog(null, panel);                System.out.println("Point of interest: " + pointOfInterest);            }        });    }}典型输出Point of interest: java.awt.Point[x=342,y=43]Press any key to continue . . .
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java