自动化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 小时前
服务器托管 vs 云主机 vs 裸金属:一个决策故事
运维·服务器·云计算
杨云龙UP1 小时前
Oracle RAC / ODA 生产环境指定 PDB 启动 SOP
linux·运维·数据库·oracle
郑洁文2 小时前
基于网络爬虫的Web敏感信息泄露自动化检测工具
前端·爬虫·网络安全·自动化
luweis2 小时前
企智孪生 ETA(3.3 认知算法层:ETA 的思维内核 3.4 基础架构:算力与弹性)【浙江联保网络 卢伟舜】
大数据·运维·线性代数·ai·矩阵·学习方法
极客老王说Agent2 小时前
屏幕理解能力是下一代自动化的关键吗?2026年自动化范式演进深度解析
运维·人工智能·ai·chatgpt·自动化
LT10157974443 小时前
2026年电商RPA选型指南:电商运营全流程自动化测评
运维·自动化·rpa
Black蜡笔小新3 小时前
自动化AI算法训练服务器DLTM训推一体化平台助力农业生产管理实现安全智能化
人工智能·算法·自动化
JAVA社区3 小时前
Java高级全套教程(十一)—— Kubernetes 超详细企业级实战详解
java·运维·微服务·容器·面试·kubernetes
linyanRPA4 小时前
影刀RPA+Python店群自动化实战:自研环境隔离引擎,200店铺并发不卡不串号
python·自动化·rpa
tedcloud1234 小时前
cc-switch评测:多AI Coding Agent管理工具详解
数据库·人工智能·sql·学习·自动化