com.sun.jna.platform.win32.WinDef.HWND Java Examples

The following examples show how to use com.sun.jna.platform.win32.WinDef.HWND. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PowerPointHandler.java    From Quelea with GNU General Public License v3.0 8 votes vote down vote up
/**
 * Manually focus the PowerPoint Viewer window. Shift focus to PowerPoint
 * Viewer to move that window to the top or resume playback.
 */
private static String focusPPTView() {
    // Set focus to PowerPoint Viewer
    HWND hwnd = User32.INSTANCE.FindWindow(null,
            title); // window title
    String ret = "";
    if (hwnd == null) {
        ret = "PowerPoint Viewer is not running";
        LOGGER.log(Level.INFO, ret);

    } else {
        User32.INSTANCE.ShowWindow(hwnd, 9); // SW_RESTORE
        User32.INSTANCE.SetForegroundWindow(hwnd); // bring to front
        ret = "Successfully focued PPTView";
    }
    return ret;
}
 
Example #2
Source File: KeyboardLayout.java    From SikuliX1 with MIT License 6 votes vote down vote up
private static Map<Character, int[]> getCurrentLayout() {
  Map<Character, int[]> layout = DEFAULT_KEYBOARD_LAYOUT;

  if (Settings.AutoDetectKeyboardLayout && Settings.isWindows()) {
    int keyboarLayoutId = DEFAULT_KEYBOARD_LAYOUT_ID;
    HWND hwnd = SXUser32.INSTANCE.GetForegroundWindow();
    if (hwnd != null) {
      int threadID = SXUser32.INSTANCE.GetWindowThreadProcessId(hwnd, null);
      HKL keyboardLayoutHKL = SXUser32.INSTANCE.GetKeyboardLayout(threadID);
      if (keyboardLayoutHKL != null) {
        keyboarLayoutId = keyboardLayoutHKL.getLanguageIdentifier();
      }
    }

    synchronized(LAYOUTS) {
      layout = LAYOUTS.get(keyboarLayoutId);

      if (layout == null) {
        layout = buildWindowsLayout(keyboarLayoutId);
        LAYOUTS.put(keyboarLayoutId, layout);
      }
    }
  }
  return layout;
}
 
Example #3
Source File: RobotUtils.java    From karate with MIT License 6 votes vote down vote up
public static boolean switchToWinOs(Predicate<String> condition) {
    final AtomicBoolean found = new AtomicBoolean();
    User32.INSTANCE.EnumWindows((HWND hwnd, com.sun.jna.Pointer p) -> {
        char[] windowText = new char[512];
        User32.INSTANCE.GetWindowText(hwnd, windowText, 512);
        String windowName = Native.toString(windowText);
        logger.debug("scanning window: {}", windowName);
        if (condition.test(windowName)) {
            found.set(true);
            focusWinOs(hwnd);
            return false;
        }
        return true;
    }, null);
    return found.get();
}
 
Example #4
Source File: WebDriverService.java    From cerberus-source with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Tries to focus the browser window thanks to the title given by the
 * webdriver in order to put it in foreground for Robot to work (Only works
 * on Windows so far, another way is to find for Xorg)
 *
 * @param session Webdriver session instance
 * @return True if the window is found, False otherwise
 */
public boolean focusBrowserWindow(Session session) {
    WebDriver driver = session.getDriver();
    String title = driver.getTitle();
    try {
        User32 user32 = User32.instance;

        // Arbitrary
        String[] browsers = new String[]{
            "",
            "Google Chrome",
            "Mozilla Firefox",
            "Opera",
            "Safari",
            "Internet Explorer",
            "Microsoft Edge",};

        for (String browser : browsers) {
            HWND window;
            if (browser.isEmpty()) {
                window = user32.FindWindow(null, title);
            } else {
                window = user32.FindWindow(null, title + " - " + browser);
            }
            if (user32.ShowWindow(window, User32.SW_SHOW)) {
                return user32.SetForegroundWindow(window);
            }
        }

    } catch (Exception e) {
        LOG.error(e, e);
    }

    return false;
}
 
Example #5
Source File: BrowserComponent.java    From HubTurbo with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #6
Source File: Windows.java    From Simplified-JNA with MIT License 5 votes vote down vote up
/**
 * Returns a map of process HWND's to their window titles.
 * 
 * @return
 */
public static Map<HWND, String> getWindows() {
	Map<HWND, String> map = new HashMap<HWND, String>();
	User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
		@Override
		public boolean callback(HWND hWnd, Pointer arg1) {
			String wText = getWindowTitle(hWnd);
			map.put(hWnd, wText);
			return true;
		}
	}, null);
	return map;
}
 
