亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

呼喚遠方 2019-09-03 19:18:31
有沒有辦法讓我在java中獲取窗口的X和Y值?我讀到我將不得不使用運行時,因為java不能直接混亂,但是我不太清楚如何做到這一點。誰能指出一些如何獲得這個的鏈接/提示?
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

要獲得“任何其他無關應用程序”的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的構建路徑中。


查看完整回答
反對 回復 2019-09-03
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

在最終用戶的幫助下,這很容易做到。讓他們點擊屏幕截圖中的一個點。


例如

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 . . .


查看完整回答
反對 回復 2019-09-03
  • 3 回答
  • 0 關注
  • 956 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號