AutoItとUI Automation UDFを使ってTreeWalkerする

準備

コード

#include "..\Includes\CUIAutomation2.au3"

Example()

Func Example()
  Sleep( 1000 )

  ; Create UI Automation object
  Local $oUIAutomation = ObjCreateInterface( $sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtagIUIAutomation )
  If Not IsObj( $oUIAutomation ) Then Return ConsoleWrite( "$oUIAutomation ERR" & @CRLF )
  ConsoleWrite( "$oUIAutomation OK" & @CRLF )

  ; Get Desktop element
  Local $pDesktop, $oDesktop
  $oUIAutomation.GetRootElement( $pDesktop )
  $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF )
  ConsoleWrite( "$oDesktop OK" & @CRLF )

  ; ---  window ---

  Local $pTreeWalker, $oTreeWalker
  $oUIAutomation.RawViewWalker($pTreeWalker)
;  $oUIAutomation.ControlViewWalker($pTreeWalker)
;  $oUIAutomation.ContentViewWalker($pTreeWalker)
  $oTreeWalker = ObjCreateInterface($pTreeWalker, $sIID_IUIAutomationTreeWalker, $dtagIUIAutomationTreeWalker)
  If Not IsObj( $oTreeWalker ) Then Return ConsoleWrite( "$oTreeWalker ERR" & @CRLF )
  ConsoleWrite( "$oTreeWalker OK" & @CRLF )

  Local $pElement,$oElement
  $oTreeWalker.GetFirstChildElement($oDesktop, $pElement)
  $oElement = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
    Local $i = 0
    While IsObj($oElement) = True
;        $oElement.SetFocus()
        Local $sValue1, $sValue2, $i,$oElementChild
        $oElement.GetCurrentPropertyValue($UIA_ClassNamePropertyId, $sValue1)
        $oElement.GetCurrentPropertyValue($UIA_NamePropertyId, $sValue2)
        ConsoleWrite("<" & $i & "> MAIN "  & _
                    "Class=" & $sValue1 & _
                    " Title=" & $sValue2 & _
                     @CRLF)
	    $oTreeWalker.GetFirstChildElement($oElement, $pElement)
	    $oElementChild = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
	    While IsObj($oElementChild) = True
	;        $oElementChild.SetFocus()
	        Local $sValue1, $i
	        $oElementChild.GetCurrentPropertyValue($UIA_ClassNamePropertyId, $sValue1)
	        $oElementChild.GetCurrentPropertyValue($UIA_NamePropertyId, $sValue2)
	        ConsoleWrite("<" & $i & "> CHILD "  & _
	                    "Class=" & $sValue1 & _
	                    " Title=" & $sValue2 & _
	                     @CRLF)

	        $oTreeWalker.GetNextSiblingElement($oElementChild, $pElement)
	        $oElementChild = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
	        Sleep(5000)
	    WEnd
		$oElementChild = 0

        $oTreeWalker.GetNextSiblingElement($oElement, $pElement)
        $oElement = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
		$i = $i + 1
		Sleep(5000)

    WEnd
EndFunc

参考

https://learn.microsoft.com/ja-jp/dotnet/framework/ui-automation/navigate-among-ui-automation-elements-with-treewalker

GitHub – exorcistas/autoit-uiautomation-udf: AutoIt UDF wrapper for MS Windows UIAutomation (UIA) layer

AutoIt

Posted by eightban