上传备份

master
王兵 3 weeks ago
parent 3e5b47a61c
commit 23fbfb426a

@ -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;
}
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.