// 设置窗口最大化
public static void setMAX(HWND hwnd) {
if (hwnd == null)
return;
while (!isMax(hwnd)) {
System.out.println("尝试窗口最大化");
User32.INSTANCE.ShowWindow(hwnd, User32.SW_MAXIMIZE);
}
while (!hwnd.equals(getACtiveHwnd())) {
System.out.println("设焦点");
User32.INSTANCE.SetForegroundWindow(hwnd);
}
}
public static boolean isMin(HWND hwnd) {
if (hwnd == null)
return false;
RECT rf = getWindowRect(hwnd); // 返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
if (rf.right < 0 || rf.bottom < 0)
return true;
return false;
}
public static boolean isMax(HWND hwnd) {
if (hwnd == null)
return false;
RECT rf = getWindowRect(hwnd);
if (rf.left < 0 && rf.top < 0)
return true;
return false;
}
// 由窗体句柄得到位置信息
public static RECT getWindowRect(HWND hwnd) {
if (hwnd == null)
return null;
RECT rf = new RECT();
User32.INSTANCE.GetWindowRect(hwnd, rf);
return rf;
}
// 判断坐标(x,y)是否在rf内
public static boolean inItem(int x, int y, RECT rf) {
if (rf.right < 0 || rf.bottom < 0)
return false;
if (x < rf.left || x > rf.right || y < rf.top || y > rf.bottom)
return false;
return true;
}
// 判断点是否rf内
public static boolean inItem(Point mousepoint, RECT rf) {
return inItem(mousepoint.x, mousepoint.y, rf);
}
// 由句柄获取窗口名称
public static String getTitle(HWND hwnd) {
if (hwnd == null)
return null;
char[] sTitle = new char[255];
User32.INSTANCE.GetWindowText(hwnd, sTitle, 255);
String title = char2String(sTitle);
return title;
}
// 获取当前聚焦的窗体的句柄
public static HWND getACtiveHwnd() {
HWND hwnd = User32.INSTANCE.GetForegroundWindow();
return hwnd;
}
// 获取当期鼠标位置
public static Point getPoint() {
Point mousepoint = MouseInfo.getPointerInfo().getLocation();
return mousepoint;
}
// 获取鼠标所在该控件
public static List<HWND> getCurrentItem() {
HWND window = JNAUtils.getACtiveHwnd();
List<HWND> hs = JNAUtils.getItems(window);
List<HWND> hwnds = new ArrayList<HWND>();
for (HWND h : hs) {
RECT rf = JNAUtils.getWindowRect(h);
Point mousepoint = JNAUtils.getPoint();
if (JNAUtils.inItem(mousepoint, rf)) {
hwnds.add(h);
}
}
return hwnds;
}