Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.tkunkel.mechcollect.watcher;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.Kernel32.PROCESS_QUERY_INFORMATION;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.Kernel32.PROCESS_VM_READ;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.Psapi.GetModuleBaseNameW;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetForegroundWindow;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetWindowRect;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetWindowTextW;
- import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetWindowThreadProcessId;
- import java.awt.Rectangle;
- import com.sun.jna.Native;
- import com.sun.jna.Pointer;
- import com.sun.jna.platform.win32.WinDef;
- import com.sun.jna.platform.win32.WinDef.HWND;
- import com.sun.jna.platform.win32.WinUser;
- import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
- import com.sun.jna.ptr.PointerByReference;
- public class EnumerateWindows {
- private static final int MAX_TITLE_LENGTH = 1024;
- static Rectangle rc = null;
- public static Rectangle getSizeByWindowName(final String windowName) throws Exception {
- User32DLL.EnumWindows(new WNDENUMPROC() {
- @Override
- public boolean callback(HWND hWnd, Pointer arg1) {
- byte[] windowText = new byte[512];
- User32DLL.GetWindowTextA(hWnd, windowText, 512);
- String wText = Native.toString(windowText);
- WinDef.RECT lpRect = new WinDef.RECT();
- GetWindowRect(hWnd, lpRect);
- if (wText == null || wText.isEmpty()) {
- //return true;
- } else if (wText.equalsIgnoreCase(windowName)) {
- rc = lpRect.toRectangle();
- //System.out.println(windowName);
- //System.out.println(lpRect);
- }
- return true;
- }
- }, null);
- return rc;
- }
- public static Rectangle getSizeByProcessnameActiveWindow(String processName) throws Exception {
- Rectangle rc = null;
- char[] buffer = new char[MAX_TITLE_LENGTH * 2];
- WinDef.RECT lpRect = new WinDef.RECT();
- GetWindowRect(GetForegroundWindow(), lpRect);
- GetWindowTextW(GetForegroundWindow(), buffer, MAX_TITLE_LENGTH);
- PointerByReference pointer = new PointerByReference();
- GetWindowThreadProcessId(GetForegroundWindow(), pointer);
- Pointer process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pointer.getValue());
- GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
- if (processName.equalsIgnoreCase(Native.toString(buffer))) {
- rc = lpRect.toRectangle();
- }
- return rc;
- }
- public static void main(String[] args) throws Exception {
- getSizeByWindowName("MechWarrior Online");
- }
- static class Psapi {
- static {
- Native.register("psapi");
- }
- public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
- }
- static class Kernel32 {
- static {
- Native.register("kernel32");
- }
- public static int PROCESS_QUERY_INFORMATION = 0x0400;
- public static int PROCESS_VM_READ = 0x0010;
- public static native int GetLastError();
- public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer);
- }
- static class User32DLL {
- static {
- Native.register("user32");
- }
- public static native int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);
- public static native HWND GetForegroundWindow();
- public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);
- public static native boolean GetWindowRect(HWND hWnd, WinDef.RECT lpRect);
- public static native boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
- public static native int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement