public void startMainLoop() { mainLoopStarted = true; MSG msg = new MSG(); while (libU.GetMessage(msg, null, 0, 0) == 1) { libU.TranslateMessage(msg); libU.DispatchMessage(msg); } }
@Override public void run() { HWND hWnd = getHWnd(w); User32 user = User32.INSTANCE; int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE); byte level = (byte)((int)(255 * alpha) & 0xFF); if (usingUpdateLayeredWindow(w)) { // If already using UpdateLayeredWindow, continue to // do so BLENDFUNCTION blend = new BLENDFUNCTION(); blend.SourceConstantAlpha = level; blend.AlphaFormat = WinUser.AC_SRC_ALPHA; user.UpdateLayeredWindow(hWnd, null, null, null, null, null, 0, blend, WinUser.ULW_ALPHA); } else if (alpha == 1f) { flags &= ~WinUser.WS_EX_LAYERED; user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags); } else { flags |= WinUser.WS_EX_LAYERED; user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags); user.SetLayeredWindowAttributes(hWnd, 0, level, WinUser.LWA_ALPHA); } setForceHeavyweightPopups(w, alpha != 1f); storeAlpha(w, level); } });
return !user32.EnumWindows(new User32.WNDENUMPROC() { @Override public boolean callback(Pointer hWnd, Pointer userData) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText).trim(); user32.EnumWindows(new User32.WNDENUMPROC() { user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText).trim(); if (!wText.isEmpty()) { return user32.IsWindow(hWnd); user32.EnumWindows(new User32.WNDENUMPROC() { @Override public boolean callback(Pointer hWnd, Pointer userData) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText).trim(); return user32.SetForegroundWindow(hWnd) != 0; return user32.GetForegroundWindow(); byte[] lpString = new byte[nMaxCount]; int getWindowTextResult = user32 .GetWindowTextA(hWnd, lpString, nMaxCount);
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); }
return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer()); hhk = lib.SetWindowsHookEx(13, keyboardHook, hMod, 0); while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) { if (result == -1) { break; } else { lib.TranslateMessage(msg); lib.DispatchMessage(msg); lib.UnhookWindowsHookEx(hhk);
public WinLockListener() { // define new window class final WString windowClass = new WString("MyWindowClass"); final WinDef.HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle(""); WinUser.WNDCLASSEX wClass = new WinUser.WNDCLASSEX(); wClass.hInstance = hInst; wClass.lpfnWndProc = WinLockListener.this; wClass.lpszClassName = windowClass.toString(); // register window class User32.INSTANCE.RegisterClassEx(wClass); getLastError(); // create new window final WinDef.HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass.toString(), "'hidden helper window to catch Windows events", 0, 0, 0, 0, 0, null, // WM_DEVICECHANGE contradicts parent=WinUser.HWND_MESSAGE null, hInst, null); getLastError(); Log.debug("window sucessfully created! window hwnd: " + hWnd.getPointer().toString()); Wtsapi32.INSTANCE.WTSRegisterSessionNotification(hWnd, Wtsapi32.NOTIFY_FOR_THIS_SESSION); WinUser.MSG msg = new WinUser.MSG(); while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) != 0) { User32.INSTANCE.TranslateMessage(msg); User32.INSTANCE.DispatchMessage(msg); } /// This code is to clean at the end. You can attach it to your custom application shutdown listener Wtsapi32.INSTANCE.WTSUnRegisterSessionNotification(hWnd); User32.INSTANCE.UnregisterClass(windowClass.toString(), hInst); User32.INSTANCE.DestroyWindow(hWnd); Log.debug("program exit!"); }
User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0); javaThreadId = Thread.currentThread().getId(); nativeThreadId = Kernel32.INSTANCE.GetCurrentThreadId(); while ((getMessageReturn = User32.INSTANCE.GetMessage(msg, null, 0, 0)) != 0) { if (getMessageReturn != -1) { User32.INSTANCE.TranslateMessage(msg); User32.INSTANCE.DispatchMessage(msg); } else {
@Override public synchronized void hook() { KeyboardHook keyboardHook = new KeyboardHook(createKeyboardEventLogger()); AtomicReference<User32.HHOOK> hhookRef = new AtomicReference<>(); Thread hookKeyboardEventsThread = new Thread(() -> { this.keyboardHookThreadID = Kernel32.INSTANCE.GetCurrentThreadId(); WinDef.HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null); User32.HHOOK hhook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, keyboardHook, hInst, 0); boolean keyboardEventsHooked = hhook != null; if (!keyboardEventsHooked) { LOG.error("Failed to hook keyboard events"); return; } hhookRef.set(hhook); LOG.info("Keyboard events hooked"); User32.INSTANCE.GetMessage(new WinUser.MSG(), new WinDef.HWND(Pointer.NULL), 0, 0); User32.INSTANCE.UnhookWindowsHookEx(hhook); this.keyboardHookThreadID = 0; LOG.info("Keyboard events unhooked"); }); hookKeyboardEventsThread.setName("KeyboardHookThread"); hookKeyboardEventsThread.start(); awaitUtils.waitFor(() -> hhookRef.get() != null, 2000L, () -> LOG.error("Failed to hook keyboard events in time")); }
HWND hwnd = user32.GetForegroundWindow(); user32.GetWindowText(hwnd, windowText, 512); int oldStyle = user32.GetWindowLong(hwnd, GWL_STYLE); int newStyle = oldStyle & ~0x00400000; //bitwise not WS_DLGFRAME means remove the style newStyle = newStyle & ~0x00040000;//WS_THICKFRAME user32.SetWindowLong(hwnd, GWL_STYLE, newStyle);
@Override public void run() { create(); synchronized (lock) { lock.notifyAll(); } MSG msg = new MSG(); while (User32.INSTANCE.GetMessage(msg, null, 0, 0) > 0) { User32.INSTANCE.DispatchMessage(msg); } destory(); }
final boolean hasMessage = CanonCamera.lib.PeekMessage( msg, null, 0, 0, 1 ); // peek and remove if ( hasMessage ) { CanonCamera.lib.TranslateMessage( msg ); CanonCamera.lib.DispatchMessage( msg );
private void gameToFront() { User32.INSTANCE.EnumWindows((hWnd, arg1) -> { char[] className = new char[512]; User32.INSTANCE.GetClassName(hWnd, className, 512); String wText = Native.toString(className); if (wText.isEmpty()) { return true; } if (wText.equals("POEWindowClass")) { User32.INSTANCE.SetForegroundWindow(hWnd); return false; } return true; }, null); }
boolean checkWindowClass = (windowClass != null); final int length = user32.GetWindowTextLength(hWnd) + 1; if (length == 1) { return true; user32.GetWindowText(hWnd, windowText, length); final String wText = Native.toString(windowText); if (checkWindowClass || logger.isLoggable(Level.FINE)) { final char[] classText = new char[255]; User32.INSTANCE.GetClassName(hWnd, classText, 255); wClass = Native.toString(classText);
/** * Sets transparency of the window. * @param alpha 0..255 alpha attribute. * @throws Win32Exception WIN32 call has failed. * @throws AutomationException Something is wrong in automation. */ public void setTransparency(int alpha) throws Win32Exception, AutomationException { WinDef.HWND hwnd = this.getNativeWindowHandle(); if (user32.SetWindowLong( hwnd, User32.GWL_EXSTYLE, User32.WS_EX_LAYERED) == 0) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } if (!user32.SetLayeredWindowAttributes( hwnd, 0, (byte)alpha, User32.LWA_ALPHA)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } } }
User32 user32 = User32.INSTANCE; char path[] = new char[512]; long sleepTime = 2000; try { Thread.sleep(sleepTime); } catch (InterruptedException e) {} HWND hWnd = user32.GetForegroundWindow(); user32.GetWindowModuleFileName(hWnd, path, 512); System.out.println("Foreground Window Module FileName: " + Native.toString(path)); user32.GetWindowText(hWnd, path, 512); System.out.println("Window text is: " + Native.toString(path));
protected Optional<CurrentWindow> getCurrentWindow(HWND hwnd) { int windowTextMaxLength = 512; char[] titleText = new char[windowTextMaxLength]; user32.GetWindowText(hwnd, titleText, windowTextMaxLength); String title = Native.toString(titleText).trim(); user32.GetWindowThreadProcessId(hwnd, pidPointer); int pid = pidPointer.getValue();
@Override public String getWindowTitle(final HWND hwnd) { final int requiredLength = User32.INSTANCE .GetWindowTextLength(hwnd) + 1; final char[] title = new char[requiredLength]; final int length = User32.INSTANCE.GetWindowText(hwnd, title, title.length); return Native.toString(Arrays.copyOfRange(title, 0, length)); }
public WindowsPowerXP() { if (!Kernel32Ex.INSTANCE.SetProcessShutdownParameters(0x03FF, 0)) throw new GetLastErrorException(); mp.start(); if (!Kernel32Ex.INSTANCE.SetConsoleCtrlHandler(hr, true)) throw new GetLastErrorException(); final HWND hwnd = new HWND(); f.pack(); hwnd.setPointer(Native.getComponentPointer(f)); int wID = User32.INSTANCE.GetWindowThreadProcessId(hwnd, null); hHook = User32.INSTANCE.SetWindowsHookEx(WH_CALLWNDPROC, hp, null, wID); if (hHook == null) throw new GetLastErrorException(); }
public void enableSettings() { User32.INSTANCE.SetWindowLong(componentHwnd, WinUser.GWL_EXSTYLE, settingWl); }
public static final void destroyWindow(final HWND hWnd) { if (!User32.INSTANCE.DestroyWindow(hWnd)) throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } }