|
|
@@ -0,0 +1,232 @@
|
|
|
+package user32util;
|
|
|
+
|
|
|
+import com.sun.jna.Native;
|
|
|
+import com.sun.jna.Pointer;
|
|
|
+import com.sun.jna.platform.win32.BaseTSD;
|
|
|
+import com.sun.jna.platform.win32.User32;
|
|
|
+import com.sun.jna.platform.win32.WinDef;
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * static methods to allow Java to call Windows code. user32.dll code is as
|
|
|
+ * specified in the JNA interface User32.java
|
|
|
+ */
|
|
|
+
|
|
|
+public class User32Util {
|
|
|
+ private static final User32Instance USER_32_INSTANCE = User32Instance.INSTANCE;
|
|
|
+ private static Pointer callBackHwnd;
|
|
|
+
|
|
|
+ public static boolean windowExists(final String startOfWindowName) {
|
|
|
+ return !USER_32_INSTANCE.EnumWindows(new User32Instance.WNDENUMPROC() {
|
|
|
+ @Override
|
|
|
+ public boolean callback(Pointer hWnd, Pointer userData) {
|
|
|
+ byte[] windowText = new byte[512];
|
|
|
+ USER_32_INSTANCE.GetWindowTextA(hWnd, windowText, 512);
|
|
|
+ String wText = Native.toString(windowText).trim();
|
|
|
+
|
|
|
+ if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> getAllWindowNames() {
|
|
|
+ final List<String> windowNames = new ArrayList<String>();
|
|
|
+ USER_32_INSTANCE.EnumWindows(new User32Instance.WNDENUMPROC() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean callback(Pointer hWnd, Pointer arg) {
|
|
|
+ byte[] windowText = new byte[512];
|
|
|
+ USER_32_INSTANCE.GetWindowTextA(hWnd, windowText, 512);
|
|
|
+ String wText = Native.toString(windowText).trim();
|
|
|
+ if (!wText.isEmpty()) {
|
|
|
+ windowNames.add(wText);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }, null);
|
|
|
+
|
|
|
+ return windowNames;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean windowExists(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.IsWindow(hWnd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getWinHwnd(final String startOfWindowName) {
|
|
|
+ callBackHwnd = null;
|
|
|
+
|
|
|
+ USER_32_INSTANCE.EnumWindows(new User32Instance.WNDENUMPROC() {
|
|
|
+ @Override
|
|
|
+ public boolean callback(Pointer hWnd, Pointer userData) {
|
|
|
+ byte[] windowText = new byte[512];
|
|
|
+ USER_32_INSTANCE.GetWindowTextA(hWnd, windowText, 512);
|
|
|
+ String wText = Native.toString(windowText).trim();
|
|
|
+
|
|
|
+ if (!wText.isEmpty() && wText.startsWith(startOfWindowName)) {
|
|
|
+ callBackHwnd = hWnd;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }, null);
|
|
|
+ return callBackHwnd;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean setForegroundWindow(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.SetForegroundWindow(hWnd) != 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getForegroundWindow() {
|
|
|
+ return USER_32_INSTANCE.GetForegroundWindow();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getForegroundWindowText() {
|
|
|
+ Pointer hWnd = getForegroundWindow();
|
|
|
+ int nMaxCount = 512;
|
|
|
+ byte[] lpString = new byte[nMaxCount];
|
|
|
+ int getWindowTextResult = USER_32_INSTANCE
|
|
|
+ .GetWindowTextA(hWnd, lpString, nMaxCount);
|
|
|
+ if (getWindowTextResult == 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ return Native.toString(lpString);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isForegroundWindow(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.GetForegroundWindow().equals(hWnd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean setForegroundWindow(String startOfWindowName) {
|
|
|
+ Pointer hWnd = getWinHwnd(startOfWindowName);
|
|
|
+ return USER_32_INSTANCE.SetForegroundWindow(hWnd) != 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Rectangle getClientRect(WinDef.HWND hWnd) throws User32UtilException {
|
|
|
+ if (hWnd == null) {
|
|
|
+ throw new User32UtilException(
|
|
|
+ "Failed to getWindowRect since Pointer hWnd is null");
|
|
|
+ }
|
|
|
+ Rectangle result = null;
|
|
|
+ WinDef.RECT rect = new WinDef.RECT();
|
|
|
+ boolean rectOK = USER_32_INSTANCE.GetClientRect(hWnd, rect);
|
|
|
+ if (rectOK) {
|
|
|
+ result = rect.toRectangle();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Rectangle getWindowRect(Pointer hWnd) throws User32UtilException {
|
|
|
+ if (hWnd == null) throw new User32UtilException("Failed to getWindowRect since Pointer hWnd is null");
|
|
|
+ WinDef.RECT rect = new WinDef.RECT();
|
|
|
+ if (USER_32_INSTANCE.GetWindowRect(hWnd, rect)) return rect.toRectangle();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * set window at x and y position with w and h width. Set on top of z-order
|
|
|
+ *
|
|
|
+ * @param hWnd
|
|
|
+ * @param x
|
|
|
+ * @param y
|
|
|
+ * @param w
|
|
|
+ * @param h
|
|
|
+ * @return boolean -- did it work?
|
|
|
+ */
|
|
|
+ public static boolean setWindowPos(Pointer hWnd, int x, int y, int w, int h) {
|
|
|
+ int uFlags = 0;
|
|
|
+ return USER_32_INSTANCE.SetWindowPos(hWnd, User32Instance.HWND_TOP, x, y, w, h, uFlags);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean moveWindow(Pointer hWnd, int x, int y, int nWidth,
|
|
|
+ int nHeight) {
|
|
|
+ boolean bRepaint = true;
|
|
|
+ return USER_32_INSTANCE.MoveWindow(hWnd, x, y, nWidth, nHeight, bRepaint);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Rectangle getClientRect(String startOfWindowName) throws User32UtilException {
|
|
|
+ WinDef.HWND hWnd = User32.INSTANCE.FindWindow(null, startOfWindowName);
|
|
|
+ WinDef.POINT getPos = new WinDef.POINT();
|
|
|
+ WinDef.RECT rect = new WinDef.RECT();
|
|
|
+ User32Instance.INSTANCE.GetClientRect(hWnd, rect);
|
|
|
+ User32Ex.instance.ClientToScreen(hWnd, getPos);
|
|
|
+
|
|
|
+ return new Rectangle(getPos.x, getPos.y, rect.right, rect.bottom);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Rectangle getWindowRect(String startOfWindowName)
|
|
|
+ throws User32UtilException {
|
|
|
+ Pointer hWnd = getWinHwnd(startOfWindowName);
|
|
|
+ if (hWnd != null) {
|
|
|
+ return getWindowRect(hWnd);
|
|
|
+ } else {
|
|
|
+ throw new User32UtilException("Failed to getWindowRect for \""
|
|
|
+ + startOfWindowName + "\"");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getWindow(Pointer hWnd, int uCmd) {
|
|
|
+ return USER_32_INSTANCE.GetWindow(hWnd, uCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getWindowText(Pointer hWnd) {
|
|
|
+ int nMaxCount = 512;
|
|
|
+ byte[] lpString = new byte[nMaxCount];
|
|
|
+ int result = USER_32_INSTANCE.GetWindowTextA(hWnd, lpString, nMaxCount);
|
|
|
+ if (result == 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return Native.toString(lpString);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getOwnerWindow(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.GetWindow(hWnd, User32Instance.GW_OWNER);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getOwnerWindow(String childTitle) {
|
|
|
+ Pointer hWnd = getWinHwnd(childTitle);
|
|
|
+ Pointer parentHWnd = getOwnerWindow(hWnd);
|
|
|
+ if (parentHWnd == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return getWindowText(parentHWnd);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getNextWindow(Pointer hWnd) {
|
|
|
+ if (hWnd == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return USER_32_INSTANCE.GetWindow(hWnd, User32Instance.GW_HWNDNEXT);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isWindowVisible(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.IsWindowVisible(hWnd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getParent(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.GetParent(hWnd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pointer getRoot(Pointer hWnd) {
|
|
|
+ return USER_32_INSTANCE.GetAncestor(hWnd, User32Instance.GA_ROOT);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BaseTSD.LONG_PTR getWindowLongPtr(Pointer hWndP, int nIndex) {
|
|
|
+ WinDef.HWND hwnd = new WinDef.HWND(hWndP);
|
|
|
+ return USER_32_INSTANCE.GetWindowLongPtr(hwnd, nIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static WinDef.HDC GetWindowDC(WinDef.HWND hWnd) {
|
|
|
+ return USER_32_INSTANCE.GetWindowDC(hWnd);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|