Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package de.tkunkel.mechcollect.watcher;
  2.  
  3. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.Kernel32.PROCESS_QUERY_INFORMATION;
  4. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.Kernel32.PROCESS_VM_READ;
  5. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.Psapi.GetModuleBaseNameW;
  6. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetForegroundWindow;
  7. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetWindowRect;
  8. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetWindowTextW;
  9. import static de.tkunkel.mechcollect.watcher.EnumerateWindows.User32DLL.GetWindowThreadProcessId;
  10.  
  11. import java.awt.Rectangle;
  12.  
  13. import com.sun.jna.Native;
  14. import com.sun.jna.Pointer;
  15. import com.sun.jna.platform.win32.WinDef;
  16. import com.sun.jna.platform.win32.WinDef.HWND;
  17. import com.sun.jna.platform.win32.WinUser;
  18. import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
  19. import com.sun.jna.ptr.PointerByReference;
  20.  
  21. public class EnumerateWindows {
  22.     private static final int MAX_TITLE_LENGTH = 1024;
  23.     static Rectangle rc = null;
  24.  
  25.     public static Rectangle getSizeByWindowName(final String windowName) throws Exception {
  26.  
  27.         User32DLL.EnumWindows(new WNDENUMPROC() {
  28.             @Override
  29.             public boolean callback(HWND hWnd, Pointer arg1) {
  30.                 byte[] windowText = new byte[512];
  31.                 User32DLL.GetWindowTextA(hWnd, windowText, 512);
  32.                 String wText = Native.toString(windowText);
  33.  
  34.                 WinDef.RECT lpRect = new WinDef.RECT();
  35.                 GetWindowRect(hWnd, lpRect);
  36.  
  37.                 if (wText == null || wText.isEmpty()) {
  38.                     //return true;
  39.                 } else if (wText.equalsIgnoreCase(windowName)) {
  40.                     rc = lpRect.toRectangle();
  41.                     //System.out.println(windowName);
  42.                     //System.out.println(lpRect);
  43.                 }
  44.  
  45.                 return true;
  46.             }
  47.         }, null);
  48.  
  49.         return rc;
  50.  
  51.     }
  52.  
  53.     public static Rectangle getSizeByProcessnameActiveWindow(String processName) throws Exception {
  54.         Rectangle rc = null;
  55.  
  56.         char[] buffer = new char[MAX_TITLE_LENGTH * 2];
  57.  
  58.         WinDef.RECT lpRect = new WinDef.RECT();
  59.         GetWindowRect(GetForegroundWindow(), lpRect);
  60.  
  61.         GetWindowTextW(GetForegroundWindow(), buffer, MAX_TITLE_LENGTH);
  62.  
  63.         PointerByReference pointer = new PointerByReference();
  64.  
  65.         GetWindowThreadProcessId(GetForegroundWindow(), pointer);
  66.  
  67.         Pointer process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pointer.getValue());
  68.  
  69.         GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
  70.  
  71.         if (processName.equalsIgnoreCase(Native.toString(buffer))) {
  72.             rc = lpRect.toRectangle();
  73.         }
  74.         return rc;
  75.  
  76.     }
  77.  
  78.     public static void main(String[] args) throws Exception {
  79.         getSizeByWindowName("MechWarrior Online");
  80.        
  81.     }
  82.  
  83.     static class Psapi {
  84.         static {
  85.             Native.register("psapi");
  86.         }
  87.  
  88.         public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
  89.     }
  90.  
  91.     static class Kernel32 {
  92.         static {
  93.             Native.register("kernel32");
  94.         }
  95.  
  96.         public static int PROCESS_QUERY_INFORMATION = 0x0400;
  97.  
  98.         public static int PROCESS_VM_READ = 0x0010;
  99.  
  100.         public static native int GetLastError();
  101.  
  102.         public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer);
  103.     }
  104.  
  105.     static class User32DLL {
  106.         static {
  107.             Native.register("user32");
  108.         }
  109.  
  110.         public static native int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);
  111.  
  112.         public static native HWND GetForegroundWindow();
  113.  
  114.         public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);
  115.  
  116.         public static native boolean GetWindowRect(HWND hWnd, WinDef.RECT lpRect);
  117.  
  118.         public static native boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
  119.  
  120.         public static native int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement