各位高手你们好!我想请教一个关于如何利用java获取windows应用程序在屏幕的坐标的问题。
1.背景如下:QQ游戏有一个是“大家来找茬”,这个游戏启动后,可以用窗口模式运行,就是以一个windows窗口的形式进行游戏;
2.问题:我想利用java程序,知道这个窗口在屏幕中的位置,或者说想知道这个窗口左上角的屏幕坐标,请问如何能够实现呢?最好给出相关源代码,谢谢了。
1.背景如下:QQ游戏有一个是“大家来找茬”,这个游戏启动后,可以用窗口模式运行,就是以一个windows窗口的形式进行游戏;
2.问题:我想利用java程序,知道这个窗口在屏幕中的位置,或者说想知道这个窗口左上角的屏幕坐标,请问如何能够实现呢?最好给出相关源代码,谢谢了。
import java.awt.*;
public class CenterFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("CenterFrame");
frame.setSize(400, 300); // New since JDK 1.3 to exit the program upon closing
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Get the dimension of the screen
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height; // Locate the upper-left corner (x, y) of the centered frame
int x = (screenWidth - frame.getWidth()) / 2;
int y = (screenHeight - frame.getHeight()) / 2; // Set the location of the frame
frame.setLocation(x, y);
frame.setVisible(true);
}
}
int x=frame.getLocation().x;
int y=frame.getLocation().y;
这样应该可以了。
首先是多谢saibf热心的回复。上述代码的确可以获取窗口的坐标,但这个窗口是java程序生成的JFrame窗口,不是我题目中说的游戏窗口。
同时也多谢 xzqttt 的回复。经过我铺天盖地的baidu,google,我得出的结论和你一样:必须使用JNI才有可能实现我题目的要求。至于获得“大家来找茬”的句柄,我估计利用C++和windowsAPI能实现,因为网上有很多窗口辅助工具,他们都有选择窗口句柄的功能,例如在CSDN下载频道里面能找到的WindowMaster