Example #7
Source File: RobotUtils.java    From karate with MIT License 5 votes vote down vote up
public static boolean switchToWinOs(String title) {
    HWND hwnd = User32.INSTANCE.FindWindow(null, title);
    if (hwnd == null) {
        return false;
    } else {
        focusWinOs(hwnd);
        return true;
    }
}
 
Example #8
Source File: WinUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
private static HWND getHwnd(int pid, int winNum) {
  List<WindowInfo> windows = getWindowsForPid(pid);
  if (windows.size() > winNum) {
    return windows.get(winNum).getHwnd();
  }
  return null;
}
 
Example #9
Source File: WinUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
private static HWND getHwnd(String appName, int winNum) {
  List<WindowInfo> windows = getWindowsForName(appName);

  if (windows.size() > winNum) {
    return windows.get(winNum).getHwnd();
  }
  return null;
}
 
Example #10
Source File: WinUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
private int switchAppWindow(WindowInfo window) {
  HWND hwnd = window.getHwnd();

  WinUser.WINDOWPLACEMENT lpwndpl = new WinUser.WINDOWPLACEMENT();

  user32.GetWindowPlacement(hwnd, lpwndpl);

  if (lpwndpl.showCmd == WinUser.SW_SHOWMINIMIZED || lpwndpl.showCmd == WinUser.SW_MINIMIZE) {
    user32.ShowWindow(hwnd, WinUser.SW_RESTORE);
  }

  boolean success = user32.SetForegroundWindow(hwnd);

  if (success) {
    user32.SetFocus(hwnd);
    IntByReference windowPid = new IntByReference();
    user32.GetWindowThreadProcessId(hwnd, windowPid);

    return windowPid.getValue();
  } else {
    return 0;
  }
}
 
Example #11
Source File: WinUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
public static List<WindowInfo> allWindows() {
  /* Initialize the empty window list. */
  final List<WindowInfo> windows = new ArrayList<>();

  /* Enumerate all of the windows and add all of the one for the
   * given process id to our list. */
  boolean result = user32.EnumWindows(
      new WinUser.WNDENUMPROC() {
        public boolean callback(
            final HWND hwnd, final Pointer data) {

          if (user32.IsWindowVisible(hwnd)) {
            IntByReference windowPid = new IntByReference();
            user32.GetWindowThreadProcessId(hwnd, windowPid);

            String windowTitle = getWindowTitle(hwnd);

            windows.add(new WindowInfo(hwnd, windowPid.getValue(), windowTitle));
          }

          return true;
        }
      },
      null);

  /* Handle errors. */
  if (!result && Kernel32.INSTANCE.GetLastError() != 0) {
    throw new RuntimeException("Couldn't enumerate windows.");
  }

  /* Return the window list. */
  return windows;
}
 
Example #12
Source File: WinUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public Rectangle getWindow(App app, int winNum) {
  get(app);
  if (!app.isValid()) {
    return null;
  }
  HWND hwnd = getHwnd(app.getPID(), winNum);
  return hwnd != null ? getRectangle(hwnd, winNum) : null;
  //return getWindow(app.getPID(), winNum);
}
 
Example #13
Source File: WinUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public Rectangle getFocusedWindow() {
  HWND hwnd = user32.GetForegroundWindow();
  RECT rect = new User32.RECT();
  boolean success = user32.GetWindowRect(hwnd, rect);
  return success ? rect.toRectangle() : null;
  //return getFocusedRectangle();
}
 
Example #14
Source File: WinUtil.java    From SikuliX1 with MIT License 4 votes vote down vote up
private static Rectangle getRectangle(HWND hwnd, int winNum) {
  RECT rect = new User32.RECT();
  boolean success = user32.GetWindowRect(hwnd, rect);
  return success ? rect.toRectangle() : null;
}
 
Example #15
Source File: UI.java    From HubTurbo with GNU Lesser General Public License v3.0 4 votes vote down vote up
public HWND getMainWindowHandle() {
    return mainWindowHandle;
}
 
Example #16
Source File: Windows.java    From Simplified-JNA with MIT License 4 votes vote down vote up
/**
 * @param hWnd
 * @return Window information of a given HWND.
 */
public static WINDOWINFO getInfo(HWND hWnd) {
	WINDOWINFO info = new WINDOWINFO();
	User32.INSTANCE.GetWindowInfo(hWnd, info);
	return info;
}
 
Example #17
Source File: Windows.java    From Simplified-JNA with MIT License 4 votes vote down vote up
/**
 * @param hWnd
 * @return Title of a given process HWND.
 */
public static String getWindowTitle(HWND hWnd) {
	char[] buffer = new char[MAX_TITLE_LENGTH * 2];
	User32.INSTANCE.GetWindowText(hWnd, buffer, MAX_TITLE_LENGTH);
	return Native.toString(buffer);
}
 
