package at.pb.main;
import java.util.ArrayList;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.examples.win32.GDI32;
import com.sun.jna.examples.win32.User32;
import com.sun.jna.examples.win32.GDI32.RECT;
import com.sun.jna.examples.win32.User32.WNDENUMPROC;
import com.sun.jna.examples.win32.W32API.HWND;
public class WindowObserver {
private static ArrayList<Fenster> fenster;
public static void main(String[] args) {
ArrayList<Fenster> fenster = WindowObserver.getOpenWindows();
for(Fenster f: fenster)
System.out.println(f.getName()+" "+f.getRectangle());
}
public static ArrayList<Fenster> getOpenWindows() {
fenster = new ArrayList<Fenster>();
User32B user32 = User32B.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
public boolean callback(HWND hwnd, Pointer arg1) {
User32B user32 = User32B.INSTANCE;
if (user32.IsWindowVisible(hwnd)) {
byte[] name = new byte[512];
user32.GetWindowText(hwnd, name, name.length);
Fenster f = new Fenster();
f.setName(byte2String(name));
RECT r = new GDI32.RECT();
user32.GetWindowRect(hwnd, r);
f.setRectangle(r.toRectangle());
fenster.add(f);
}
return true;
}
}, null);
return fenster;
}
public static String byte2String(byte[] array) {
char[] charray = new char[array.length / 2];
int c = 0;
for (int i = 0; i < charray.length; i = i + 2) {
Byte bt = new Byte(array[i]);
char ch = (char) bt.intValue();
if (ch == 0) {
break;
}
charray[c] = ch;
c++;
}
String str = new String(charray);
return str.substring(0, c);
}
public interface User32B extends User32 {
User32B INSTANCE = (User32B) Native.loadLibrary("user32",
User32B.class, DEFAULT_OPTIONS);
boolean IsWindowVisible(HWND hWnd);
}
}