作者君 发表于 2025-4-23 13:04:36

【操作支持UIAutomation的窗口控件】内部集成的AutoHotKey的UIAutomation的用法

本帖最后由 作者君 于 2025-4-23 13:27 编辑

从2.0.5开始,zTasker直接继承了AHK的UIAutomation模块,原地址:https://github.com/Descolada/UIA-v2

您可以在任务分类->程序->执行AutoHotkey脚本里,选择执行V2版AutoHotkey脚本时,
直接在代码里#Include UIA.ahk就可以使用UIAutomation操作桌面上UI元素,十分方便

zTasker一个用法示例的任务:https://bbs.everauto.net/forum.php?mod=viewthread&tid=382

一些基本用法如下:搬运自:https://www.autoahk.com/archives/40453,感谢原作者Tebayaki

基本的元素获取:
[*]ele := UIA.ElementFromHandle(WinExist("A")) ; 获取窗口元素
[*]ele := UIA.ElementFromPoint(100 | 200 << 32) ; 获取屏幕坐标 100, 200 的元素
[*]ele := UIA.GetFocusedElement() ; 获取当前具有键盘焦点的元素

基本属性的获取:
[*]ele := UIA.ElementFromHandle(WinExist("A"))
[*]MsgBox ele.Name
[*]MsgBox ele.ClassName
[*]pos := ele.BoundingRectangle
[*]MsgBox pos.left " " pos.top " " pos.right " " pos.bottom

获取指定QQ聊天的最新消息:
[*]DetectHiddenWindows(true)
[*]qq := UIA.ElementFromHandle(WinExist("QQ ahk_class TXGuiFoundation"))
[*]condi := UIA.CreatePropertyCondition(30005, "AutoHotkey(ahk) | 中文社区") ;创建一个条件,指定30005: Name
[*]dialog := qq.FindFirst(condi)
[*]MsgBox dialog.Value

获取Edge浏览器的当前URL内容:
[*]chrome := UIA.ElementFromHandle(WinExist("ahk_exe msedge.exe"))
[*]; 有时候通过FindFirst寻找元素可能会比较慢,可以用TreeWalker指定搜索条件
[*]walker := UIA.CreateTreeWalker(UIA.CreatePropertyCondition(30006, "Ctrl+L")) ; 30006: AcceleratorKey
[*]search_bar := walker.GetFirstChildElement(chrome)
[*]MsgBox search_bar.Value

还可以简单地操作网页,打开bing并搜索指定内容:
[*]Run "www.bing.com"
[*]hwnd := WinWaitActive("必应")
[*]page := UIA.ElementFromHandle(hwnd)
[*]condi1 := UIA.CreatePropertyCondition(30005, "Enter your search term")
[*]; 组合两个条件
[*]condi2 := UIA.CreateAndCondition(UIA.CreatePropertyCondition(30005, "Search"), UIA.CreatePropertyCondition(30003, 50000))
[*]if search_box := page.Wait(condi1, 3000) {
[*]      search_box.Value := "Autohotkey"
[*]      if search_button := page.Wait(condi2, 3000)
[*]                search_button.Invoke()
[*]}

另外还有类似于钩子的机制可以异步监控不同种类的事件,这里展示焦点变化事件的监控:
[*]Persistent
[*]handler := UIA.AddFocusChangedEventHandler(HandleFocusChangedEvent)
[*]; 解除监控
[*]; UIA.RemoveFocusChangedEventHandler(handler)
[*]; handler := ""
[*]
[*]HandleFocusChangedEvent(this, sender){
[*]      ToolTip "焦点变化了"
[*]}

还有更多用法,留待您去发现,一个更多的用法例子:https://www.autoahk.com/archives/43359

页: [1]
查看完整版本: 【操作支持UIAutomation的窗口控件】内部集成的AutoHotKey的UIAutomation的用法