|
|
|
@ -6,11 +6,21 @@ import com.sun.jna.platform.win32.WinDef;
|
|
|
|
|
import com.sun.jna.platform.win32.WinUser;
|
|
|
|
|
import com.sun.jna.win32.W32APIOptions;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 窗口工具类
|
|
|
|
|
* <p>
|
|
|
|
|
* 可以打印窗口句柄和窗口信息
|
|
|
|
|
* 可以根据关键词匹配窗口句柄
|
|
|
|
|
* <p>
|
|
|
|
|
* 如果使用的依赖树不含jna库,需要添加以下依赖
|
|
|
|
|
* <dependency>
|
|
|
|
|
* <groupId>net.java.dev.jna</groupId>
|
|
|
|
|
* <artifactId>jna</artifactId>
|
|
|
|
|
* <version>5.14.0</version>
|
|
|
|
|
* </dependency>
|
|
|
|
|
*
|
|
|
|
|
* @author wangbing
|
|
|
|
|
* @version 0.0.1
|
|
|
|
@ -139,11 +149,119 @@ public class WindowsUtil {
|
|
|
|
|
}, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static WinDef.HWND findWindow(String className, String windowName) {
|
|
|
|
|
return User32.INSTANCE.FindWindow(className, windowName);
|
|
|
|
|
/**
|
|
|
|
|
* 查找窗口
|
|
|
|
|
*
|
|
|
|
|
* @param className 类名
|
|
|
|
|
* @param windowName 窗口名
|
|
|
|
|
* @return 窗口句柄
|
|
|
|
|
*/
|
|
|
|
|
public static List<WindowInfo> findWindow(String className, String windowName) {
|
|
|
|
|
// 实际使用中,发现这个方法返回存在误差,可能返回的不是我们想要的
|
|
|
|
|
// User32.INSTANCE.FindWindow(className, windowName);
|
|
|
|
|
|
|
|
|
|
List<WindowInfo> windowInfos = new ArrayList<>();
|
|
|
|
|
User32.INSTANCE.EnumWindows((hWnd, data) -> {
|
|
|
|
|
if (!User32.INSTANCE.IsWindowVisible(hWnd)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取类名
|
|
|
|
|
char[] classNameBuffer = new char[512];
|
|
|
|
|
User32.INSTANCE.GetClassNameW(hWnd, classNameBuffer, 512);
|
|
|
|
|
String actualClassName = Native.toString(classNameBuffer);
|
|
|
|
|
|
|
|
|
|
// 获取窗口标题
|
|
|
|
|
char[] windowText = new char[512];
|
|
|
|
|
User32.INSTANCE.GetWindowTextW(hWnd, windowText, 512);
|
|
|
|
|
String title = Native.toString(windowText);
|
|
|
|
|
|
|
|
|
|
// 双重条件匹配
|
|
|
|
|
boolean classMatches = (className == null || actualClassName.equals(className));
|
|
|
|
|
boolean titleMatches = (windowName == null || title.contains(windowName));
|
|
|
|
|
|
|
|
|
|
if (classMatches && titleMatches) {
|
|
|
|
|
windowInfos.add(parseWindowInfo(hWnd));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
return windowInfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在现有类中添加私有方法
|
|
|
|
|
private static WindowInfo parseWindowInfo(WinDef.HWND hWnd) {
|
|
|
|
|
char[] className = new char[512];
|
|
|
|
|
User32.INSTANCE.GetClassNameW(hWnd, className, 512);
|
|
|
|
|
|
|
|
|
|
char[] windowText = new char[512];
|
|
|
|
|
User32.INSTANCE.GetWindowTextW(hWnd, windowText, 512);
|
|
|
|
|
|
|
|
|
|
WinDef.RECT rect = new WinDef.RECT();
|
|
|
|
|
if (User32.INSTANCE.GetWindowRect(hWnd, rect)) {
|
|
|
|
|
return new WindowInfo(
|
|
|
|
|
Native.toString(className),
|
|
|
|
|
Native.toString(windowText),
|
|
|
|
|
// x坐标
|
|
|
|
|
rect.left,
|
|
|
|
|
// y坐标
|
|
|
|
|
rect.top,
|
|
|
|
|
// 宽度
|
|
|
|
|
rect.right - rect.left,
|
|
|
|
|
// 高度
|
|
|
|
|
rect.bottom - rect.top,
|
|
|
|
|
hWnd
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
printWindowInfo("微信");
|
|
|
|
|
public static class WindowInfo {
|
|
|
|
|
private final String className;
|
|
|
|
|
private final String windowName;
|
|
|
|
|
private final int x;
|
|
|
|
|
private final int y;
|
|
|
|
|
private final int width;
|
|
|
|
|
private final int height;
|
|
|
|
|
private final WinDef.HWND hWnd;
|
|
|
|
|
|
|
|
|
|
public WindowInfo(String className, String windowName, int x, int y,
|
|
|
|
|
int width, int height, WinDef.HWND hWnd) {
|
|
|
|
|
this.className = className;
|
|
|
|
|
this.windowName = windowName;
|
|
|
|
|
this.x = x;
|
|
|
|
|
this.y = y;
|
|
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
|
|
|
|
this.hWnd = hWnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getClassName() {
|
|
|
|
|
return className;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getWindowName() {
|
|
|
|
|
return windowName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getX() {
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getY() {
|
|
|
|
|
return y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getWidth() {
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getHeight() {
|
|
|
|
|
return height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WinDef.HWND gethWnd() {
|
|
|
|
|
return hWnd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|