;--------------------------------------
; 利用ACC和UIA获取文本 By FeiYue
; 实现后台获取文本,F6是初始化可以根据情况写入参数,F7是ACC后台获取 ,F9是UIA后台获取
;--------------------------------------
; F9:: Reload
F6::
MouseGetPos, mouseX, mouseY, id, , 2
x:=mouseX,y:=mouseY
UIA预备:
{
;参数:id,x,y
ifWinNotExist, ahk_id %id%,, return
WinActivate, ahk_id %id%
WinGetPos, winx, winy,winWidth,winHeight,ahk_id %id%
x:=x+winx,y:=y+winy
UIA:=ComObjCreate("{ff48dba4-60ef-4201-aa87-54103eef594e}"
, "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
, h:=DllCall("LoadLibrary", "Str","oleaut32.dll")
pos:=(x=""||y="" ? 0*DllCall("GetCursorPos","Int64*",pt)+pt : x&0xFFFFFFFF|y<<32)
id:=DllCall("WindowFromPoint", "Int64",pos)
SendMessage, WM_GETOBJECT:=0x3D, 0, 1,, ahk_id %id%
DllCall(NumGet(NumGet(UIA+0)+7*A_PtrSize), "Ptr",UIA, "Int64",pos, "Ptr*",Element)
}
ToolTip,获取了id
return
F7::
; arr:=ACC获取文本()
arr:=后台acc获取文本(id,mousex,mousey)
MsgBox 4096,, % "Name --> " arr.Name "`n`nText --> " arr.Value
return
F8::
arr:=UIA获取文本()
; arr:=后台UIA获取文本(id,mouseX,mouseY)
MsgBox 4096,, % "Name --> " arr.Name "`n`nText --> " arr.Value
return
F9::
后台UIA获取文本:
{
arr:=[], VarSetCapacity(var, 8+2*A_PtrSize), NumPut(8, var, "Short")
For k, v in { "Name":30005, "Value":30045 }
{
DllCall(NumGet(NumGet(Element+0)+10*A_PtrSize),"Ptr",Element,"Int",v,"Ptr",&var)
arr[k]:=StrGet(pstr:=NumGet(var, 8, "Ptr"), "utf-16")
DllCall("oleaut32\SysFreeString", "Ptr",pstr)
}
MsgBox 4096,, % "Name --> " arr.Name "`n`nText --> " arr.Value
}
return
;=========================================================================================|
;=========================================================================================|
;=========================================================================================|
UIA获取文本(x="", y="")
{
static UIA:=ComObjCreate("{ff48dba4-60ef-4201-aa87-54103eef594e}"
, "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
, h:=DllCall("LoadLibrary", "Str","oleaut32.dll")
pos:=(x=""||y="" ? 0*DllCall("GetCursorPos","Int64*",pt)+pt : x&0xFFFFFFFF|y<<32)
id:=DllCall("WindowFromPoint", "Int64",pos)
SendMessage, WM_GETOBJECT:=0x3D, 0, 1,, ahk_id %id%
;-- UIA.ElementFromPoint == 7
DllCall(NumGet(NumGet(UIA+0)+7*A_PtrSize), "Ptr",UIA, "Int64",pos, "Ptr*",Element)
arr:=[], VarSetCapacity(var, 8+2*A_PtrSize), NumPut(8, var, "Short")
For k, v in { "Name":30005, "Value":30045 }
{
;-- Element.GetCurrentPropertyValue == 10
DllCall(NumGet(NumGet(Element+0)+10*A_PtrSize),"Ptr",Element,"Int",v,"Ptr",&var)
arr[k]:=StrGet(pstr:=NumGet(var, 8, "Ptr"), "utf-16")
DllCall("oleaut32\SysFreeString", "Ptr",pstr)
}
ObjRelease(Element)
return arr
}
ACC获取文本(x="", y="", ByRef Acc="", ByRef child="")
{
static h:=DllCall("LoadLibrary", "Str","oleacc.dll")
pos:=(x=""||y="" ? 0*DllCall("GetCursorPos","Int64*",pt)+pt : x&0xFFFFFFFF|y<<32)
id:=DllCall("WindowFromPoint", "Int64",pos)
SendMessage, WM_GETOBJECT:=0x3D, 0, 1,, ahk_id %id%
arr:=[], Acc:=child:="", VarSetCapacity(var,8+2*A_PtrSize,0)
if DllCall("oleacc\AccessibleObjectFromPoint","Int64",pos,"Ptr*",pacc,"Ptr",&var)=0
{
Acc:=ComObjEnwrap(9,pacc,1), child:=NumGet(var,8,"UInt")
Try arr["Name"]:=Acc.accName(child)
Try arr["Value"]:=Acc.accValue(child)
return arr
}
}
;窗口ID和相对窗口坐标,不能最小化
后台ACC获取文本(id, x, y)
{
static tab:=[]
if !(Acc:=tab[id "|" x "|" y])
{
ifWinNotExist, ahk_id %id%,, return
WinActivate, ahk_id %id%
WinGetPos, winx, winy,winWidth,winHeight,ahk_id %id%
; x:=x+winx,y:=y+winy
ACC获取文本(winx+x, winy+y, Acc)
tab[id "|" x "|" y]:=Acc
}
arr:=[]
Try arr["Name"]:=Acc.accName(0)
Try arr["Value"]:=Acc.accValue(0)
return arr
}
;=======================================
;窗口ID和相对窗口坐标,不能最小化
后台UIA获取文本(id, x, y)
{
static Tab2:=[]
; if !(UIA:=Tab2[id "|" x "|" y])
; {
; WinGet, aid, ID, A
; ifWinNotExist, ahk_id %id%,, return
; WinRestore
; WinGetPos, winx, winy
; WinMove, 0, 0
; WinActivate
; Tab2[id "|" x "|" y]:=UIA获取文本(x, y)
; WinActivate, ahk_id %aid%
; WinMove, winx, winy
; }
arr:=[], VarSetCapacity(var, 8+2*A_PtrSize), NumPut(8, var, "Short")
For k, v in { "Name":30005, "Value":30045 }
{
;-- Element.GetCurrentPropertyValue == 10
DllCall(NumGet(NumGet(Element+0)+10*A_PtrSize),"Ptr",Element,"Int",v,"Ptr",&var)
arr[k]:=StrGet(pstr:=NumGet(var, 8, "Ptr"), "utf-16")
DllCall("oleaut32\SysFreeString", "Ptr",pstr)
}
return arr
}
写的真不错,学到了!