자바로 현제 focusing 되어있는 창의 크기 가져오기
조회수 975회
-
(•́ ✖ •̀)
알 수 없는 사용자
2 답변
-
java만으로는 어렵습니다. 운영체제가 윈도우라면 윈도우의 api 를 이용해야 합니다. 문제는 윈도우 api는 c/c++로 되어 있기 때문에 java(정확히는 jdk)만으로는 어렵고 jni를 이용해서 api를 호출해야 합니다.
바로 jni를 이용해도 되지만 편리하게 jna 라이브러리를 이용할 수 있습니다.
윈도우 핸들을 얻기 위해서는 FindWindow 함수를 사용하면 됩니다. https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
윈도우의 크기및 위치를 얻을때 아래의 함수를 이용합니다.
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
-
아래는 샘플입니다.
- build.gradle
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' }
- Main.java
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(); } } }
댓글 입력