/** * Brings a selected window to the front. If the window is a hidden or non-visible * nothing will happen. This function checks the style of the window to ensure that * non-visible windows remain hidden. * @param selectedWindow */ private void bringWindowToFront(HWND selectedWindow) { User32 user32 = User32.INSTANCE; User32Ext myuser32 = User32Ext.INSTANCE; boolean result; WINDOWINFO winfo = new WINDOWINFO(); result = user32.GetWindowInfo(selectedWindow, winfo); // If the windows are not titled then we don't even want to TRY to bring // them to the front. There are more windows than just what you can see // in every application. if (result && ((winfo.dwStyle & WinUserExt.WS_TILEDWINDOW) == WinUserExt.WS_TILEDWINDOW)) { if ((winfo.dwStyle & WinUserExt.WS_MINIMIZE) == WinUserExt.WS_MINIMIZE) { result = myuser32.ShowWindow(selectedWindow, WinUserExt.SW_RESTORE); processResult(result, "Show Window"); } result = myuser32.BringWindowToTop(selectedWindow); processResult(result, "Bring to top"); } }
/** * Convenience method for getting all the handles from a java.util.Process * @param process - the process we want the windows from. * @return */ private static List<HWND> getWindowHandlesFromProcess(Process process) { final User32 user32 = User32.INSTANCE; Collection<HWND> windows = getWindowProcessMap().get(getProcessId(process)); if (windows == null) { System.out.println("There was a problem with the window getting"); windows = new ArrayList<HWND>(); } windows = Collections2.filter(windows, new Predicate<HWND>(){ @Override public boolean apply(HWND window) { boolean result; WINDOWINFO winfo = new WINDOWINFO(); result = user32.GetWindowInfo(window, winfo); // If the windows are not titled then we don't even want to TRY to bring // them to the front. There are more windows than just what you can see // in every application. return (result && ((winfo.dwStyle & WinUserExt.WS_TILEDWINDOW) == WinUserExt.WS_TILEDWINDOW)); } }); return new ArrayList<HWND>(windows); }
public static WindowHolder findAndShowWindow(String className, String windowsName) { HWND hWnd = user32.FindWindow(className, windowsName); if (hWnd == null) { System.out.println("there is no running calc app"); return null; } user32.ShowWindow(hWnd, User32.SW_SHOW); WINDOWINFO pwi = new WINDOWINFO(); WindowHolder windowHolder = new WindowHolder(hWnd, pwi); user32.SetForegroundWindow(hWnd); return windowHolder; }
public static WindowHolder findChildWindowByText(WindowHolder parenWindow, String text) { WinDef.DWORD fiveButton = new WinDef.DWORD(Long.parseLong(text)); WinDef.HWND fiveButtonWnd = user32.GetWindow(parenWindow.getParentWindow(), fiveButton); if (fiveButtonWnd == null) { return null; } WINDOWINFO pwi = new WINDOWINFO(); System.out.println(user32.GetWindowInfo(fiveButtonWnd, pwi)); WindowHolder windowHolder = new WindowHolder(fiveButtonWnd, pwi); return windowHolder; }
public WindowHolder(HWND parentWindow, WINDOWINFO info) { super(); this.parentWindow = parentWindow; this.info = info; this.rect = info.rcWindow; this.clientRect = info.rcClient; }
private boolean isActiveWindow(HWND wnd) { WINDOWINFO pwi = new WINDOWINFO(); pwi.size(); if (MyUser32.INSTANCE.GetWindowInfo(wnd, pwi)) { pwi.read(); return pwi.dwWindowStatus == 1; } return false; }
public static boolean isWindowMinimized(int hwndpt){ HWND hwnd = new HWND(new Pointer(hwndpt)); WINDOWINFO info = new WINDOWINFO(); User32.INSTANCE.GetWindowInfo(hwnd, info); // print out the title of minimized (WS_ICONIC) windows if ((info.dwStyle & com.sun.jna.platform.win32.WinUser.WS_MINIMIZE ) == com.sun.jna.platform.win32.WinUser.WS_MINIMIZE ) { return true; } return false; }
/** * Gets the first window visible in the given process. It will try this every retryPeriod milliseconds until * timeoutMillis milliseconds has elapsed. * @param process - The process we care about getting windows from * @param timeoutMillis - How long to try getting these windows * @param retryPeriod - How long to wait before trying again. * @return - the first window visible in the application or null if no windows have been found. */ private static HWND getFirstVisibleWindow(Process process, long timeoutMillis, long retryPeriod) { Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINE, "Getting the first visible window"); User32 user32 = User32.INSTANCE; long endTime = System.currentTimeMillis() + timeoutMillis; HWND visibleWindow = null; boolean success = false; Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINER, "Waiting for the window to become visible."); Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINEST, "{1} < {2} endTime; VISABL {3}", new Object[]{System.currentTimeMillis(), endTime, visibleWindow}); while (System.currentTimeMillis() < endTime && visibleWindow == null) { List<HWND> windows = getWindowHandlesFromProcess(process); for (HWND window : windows) { Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINE, "Getting window info."); WINDOWINFO winfo = new WINDOWINFO(); success = user32.GetWindowInfo(window, winfo); if (success) { if ((winfo.dwWindowStatus & WinUserExt.WS_ACTIVECAPTION) == WinUserExt.WS_ACTIVECAPTION) { Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINE, "Window is active"); } if ((winfo.dwStyle & WinUserExt.WS_VISIBLE) == WinUserExt.WS_VISIBLE) { Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINE, "Window is visible"); visibleWindow = window; } } } try { Thread.sleep(retryPeriod); } catch (InterruptedException ex) { Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.SEVERE, null, ex); } Logger.getLogger(Win32NativeProcessWrapper.class.getName()).log(Level.FINE, "{1} < {2} endTime; VISABL {3}", new Object[]{System.currentTimeMillis(), endTime, visibleWindow}); } return visibleWindow; }
public WINDOWINFO getInfo() { return info; }
boolean GetWindowInfo(HWND hwnd, WINDOWINFO pwi);
boolean GetWindowInfo( HWND hWnd, WINDOWINFO info);