/** * @param processName .exe name * @return window title, if found - else null */ public static String getProcessWindowTitle(String processName) { final String[] res = {null}; List<DesktopWindow> windows = WindowUtils.getAllWindows(true); for (DesktopWindow dw : windows) { if (dw.getFilePath().endsWith(processName)) { String result = dw.getTitle(); if (!result.isEmpty()) { if (!result.contains("MSCTFIME") && !result.contains("Default IME")) { if (res[0] == null || res[0].length() < result.length()) { if(res[0] != null) { log.db("Changing window name from " + res[0]); } res[0] = result; String msg = "Set to " + result; if (!msg.equals(lastMessage)) { log.db(msg); lastMessage = msg; } } } } } } return res[0]; }
/** * @param args * @throws InterruptedException * @throws AWTException */ public static void main(String[] args) throws InterruptedException, AWTException { DesktopWindow windowHolder = User32Utils.findAndShowWindow("Calculator"); if (windowHolder != null) { Rectangle locAndSize = windowHolder.getLocAndSize(); MouseAWTUtils.getInstance().mouseMove(windowHolder.getLocAndSize().x, windowHolder.getLocAndSize().y); Thread.sleep(1000); int fivex= locAndSize.x + (int)locAndSize.width/3; int fivey= locAndSize.y + (int)locAndSize.height*3/4; MouseAWTUtils.getInstance().mouseMove(fivex,fivey); MouseAWTUtils.getInstance().mouseLeftClick(); Thread.sleep(1000); fivex= locAndSize.x + (int)locAndSize.width*8/9; fivey= fivey + 20; Thread.sleep(1000); MouseAWTUtils.getInstance().mouseMove(fivex,fivey); MouseAWTUtils.getInstance().mouseLeftClick(); fivex= locAndSize.x + (int)locAndSize.width/3; fivey= locAndSize.y + (int)locAndSize.height*3/4; Thread.sleep(1000); MouseAWTUtils.getInstance().mouseMove(fivex,fivey); MouseAWTUtils.getInstance().mouseLeftClick(); fivex= locAndSize.x + (int)locAndSize.width*8/9; fivey= fivey + 80; Thread.sleep(1000); MouseAWTUtils.getInstance().mouseMove(fivex,fivey); MouseAWTUtils.getInstance().mouseLeftClick(); } }
public static DesktopWindow findAndShowWindow(String title) { List<DesktopWindow> allWindows = WindowUtils.getAllWindows(true); for (DesktopWindow window : allWindows) { if (window.getTitle().equals(title)) { user32.SetForegroundWindow(window.getHWND()); return window; } } return null; }
/** * Gets a list of currently active processes by creating a snapshot. * * @return List of currently active processes * @throws Win32Exception * If the operation was not successful */ public static ProcessList getProcessList() throws Win32Exception { final ProcessList plist = new ProcessList(); final List<PROCESSENTRY32> list = new LinkedList<>(); final HANDLE hProcessSnap = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new DWORD(0)); PROCESSENTRY32 pe32 = new PROCESSENTRY32(); if (!Kernel32.INSTANCE.Process32First(hProcessSnap, pe32)) { throw new Win32Exception(Native.getLastError()); } do { if (pe32.th32ProcessID.intValue() != 0) { list.add(pe32); } pe32 = new PROCESSENTRY32(); } while (Kernel32.INSTANCE.Process32Next(hProcessSnap, pe32)); for (final PROCESSENTRY32 pe : list) { plist.add(new Process(pe)); } Kernel32.INSTANCE.CloseHandle(hProcessSnap); final List<DesktopWindow> windows = WindowUtils.getAllWindows(false); final IntByReference lpdwProcessId = new IntByReference(); int pid = 0; for (final DesktopWindow window : windows) { User32.INSTANCE.GetWindowThreadProcessId(window.getHWND(), lpdwProcessId); pid = lpdwProcessId.getValue(); plist.add(pid, window.getHWND()); } return plist; }
@Test public void testSoundReducer() throws IOException { TestSubscriber<Map<String,String>> testSubscriber = new TestSubscriber<>(); List<DesktopWindow> allWindows = WindowUtils.getAllWindows(false); allWindows.forEach(window -> { System.out.println(window.getFilePath()); }); }