public static void main(String[] args) { final User32 user32 = User32.INSTANCE; user32.EnumWindows(new WinUser.WNDENUMPROC() { int count = 0; @Override public boolean callback(WinDef.HWND hwnd, Pointer pointer) { byte[] windowText = new byte[512]; byte[] className = new byte[512]; user32.GetWindowTextA(hwnd, windowText, 512); user32.GetClassNameA(hwnd, className, 512); String title = Native.toString(windowText); String classN = Native.toString(className); // get rid of this if block if you want all windows regardless of whether // or not they have text if (title.isEmpty()) { return true; } System.out.println("Title: " + title + " Class name: " + classN); return true; } },null); }
@Override public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) { Logger.getLogger(SSHAgent.class.getName()).log(Level.FINE, "Windows requests operation: {0}", uMsg); switch (uMsg) { case MY_WM_COPYDATA: { return processMessage(hwnd, wParam, lParam); } case WinUser.WM_CREATE: { return new LRESULT(0); } case WinUser.WM_DESTROY: { libU.PostQuitMessage(0); return new LRESULT(0); } default: return libU.DefWindowProc(hwnd, uMsg, wParam, lParam); } }
public void pressKeys(final List<KeyboardKey> keys) { if (keys.isEmpty()) { return; } final WinUser.INPUT input = new WinUser.INPUT(); final WinUser.INPUT[] inputs = (WinUser.INPUT[]) input.toArray(keys.size() * 2); final ListIterator<KeyboardKey> iterator = keys.listIterator(); int index = 0; while (iterator.hasNext()) { setKeyDown(inputs[index], iterator.next()); index++; } while (iterator.hasPrevious()) { setKeyUp(inputs[index], iterator.previous()); index++; } user32.SendInput(new WinDef.DWORD(inputs.length), inputs, inputs[0].size()); }
public static Map<String, WindowInfo> EnumerateWin32ChildWindows(HWND parentHwnd) { final Map<String, WindowInfo> infoList = new LinkedHashMap<String, WindowInfo>(); class ChildWindowCallback implements WinUser.WNDENUMPROC { @Override public boolean callback(HWND hWnd, Pointer lParam) { WindowInfo wi = new WindowInfo(hWnd, true); infoList.put(wi.hwndStr, wi); return true; } } Api.User32Ex.instance.EnumChildWindows(parentHwnd, new ChildWindowCallback(), new Pointer(0)); return infoList; }
public static HWND FindMainWindowFromPid(final long targetProcessId) { final List<HWND> resultList = new ArrayList<HWND>(); class ParentWindowCallback implements WinUser.WNDENUMPROC { @Override public boolean callback(HWND hWnd, Pointer lParam) { PointerByReference pointer = new PointerByReference(); User32Ex.instance.GetWindowThreadProcessId(hWnd, pointer); long pid = pointer.getPointer().getInt(0); if (pid == targetProcessId) if (resultList.isEmpty()) resultList.add(hWnd); return true; } } Api.User32Ex.instance.EnumWindows(new ParentWindowCallback(), 0); if (!resultList.isEmpty()) return resultList.get(0); return null; }
@Test public void enumerateParentWindowsTest() { final Map<String, WindowInfo> infoList = new LinkedHashMap<String, WindowInfo>(); class ParentWindowCallback implements WinUser.WNDENUMPROC { @Override public boolean callback(HWND hWnd, Pointer lParam) { output("Getting window info " + Api.GetHandleAsString(hWnd)); //WindowInfo wi = new WindowInfo(hWnd, false); infoList.put(getWindow32Information(hWnd, false), null); //Api.User32.instance.EnumChildWindows(hWnd, new ChildWindowCallback(), new Pointer(0)); return true; } } Api.User32Ex.instance.EnumWindows(new ParentWindowCallback(), 0); output("enumerateParentWindowsTest size: " + infoList.size()); assertTrue(infoList.size() > 0); }
/** * @param args (ignored) */ public static void main(String[] args) { System.out.println("Installed Physical Monitors: " + User32.INSTANCE.GetSystemMetrics(WinUser.SM_CMONITORS)); User32.INSTANCE.EnumDisplayMonitors(null, null, new MONITORENUMPROC() { @Override public int apply(HMONITOR hMonitor, HDC hdc, RECT rect, LPARAM lparam) { enumerate(hMonitor); return 1; } }, new LPARAM(0)); }
private static void createWindow(final File f) { SwingUtilities.invokeLater(() -> { if (!f.isDirectory()) // if the file was changed or deleted before this could execute, exit return; final BDWindow w = new BDWindow(f); windows.add(w); w.pack(); w.setVisible(true); threadPool.execute(() -> { // sets whether the windows blur the background behind them Settings.INSTANCE.blurBackground.addListener(enabled -> { ch.njol.betterdesktop.win32.User32.enableBlur(w, enabled); }); // disables the windows from being hidden when "peeking" the desktop Settings.INSTANCE.excludeFromPeek.addListener(enabled -> { Dwmapi.setExcludedFromPeek(w, enabled); }); // prevents "show desktop" button from hiding the windows if (Settings.INSTANCE.showOnDesktop.get()) { final HWND progman = User32.INSTANCE.FindWindow("Progman", "Program Manager"); User32.INSTANCE.SetWindowLongPtr(new HWND(Native.getComponentPointer(w)), WinUser.GWL_HWNDPARENT, progman.getPointer()); // the following is required so that this setting actually does something - no idea why though try { Thread.sleep(100); } catch (final InterruptedException e1) {} w.setAlwaysOnTop(true); w.setAlwaysOnTop(false); } }); }); }
/** * Quits the given process. * * @param handle The handle to quit. */ public static void quitProcess(final WinDef.HWND handle) { ensureWinApiInstances(); user32.PostMessage(handle, WinUser.WM_QUIT, null, null); }
/** * Closes the given window. * * @param handle The handle of the window to close. */ public static void closeWindow(final WinDef.HWND handle) { ensureWinApiInstances(); user32.PostMessage(handle, WinUser.WM_CLOSE, null, null); }
private void setTransparent(Component w) { this.componentHwnd = getHWnd(w); this.settingWl = User32.INSTANCE.GetWindowLong(componentHwnd, WinUser.GWL_EXSTYLE); int transparentWl = User32.INSTANCE.GetWindowLong(componentHwnd, WinUser.GWL_EXSTYLE) | WinUser.WS_EX_LAYERED | WinUser.WS_EX_TRANSPARENT; User32.INSTANCE.SetWindowLong(componentHwnd, WinUser.GWL_EXSTYLE, transparentWl); }
private static void setTransparent(Component w) { WinDef.HWND hwnd = getHWnd(w); int wl = User32.INSTANCE.GetWindowLong(hwnd, WinUser.GWL_EXSTYLE); // wl = wl | WinUser.WS_EX_LAYERED | WinUser.WS_EX_TRANSPARENT; wl = wl | WinUser.WS_EX_LAYERED | WinUser.WS_EX_TRANSPARENT; User32.INSTANCE.SetWindowLong(hwnd, WinUser.GWL_EXSTYLE, wl); }
/** * Start watching... */ public void setMouseHook() { thrd = new Thread(new Runnable() { @Override public void run() { try { if (!isHooked) { hhk = USER32INST.SetWindowsHookEx(14, mouseHook, KERNEL32INST.GetModuleHandle(null), 0); isHooked = true; WinUser.MSG msg = new WinUser.MSG(); while ((USER32INST.GetMessage(msg, null, 0, 0)) != 0) { USER32INST.TranslateMessage(msg); USER32INST.DispatchMessage(msg); //System.out.print(isHooked); if (!isHooked) { break; } } } else { } } catch (Exception e) { System.err.println(e.getMessage()); System.err.println("Caught exception in MouseHook!"); } } }, "Named thread"); threadFinish = false; thrd.start(); }
public void setDisplaySleep(final boolean sleep) { user32.DefWindowProc( getForegroundWindow().get(), WinUser.WM_SYSCOMMAND, new WinDef.WPARAM(SC_MONITORPOWER), new WinDef.LPARAM(sleep ? 2 : -1) ); }
public void run(final String command, final String parameters, final String workingDirectory) throws SystemException { final WinDef.INT_PTR result = shell32.ShellExecute( null, "open", Objects.requireNonNull(command), Objects.requireNonNull(parameters), workingDirectory, WinUser.SW_SHOWNORMAL ); if (result.intValue() <= 32) { throw new SystemException("Could not run command: " + command + " " + parameters); } }
public void open(final String uri) throws SystemException { final WinDef.INT_PTR result = shell32.ShellExecute( null, "open", Objects.requireNonNull(uri), null, null, WinUser.SW_SHOWNORMAL ); if (result.intValue() <= 32) { throw new SystemException("Cannot open URI: " + uri); } }
public void focus(HWND mainWindowHandle) { if (PlatformSpecific.isOnWindows()) { // Restores browser window if it is minimized / maximized user32.ShowWindow(browserWindowHandle, WinUser.SW_SHOWNOACTIVATE); // SWP_NOMOVE and SWP_NOSIZE prevents the 0,0,0,0 parameters from taking effect. logger.info("Bringing bView to front"); boolean success = user32.SetWindowPos(browserWindowHandle, mainWindowHandle, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); if (!success) { logger.info("Failed to bring bView to front."); logger.info(Kernel32.INSTANCE.GetLastError()); } user32.SetForegroundWindow(mainWindowHandle); } }
@Test public void setOnTop() { // run notepad int pid = runNotepad(); HWND hWnd = User32.INSTANCE.FindWindow(NOTEPAAD_CLASS_NAME, NOTEPAD_TITLE); Assert.assertNotNull(hWnd); assertEquals(0, User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE) & User32.WS_EX_TOPMOST); // set on top Assert.assertTrue(Win.setOnTop(NOTEPAD_TITLE)); Assert.assertNotEquals(0, User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE) & User32.WS_EX_TOPMOST); // remove on top Assert.assertTrue(Win.setOnTop(NOTEPAD_TITLE, false)); assertEquals(0, User32.INSTANCE.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE) & User32.WS_EX_TOPMOST); // close notepad Process.close(pid); sleep(500); Assert.assertFalse(Process.exists(pid)); Assert.assertFalse(Win.setOnTop(NOTEPAD_TITLE)); }
static void enumerate(HMONITOR hMonitor) { System.out.println("Found HMONITOR: " + hMonitor.getPointer().toString()); MONITORINFOEX info = new MONITORINFOEX(); User32.INSTANCE.GetMonitorInfo(hMonitor, info); System.out.println("Screen " + info.rcMonitor); System.out.println("Work area " + info.rcWork); boolean isPrimary = (info.dwFlags & WinUser.MONITORINFOF_PRIMARY) != 0; System.out.println("Primary? " + (isPrimary ? "yes" : "no")); System.out.println("Device " + new String(info.szDevice)); DWORDByReference pdwNumberOfPhysicalMonitors = new DWORDByReference(); Dxva2.INSTANCE.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors); int monitorCount = pdwNumberOfPhysicalMonitors.getValue().intValue(); System.out.println("HMONITOR is linked to " + monitorCount + " physical monitors"); PHYSICAL_MONITOR[] physMons = new PHYSICAL_MONITOR[monitorCount]; Dxva2.INSTANCE.GetPhysicalMonitorsFromHMONITOR(hMonitor, monitorCount, physMons); for (int i = 0; i < monitorCount; i++) { HANDLE hPhysicalMonitor = physMons[0].hPhysicalMonitor; System.out.println("Monitor " + i + " - " + new String(physMons[i].szPhysicalMonitorDescription)); enumeratePhysicalMonitor(hPhysicalMonitor); } }
@Override public void setIcon (FileHandle iconCacheFolder, FileHandle icoFile, FileHandle pngFile) { //WinAPI can't read icon from JAR, needs copying to some other location FileHandle cachedIco = iconCacheFolder.child(icoFile.name()); if (cachedIco.exists() == false) icoFile.copyTo(cachedIco); try { HANDLE hImage = User32.INSTANCE.LoadImage(Kernel32.INSTANCE.GetModuleHandle(""), cachedIco.path(), WinUser.IMAGE_ICON, 0, 0, WinUser.LR_LOADFROMFILE); User32.INSTANCE.SendMessageW(User32.INSTANCE.GetActiveWindow(), User32.WM_SETICON, new WPARAM(User32.BIG_ICON), hImage); } catch (Exception e) { throw new IllegalStateException(e); } }
public void enableSettings() { User32.INSTANCE.SetWindowLong(componentHwnd, WinUser.GWL_EXSTYLE, settingWl); }
public void closeWindow(final WinDef.HWND window) { user32.SendMessage(window, WinUser.WM_CLOSE, null, null); }
private void setKeyDown(final WinUser.INPUT input, final KeyboardKey key) { input.type.setValue(WinUser.INPUT.INPUT_KEYBOARD); input.input.setType(WinUser.KEYBDINPUT.class); input.input.ki.wVk = key.getVirtualKeyCode(); }
private void setKeyUp(final WinUser.INPUT input, final KeyboardKey key) { input.type.setValue(WinUser.INPUT.INPUT_KEYBOARD); input.input.setType(WinUser.KEYBDINPUT.class); input.input.ki.dwFlags.setValue(WinUser.KEYBDINPUT.KEYEVENTF_KEYUP); input.input.ki.wVk = key.getVirtualKeyCode(); }
public boolean showWindow(HWND handle) { return user32.ShowWindow(handle, WinUser.SW_SHOW); }
public boolean hideWindow(HWND handle) { return user32.ShowWindow(handle, WinUser.SW_HIDE); }
public boolean minimizeWindow(HWND handle) { return user32.ShowWindow(handle, WinUser.SW_MINIMIZE); }
public boolean maximizeWindow(HWND handle) { return user32.ShowWindow(handle, WinUser.SW_MAXIMIZE); }
public boolean restoreWindow(HWND handle) { return user32.ShowWindow(handle, WinUser.SW_RESTORE); }
private void bringToTop() { if (PlatformSpecific.isOnWindows()) { user32.ShowWindow(browserWindowHandle, WinUser.SW_RESTORE); user32.SetForegroundWindow(browserWindowHandle); } }
public void minimizeWindow() { if (PlatformSpecific.isOnWindows()) { user32.ShowWindow(browserWindowHandle, WinUser.SW_MINIMIZE); } }
public void initKeyHook() { thread = new Thread(new Runnable() { @Override public void run() { final User32 lib = User32.INSTANCE; HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null); keyboardHook = new LowLevelKeyboardProc() { public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) { if (nCode >= 0) { switch (wParam.intValue()) { // case WinUser.WM_KEYUP: case WinUser.WM_KEYDOWN: // case WinUser.WM_SYSKEYUP: case WinUser.WM_SYSKEYDOWN: // do active userActive(); } } return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer()); } }; hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0); // This bit never returns from GetMessage int result; MSG msg = new MSG(); while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) { if (result == -1) { System.err.println("error in get message"); break; } else { System.err.println("got message"); lib.TranslateMessage(msg); lib.DispatchMessage(msg); } } lib.UnhookWindowsHookEx(hhk); } }); thread.start(); }
int SetWindowLong(HWND hWnd, int nIndex, WinUser.WindowProc callback);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer ptr);