自动化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软件的开发。

相关推荐
分布式存储与RustFS6 小时前
备份作业卡在许可证上:用 Veeam 13 把 Windows 整机备份落进 RustFS
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
飞奔的瓜牛6 小时前
Linux 数据盘分区、格式化与自动挂载完整实践
linux·运维·服务器
进击的荆棘8 小时前
Linux网络——Socket编程TCP
linux·运维·服务器·网络·tcp/ip
承渊政道8 小时前
【Python编程快速上手—让繁琐工作自动化】(字典和结构化数据:用键值对搭出棋盘和游戏背包)
python·游戏·pycharm·自动化·字典和结构化数据
大今野8 小时前
2026 专转本计算机|Linux CentOS7 软件包管理:yum 仓库配置与常用命令考点全解(附例题)
linux·运维·服务器·专转本
无奈笑天下9 小时前
Wine适配Windows程序常见故障标准处理流程
linux·运维·服务器·windows
沫璃染墨9 小时前
从零入门计算机网络系列(二十六):IPv4分片机制详解——从MTU与MSS到报文分片与重组
运维·服务器·网络·网络协议·tcp/ip·ip
无足鸟ICT10 小时前
【RHCA+】hdparm命令(查看硬盘)
linux·运维·服务器
时间的拾荒人1 天前
Linux 系统命令与网络、磁盘、日志监控(下篇):性能原理与优化
linux·运维·网络
zhensherlock1 天前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js