I'd like to use Java to get the size of the currently running game window, so how can I get it?
java
It's not possible with java alone. If the operating system is Windows, you should use Windows' API. The problem is that the window api is c/c++, so it is difficult only with java (to be exact jdk), and you have to call the api using jni.
You can use jni right away, but you can use jna library conveniently.
To obtain a window handle, Use the FindWindow function. https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
Use the functions below to obtain the size and location of the window.
GetWindowRect https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms633519(v=vs.85).aspx
GetClientRect https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms633503(v=vs.85).aspx
Below is a sample.
plugins {
id 'java'
id 'application'
}
mainClassName = 'yhjung.Main'
group 'yhjung.com'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// // https://mvnrepository.com/artifact/net.java.dev.jna/jna
compile group: 'net.java.dev.jna', name: 'jna', version: '4.5.2'
// // https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform
compile group: 'net.java.dev.jna', name: 'jna-platform', version: '4.5.2'
}
package yhjung;
import java.util.Arrays;
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.*;
public class Main {
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 = "Cmder";
int[] rect;
try {
rect = Main.getRect(windowName);
System.out.printf("The corner locations for the window \"%s\" are %s",
windowName, Arrays.toString(rect));
} } catch (Main.WindowNotFoundException e) {
e.printStackTrace();
} } catch (Main.GetWindowRectException e) {
e.printStackTrace();
}
}
}
© 2024 OneMinuteCode. All rights reserved.