自动化RPA开发 --获取所有窗口信息和进程信息

场景

准备做一个RPA工具,可以从桌面和浏览器选择元素,获取窗口信息和进程信息是必要的,因为获取了窗口信息和进程,可用对程序做一些想要的操作。

coding

工具类

java 复制代码
/**
 * Windows系统工具类
 */
public class WinOsUtils {

    static final User32 user32 = User32.INSTANCE;


    /**
     * 获取当前系统下所有的Windows窗口信息
     * @return
     */
    public static List<WindowInfo> allWindows() {

        final List<WindowInfo> windows = new ArrayList<>();


        boolean result = user32.EnumWindows(
                new WinUser.WNDENUMPROC() {
                    public boolean callback(
                            final WinDef.HWND hwnd, final Pointer data) {

                        if (user32.IsWindowVisible(hwnd)) {
                            IntByReference windowPid = new IntByReference();
                            user32.GetWindowThreadProcessId(hwnd, windowPid);

                            String windowTitle = getWindowTitle(hwnd);

                            windows.add(new WindowInfo(hwnd, windowPid.getValue(), windowTitle));
                        }

                        return true;
                    }
                },
                null);

        /* Handle errors. */
        if (!result && Kernel32.INSTANCE.GetLastError() != 0) {
            throw new RuntimeException("Couldn't enumerate windows.");
        }

        /* Return the window list. */
        return windows;
    }


    /**
     * 过窗口句柄来获取指定窗口的标题文本,通常用于窗口管理、自动化测试、或需要识别和操作特定窗口的应用程序中。
     * @param hWnd 句柄
     * @return 窗口标题
     */
    public static String getWindowTitle(WinDef.HWND hWnd) {
        char[] text = new char[1024];
        int length = user32.GetWindowText(hWnd, text, 1024);
        return length > 0 ? new String(text, 0, length) : null;
    }
}

窗口信息

java 复制代码
/**
 * 窗口信息
 */
public class WindowInfo {
    /**
     * 句柄 唯一定义APP的因素
     */
    public WinDef.HWND hwnd;

    /**
     * 进程id
     */
    public int pid;

    /**
     * 标题
     */
    public String title;

    public WindowInfo(WinDef.HWND hwnd, int pid, String title) {
        super();
        this.hwnd = hwnd;
        this.pid = pid;
        this.title = title;
    }

    public WinDef.HWND getHwnd() {
        return hwnd;
    }

    public int getPid() {
        return pid;
    }

    public String getTitle() {
        return title;
    }
}

进程信息

java 复制代码
/**
 * 系统进程信息
 * @Author: Herche Jane
 * @Date: 2023/10/16
 */
public class ProcessInfo {

    /**
     * 进程名称
     */
    private String name;

    /**
     * pid
     */
    private Integer pid;
    
    public ProcessInfo(String name,Integer pid){
        this.name = name;
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

 
    public Integer getPid() {
        return pid;
    }

  
}

结束

这样我们获取了所有的窗口信息和进程信息了,后面有利于我们RPA软件的开发。

相关推荐
女神下凡1 小时前
芯参谋(87):Linux 系统(SPI NOR)驱动开发指南
linux·运维·arm开发·驱动开发·嵌入式硬件·设计规范
海宇AI8 小时前
零信任架构实战:基于海宇个人风险报告构建自动化分期商城信用额度初审网关
运维·人工智能·架构·自动化
IMPYLH8 小时前
Linux 的时间为什么会被拨回去
linux·运维·服务器
2601_963282779 小时前
寒地窄带通信工程实践:黑龙江哈尔滨对讲机选型、组网与合规运维总结
运维·服务器·网络
叼烟扛炮10 小时前
[Git 实战] 学习笔记:从生成 SSH 密钥到解决 TortoiseGit 推送报错全指南
linux·运维·笔记·git·学习
我要见SA姐111 小时前
GitHub Actions 自动化:5 个真实运维提效案例
运维·数据库·机器学习·oracle·回归
Android系统攻城狮12 小时前
Linux FFmpeg深度解析之avcodec_flush_buffers调用流程与实战(十九)
linux·运维·服务器·ffmpeg·车载音视频·ffmpeg音视频进阶
QXWZ_IA13 小时前
公路边坡裂缝的自动化监测布设
大数据·人工智能·自动化
Y38153266213 小时前
用 httpx 写 SerpBase 异步客户端:连接池与超时配置
运维·服务器·数据库
倔强的石头10613 小时前
【Linux指南】进程通信系列(七):System V 信号量 —— 同步互斥的内核原语
linux·运维·服务器