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

相关推荐
曹牧7 分钟前
Spring MVC : Controller 层URL划分
java·运维·服务器·前端
路漫漫其修远兮sjw7 分钟前
Linux 运维知识点笔记(中级)
linux·运维·笔记
石一峰69911 分钟前
Linux INPUT 与 I2C 子系统关联
linux·运维·计算机外设
智商网输送线配件供应25 分钟前
广东自动化流水线配件滚筒输送线选型实战:小批量混批采购模式如何重构供应链效率
大数据·重构·自动化
海宇AI31 分钟前
零信任架构实战:基于海宇学历核验版构建自动化高并发资信评估网关
人工智能·微服务·架构·自动化
Dragon_qu·x34 分钟前
MongoDB 高可用集群部署
运维·数据库·mongodb·k8s·helm
YOLO数据集集合35 分钟前
风机叶片缺陷检测数据集 | 风机叶片 缺陷检测 风电运维 无人机巡检 细粒度分类9126期
运维·分类·无人机·风电叶片·风力·叶片缺陷
亚川楼宇自控系统数据中心厂家1 小时前
3D 数字孪生赋能电气综合保护监控:看得见的电力安全防线
运维
北冥有鱼被烹1 小时前
NVIDIA DGX/HGX 服务器全景深度解析:从 H100 到 GB300,专业算力选型必读
运维·服务器·网络·python
程序员-Benothing1 小时前
Linux系统启动流程详解:BIOS/UEFI、GRUB、initramfs、systemd
linux·运维·服务器