9

Java でウィンドウの X 値と Y 値を取得する方法はありますか? Javaは直接いじることができないため、ランタイムを使用する必要があると読みましたが、これを行う方法がよくわかりません。これを取得する方法について、誰かが私にいくつかのリンク/ヒントを教えてもらえますか?

4

3 に答える 3

20

「他の無関係なアプリケーション」の x と y の位置を取得するには、OS にクエリを実行する必要があります。つまり、JNI、JNA、または AutoIt (Windows の場合) などの他のスクリプト ユーティリティを使用する可能性があります。JNA またはスクリプト ユーティリティのいずれかをお勧めします。どちらも JNI よりもはるかに使いやすいためです (私の限られた経験では) が、それらを使用するには、いくつかのコードをダウンロードして Java アプリケーションに統合する必要があります。

EDIT 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 のビルド パスに配置する必要があります。

于 2011-05-22T23:51:06.177 に答える
7

これは、エンドユーザーの助けを借りて簡単に行うことができます。スクリーンショットのポイントをクリックしてもらうだけです。

例えば

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 . . .
于 2011-05-23T03:03:24.873 に答える