本篇文章通过 Acc 库(Active Accessibility / MSAA 接口)实现对窗口控件的读取与操作。相比常规的控件句柄方式,Acc 可访问更多无标准句柄的交互元素(如某些第三方工具、树形/列表内的子项),是 Windows UI 自动化的重要手段。
代码要点:
• 按下 F1 获取鼠标指针下的控件信息(句柄、类名、Acc 路径 obj.path 等),并作为后续操作的对象;
• F2 / F3 通过固定的子路径 childPathPen / childPathDele 定位到目标子控件,并调用 Acc.accDoDefaultAction(0) 触发其默认动作(相当于点击);
• F6 重载脚本,便于调试。
使用前提:需要将 acc.ahk 放入脚本目录的 lib 文件夹(脚本通过 #include 引用),并按需修改两个 childPath 变量为你目标控件实际的 Acc 路径。
AHK v2 迁移提示:该脚本为 AHK v1 语法(%A_ScriptDir%、WinGetTitle 输出变量等)。v2 中脚本参数改用 Acc_Get("Object", childPath, 0, "ahk_id " id) 的表达式写法,Global 声明位置、三元表达式与字符串拼接规则亦有差异,迁移时需同步调整。
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#include %A_scriptdir%/lib/acc.ahk
global childPathPen := "4.10.1.1.3.1"
global childPathDele := "4.10.1.1.2.1.1"
F1::
obj := Acc_GetInfoUnderCursor(), id:=obj.hwnd
DetectHiddenWindows, On
WinGetTitle, tt, ahk_id %id%
WinGetClass, class, ahk_id %id%
tt:=Trim(class="" ? tt : tt " ahk_class " class)
;MsgBox, 4096,, % "id" id "-->" tt " --> " obj.path " --> " SubStr(obj.text,1,100)
return
f2::
choosebutton(childPathPen,id)
return
f3::
choosebutton(childPathDele,id)
return
choosebutton(childPath,id)
{
local
;MsgBox,% "childPath:" childPath "id:" id
Acc := Acc_Get("Object",childPath, 0, "ahk_id" id)
Acc.accDoDefaultAction(0)
}
f6::
Reload
return
;==================================================
;Acc:=Acc_Get("Object", obj.path, 0, "ahk_id " id)
;Acc.accSelect(1,0)
;MsgBox,% Acc.accValue(0)
;SendInput, {raw}{hetong}
;Acc.put_accValue(0,"value")
;Acc.accDoDefaultAction(0)


学习了
学习了
测试缺少函数,com库加上后,acc库里也没有你用的函数。。。
Acc_GetInfoUnderCursor()