当前位置: 首页 > news >正文

Windows下使用UIAutomation技术遍历桌面窗口和指定窗口内容的AutomationWalker.exe的C#源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Automation;
//引用"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\UIAutomationClient.dll"
//引用"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\UIAutomationTypes.dll"namespace ConsoleApp1
{class Program{static void WalkElement(AutomationElement element, TreeWalker walker, int depth, int limit){Console.WriteLine($"{new string(' ', depth * 2)}" + $"\"{element.Current.Name}\"");if (limit>0 && depth>=limit) return;AutomationElement child = walker.GetFirstChild(element);while (child != null){WalkElement(child, walker, depth + 1, limit);child = walker.GetNextSibling(child);}}static void Main(string[] args){if (args.Length < 1){Console.WriteLine("Usage: AutomationWalker \"window_title\"|desktop");return;} else if (args[0]=="desktop"){try{Console.WriteLine("Walker desktop begin ...");//Get the destkop elementAutomationElement elemDesktop = AutomationElement.RootElement;TreeWalker walker = TreeWalker.ControlViewWalker;WalkElement(elemDesktop, walker, 0, 1);Console.WriteLine("... Walker desktop end.");}catch (Exception){Console.WriteLine("Walker desktop error!");}return;}try{Console.WriteLine("Walker begin ...");//Get the destkop elementAutomationElement elemDesktop = AutomationElement.RootElement;//Search the Application main window by title from all childrenPropertyCondition pCondition = new PropertyCondition(AutomationElement.NameProperty, args[0]);AutomationElementCollection elemApplicationWindows = elemDesktop.FindAll(TreeScope.Children, pCondition);Console.WriteLine("elemApplicationWindows.Count:" + elemApplicationWindows.Count);for (int i = 0; i < elemApplicationWindows.Count; i++){TreeWalker walker = TreeWalker.ControlViewWalker;WalkElement(elemApplicationWindows[i], walker, 0, 0);}Console.WriteLine("... Walker end.");}catch (Exception){Console.WriteLine("Walker error!");}}}
}

将以上代码放在C#控制台项目中,生成AutomationWalker.exe

用法:

在cmd窗口中输入命令

AutomationWalker.exe desktop

列出当前桌面上所有窗口的用双引号括住的标题,比如

Walker desktop begin ...
""
""
"管理员: 命令提示符 - automationwalker  desktop"
"新标签页 - Google Chrome"
"dummyLayeredWnd"
"Program Manager"
... Walker desktop end.

输入命令

AutomationWalker.exe "新标签页 - Google Chrome"

列出窗口"新标签页 - Google Chrome"的内容。

当你想要提取某个窗口的文本内容时,使用AutomationWalker会很方便。

http://www.xdnf.cn/news/1177111.html

相关文章:

  • C++高效实现轨迹规划、自动泊车、RTS游戏、战术迂回包抄、空中轨迹、手术机器人、KD树
  • Java技术栈/面试题合集(17)-Git篇
  • Spring-狂神说
  • day20 双向链表
  • MAC包头、IP包头 、UDP包头中的长度含义是啥?三者之间有啥区别?
  • 【SpringAI实战】提示词工程实现哄哄模拟器
  • 中小企业安全落地:低成本漏洞管理与攻击防御方案
  • SpringCache
  • 双紫擒龙紫紫红黄安装使用攻略,2025通达信指标源码,擒龙追踪源码公式学习
  • 遨游三防平板|国产芯片鸿蒙系统单北斗三防平板,安全高效
  • 算法调试技巧
  • 《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——4. 前后端联动:打通QML与C++的任督二脉
  • 【基础】go基础学习笔记
  • 极客大挑战2019-HTTP
  • 基于Odoo的微信小程序全栈开发探索分析
  • 探索复杂列表开发:从基础到高级的全面指南
  • SSE与Websocket有什么区别?
  • 如何在 conda 中删除环境
  • rust-结构体使用示例
  • Elasticsearch 的聚合(Aggregations)操作详解
  • 使用phpstudy极简快速安装mysql
  • Java 大视界 -- Java 大数据在智能家居能源管理与节能优化中的深度应用(361)
  • API安全监测工具:数字经济的免疫哨兵
  • 五、Vue项目开发流程
  • LeetCode 2563.统计公平数对的数目
  • Effective Python 第16条:用get处理字典缺失键,避免in与KeyError的陷阱
  • 【低空经济之无人集群】
  • runc源码解读(一)——runc create
  • C++右值引用与移动语义详解
  • QML 模型