Example #18
Source File: WinUtil.java    From SikuliX1 with MIT License 4 votes vote down vote up
@Override
public Rectangle getWindow(String title) {
  HWND hwnd = getHwnd(title, 0);
  return hwnd != null ? getRectangle(hwnd, 0) : null;
  //return getWindow(title, 0);
}
 
Example #19
Source File: WinUtil.java    From SikuliX1 with MIT License 4 votes vote down vote up
public static String getWindowTitle(HWND hWnd) {
  char[] text = new char[1024];
  int length = user32.GetWindowText(hWnd, text, 1024);
  return length > 0 ? new String(text, 0, length) : null;
}
 
Example #20
Source File: WinUtil.java    From SikuliX1 with MIT License 4 votes vote down vote up
public HWND getHwnd() {
  return hwnd;
}
 
Example #21
Source File: RobotUtils.java    From karate with MIT License 4 votes vote down vote up
private static void focusWinOs(HWND hwnd) {
    User32.INSTANCE.ShowWindow(hwnd, 9); // SW_RESTORE
    User32.INSTANCE.SetForegroundWindow(hwnd);
}
 
Example #22
Source File: WinUtil.java    From SikuliX1 with MIT License 4 votes vote down vote up
public WindowInfo(HWND hwnd, int pid, String title) {
  super();
  this.hwnd = hwnd;
  this.pid = pid;
  this.title = title;
}
 
Example #23
Source File: MouseEventReceiver.java    From Simplified-JNA with MIT License 2 votes vote down vote up
/**
 * Called when the mouse wheel is moved. Returning true will cancel the event.
 * 
 * @param hwnd
 *            Window handle that receives the event.
 * @param info
 *            Mouse information.
 * @return Event cancellation
 */
public abstract boolean onMouseMove(HWND hwnd, POINT info);
 
Example #24
Source File: WinUser.java    From jpexs-decompiler with GNU General Public License v3.0 2 votes vote down vote up
/**
 * @param hwnd [in] Type: HWND
 *
 * A handle to the window.
 *
 * @param uMsg [in] Type: UINT
 *
 * The message.
 *
 * For lists of the system-provided messages, see System-Defined
 * Messages.
 *
 * @param wParam [in] Type: WPARAM
 *
 * Additional message information. The contents of this parameter depend
 * on the value of the uMsg parameter.
 *
 * @param lParam [in] Type: LPARAM
 *
 * Additional message information. The contents of this parameter depend
 * on the value of the uMsg parameter.
 *
 * @return the lresult
 */
LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam);
 
Example #25
Source File: WinUser.java    From jpexs-decompiler with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Return whether to continue enumeration.
 *
 * @param hWnd hWnd
 * @param data data
 * @return result
 */
boolean callback(HWND hWnd, Pointer data);
 
Example #26
Source File: Windows.java    From Simplified-JNA with MIT License 2 votes vote down vote up
/**
 * Set the position and size of a given window.
 * 
 * @param hWnd
 *            Window handle.
 * @param x
 * @param y
 * @param width
 * @param height
 * @param flags
 */
public static void setWindowPos(HWND hWnd, int x, int y, int width, int height, int flags) {
	User32.INSTANCE.SetWindowPos(hWnd, null, x, y, width, height, flags);
}
 
Example #27
Source File: Windows.java    From Simplified-JNA with MIT License 2 votes vote down vote up
/**
 * Set window's state.
 * 
 * @param hWnd
 * @param flags
 *            State flags.
 */
public static void showWindow(HWND hWnd, int flags) {
	User32.INSTANCE.ShowWindow(hWnd, flags);
}
 
Example #28
Source File: MouseEventReceiver.java    From Simplified-JNA with MIT License 2 votes vote down vote up
/**
 * Called when the mouse wheel is moved. Returning true will cancel the event.
 * 
 * @param down
 *            Scroll event is down <i>(Negative movement)</i>
 * @param hwnd
 *            Window handle that receives the event.
 * @param info
 *            Mouse information.
 * @return Event cancellation
 */
public abstract boolean onMouseScroll(boolean down, HWND hwnd, POINT info);
 
Example #29
Source File: MouseEventReceiver.java    From Simplified-JNA with MIT License 2 votes vote down vote up
/**
 * Called when the mouse is released. Returning true will cancel the event.
 * 
 * @param type
 *            Type is press <i>(Left, Right, Middle)</i>
 * @param info
 *            Mouse information.
 * @return Event cancellation
 */
public abstract boolean onMouseRelease(MouseButtonType type, HWND hwnd, POINT info);
 
Example #30
Source File: Windows.java    From Simplified-JNA with MIT License 2 votes vote down vote up
/**
 * 
 * @return
 */
public static HWND getDesktop() {
	return User32.INSTANCE.GetDesktopWindow();
}