eightban's memo

残しておきたい記事をまとめてみました。このブログに書いてあるドキュメントやブログで配布しているファイルの使用によって発生するいかなる損害に対してもこのブログの管理者は責任を負いません。使用する場合は自己責任のもとに使用してください。

AutoIt

AutoItとUI Automation UDFを使ってポップアップウィンドウのボタンにマウスカーソルを移動させる 複数ボタン対応版

更新日:

準備

この機能は設定しておいてください。対応できないボタンを対応します

コード

メモ帳 ペイント One Drive クリップボード履歴 通常のウィンドウに対応します

色々なアプリに対応するのは難しいですね

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include "..\UIAIncludes\Includes\CUIAutomation2.au3"
Global Const $UIA_IsDialogPropertyId                            = 30174 ; Windows 10

Global $_title_1 =  "btnuia"
Global $cnt = 0
Global $cnt2 = 0

;[CLASS:Windows.UI.Core.CoreWindow; INSTANCE:0]
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
$mainwindow = GUICreate($_title_1, 70, 70)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
$msg1="stopping"
$label1=GUICtrlCreateLabel($msg1, 10, 10)
$button1 = GUICtrlCreateButton("push", 10, 20,50, 50)
GUICtrlSetOnEvent($button1, "OKButton")
GUISetState(@SW_SHOW)

Global $Paused
;

HotKeySet("{Esc}", "_Exit") ; Press ESC for exit
Func _Exit()
    Exit 0
EndFunc   ;==>_Exit

if    WinWait($_title_1, "",3) Then
		sleep(50)
    ControlClick ($_title_1,"","Button1")
	sleep(50)
	GUISetState(@SW_MINIMIZE)

EndIf


While 1
  Sleep(1000)  ; Idle around
  if $msg1="stopping"  Then
  Else
    _chk()
	Sleep(5000)
  EndIf
WEnd

Func OKButton()
;  MsgBox(0, "GUI Event", "You pressed OK!")
if $msg1="stopping"  Then
  $msg1="executing"

Else
  $msg1="stopping"
EndIf
GUICtrlSetData ($label1, $msg1)


EndFunc

Func CLOSEClicked()
 ; MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
Exit
EndFunc



;===========================
Func _chk()

  ; 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 )

  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 )

  ; 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 )

  ; --- Notepad window ---

  ConsoleWrite( "---  window ---" & @CRLF )

  Local $pCondition
  Local $pCondition1
  Local $pCondition2
  Local $pCondition0
  Local $pElements
#comments-start

  $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_WindowControlTypeId, $pCondition )

  If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF )
  ConsoleWrite( "$pCondition OK" & @CRLF )
#comments-end
  ; --- $oUIAutomation methods ---

  ConsoleWrite( "--- $oUIAutomation methods ---" & @CRLF )

  Local $pCondition
  $oUIAutomation.CreateTrueCondition( $pCondition )
  ConsoleWrite( "$oUIAutomation.CreateTrueCondition()" & @CRLF )

  ; --- $oUIElement methods ---

  ConsoleWrite( "--- $oUIElement methods ---" & @CRLF )

  Local $pElements
  $oDesktop.FindAll( $TreeScope_Children, $pCondition, $pElements )
  ConsoleWrite( "$oDesktop.FindAll()" & @CRLF )

  ; --- Code Snippets ---

  ConsoleWrite( "--- Code Snippets ---" & @CRLF )

  Local $oUIElementArray1, $iLength1 ; $pElements is a pointer to an UI Automation element array
  $oUIElementArray1 = ObjCreateInterFace( $pElements, $sIID_IUIAutomationElementArray, $dtagIUIAutomationElementArray )
  $oUIElementArray1.Length( $iLength1 )
  If Not $iLength1 Then Return ConsoleWrite( "$iLength1 = 0 ERR" & @CRLF )
  ConsoleWrite( "$iLength1 = " & $iLength1 & @CRLF )

  ; --- Code Snippets ---

  ConsoleWrite( "--- Code Snippets ---" & @CRLF )

  Local $pElement1, $oElement1, $sValue1, $sValue2, $sValue3, $sValue4, $sValue5, $sValue6
  For $i = 0 To $iLength1 - 1

    $oUIElementArray1.GetElement( $i, $pElement1 )
    $oElement1 = ObjCreateInterface( $pElement1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )

	If Not IsObj( $oElement1 ) Then
	  ConsoleWrite( "$oElement1 ERR" & @CRLF )
      ContinueLoop
 	EndIf


;  ConsoleWrite( "$oElement1 OK" & @CRLF )
    $oElement1.GetCurrentPropertyValue( $UIA_ClassNamePropertyId, $sValue1 ) ; $UIA_ClassNamePropertyId is used as example
;    ConsoleWrite( "$sValue1 = " & $sValue1 & @CRLF )
#comments-start

	Select
        Case $sValue1 = "Notepad"
        Case $sValue1 = "QQuickView"
        Case $sValue1 = "ApplicationFrameWindow"
;        Case $sValue1 = ""
            ConsoleWrite( "$sValue1 = " & $sValue1 & @CRLF )
		Case Else
			ContinueLoop
	EndSelect
#32770
	If $sValue1 = "Shell_TrayWnd" Then ContinueLoop
	If $sValue1 = "TrayNotifyWnd" Then ContinueLoop
	If $sValue1 = "TextBlock" Then ContinueLoop
	If $sValue1 = "Image" Then ContinueLoop
	If $sValue1 = "Button" Then ContinueLoop
	If $sValue1 = "ToggleButton" Then ContinueLoop
	If $sValue1 = "" Then ContinueLoop
	If $sValue1 = Null Then ContinueLoop

	If StringInStr($sValue1, "SystemTray.") Then ContinueLoop
	If StringInStr($sValue1, "Taskbar.") Then ContinueLoop
#comments-end
	If $sValue1 = "Shell_TrayWnd" Then ContinueLoop
	If $sValue1 = "TaskListThumbnailWnd" Then ContinueLoop
	If $sValue1 = "Progman" Then ContinueLoop

    ConsoleWrite( "$sValue1 = " & $sValue1 & @CRLF )
    $oElement1.GetCurrentPropertyValue( $UIA_ControlTypePropertyId, $sValue5 )
    $oElement1.GetCurrentPropertyValue( $UIA_WindowIsTopmostPropertyId, $sValue4 )
    $oElement1.GetCurrentPropertyValue( $UIA_IsDialogPropertyId, $sValue3 )

    $oElement1.GetCurrentPropertyValue( $UIA_NamePropertyId, $sValue2 )
    ConsoleWrite( "$sValue2 = " & StringLeft($sValue2, 30) & @CRLF )
;	If $sValue2 = "" Then ContinueLoop
;	If $sValue2 = Null Then ContinueLoop
;    ConsoleWrite( "$sValue3 = " & $sValue3 & @CRLF )

	If $sValue2 = "Microsoft OneDrive" Then

  		$oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "了解しました", $pCondition )
  		If  $pCondition Then
			  ConsoleWrite( "$pCondition OK" & @CRLF )

 		 Local $pSave, $oSave
  			$oElement1.FindFirst( $TreeScope_Descendants, $pCondition, $pSave )
			$oSave = ObjCreateInterface( $pSave, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  			If Not IsObj( $oSave ) Then
	  			ConsoleWrite( "$oSave ERR" & @CRLF )
      			ContinueLoop
  			EndIf
  			ConsoleWrite( "$oSave OK" & @CRLF )
		_osu($oSave)
  		EndIf
	ElseIf $sValue1 = "#32770" Then
		  _Btn($oTreeWalker, $oElement1 )
	ElseIf $sValue3 = true Then

  		$oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_GroupControlTypeId, $pCondition )
  		If  $pCondition Then
			  ConsoleWrite( "$pCondition OK" & @CRLF )

 		 Local $pSave, $oSave, $pGroup , $oGroup
  			$oElement1.FindFirst( $TreeScope_Descendants, $pCondition, $pGroup )
			$oGroup = ObjCreateInterface( $pGroup, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  			If Not IsObj( $oGroup ) Then
	  			ConsoleWrite( "$oGroup ERR" & @CRLF )
      			ContinueLoop
  			EndIf
  			ConsoleWrite( "$oGroup OK" & @CRLF )
		  _Btn($oTreeWalker, $oGroup )
  		EndIf

	ElseIf $sValue1 = "XamlExplorerHostIslandWindow" Then

  		$oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "ListView", $pCondition )
  		If  $pCondition Then
			  ConsoleWrite( "$pCondition OK" & @CRLF )

 		 Local $pSave, $oSave, $pGroup , $oGroup
  			$oElement1.FindFirst( $TreeScope_Descendants, $pCondition, $pSave )
			$oSave = ObjCreateInterface( $pSave, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  			If Not IsObj( $oSave ) Then
	  			ConsoleWrite( "$oSave ERR" & @CRLF )
      			ContinueLoop
  			EndIf
  			ConsoleWrite( "$oSave OK" & @CRLF )
		  _Btn($oTreeWalker, $oSave )
  		EndIf

	ElseIf $sValue1 = "ApplicationFrameWindow" Then
		If $sValue4  = true Then		_osu($oElement1)
#comments-start
	ElseIf $sValue5 = $UIA_WindowControlTypeId Then

  		$oUIAutomation.CreatePropertyCondition( $UIA_IsDialogPropertyId, true, $pCondition )
  		$oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_GroupControlTypeId, $pCondition0 )
  		If  $pCondition Then
			  ConsoleWrite( "$pCondition OK" & @CRLF )

  		If  $pCondition0 Then
			  ConsoleWrite( "$pCondition0 OK" & @CRLF )

 		 Local $pSave, $oSave, $pGroup , $oGroup
  			$oElement1.FindFirst( $TreeScope_Descendants, $pCondition, $pSave )
			$oSave = ObjCreateInterface( $pSave, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  			If Not IsObj( $oSave ) Then
	  			ConsoleWrite( "$oSave ERR" & @CRLF )
      			ContinueLoop
  			EndIf
  			ConsoleWrite( "$oSave OK" & @CRLF )
  			$oSave.FindFirst( $TreeScope_Descendants, $pCondition0, $pGroup )
			$oGroup = ObjCreateInterface( $pGroup, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  			If Not IsObj( $oGroup ) Then
	  			ConsoleWrite( "$oGroup ERR" & @CRLF )
      			ContinueLoop
  			EndIf
  			ConsoleWrite( "$oGroup OK" & @CRLF )
		  _Btn($oTreeWalker, $oGroup )
  		EndIf
  		EndIf
#comments-end

	ElseIf $sValue1 = "gdkWindowToplevel" Then
	;	$oElement1.SetFocus()

	;	_osu($oElement1)
	Else

;		ConsoleWrite( "$pCondition ERR" & @CRLF )

  ; --- Save As window ---

		ConsoleWrite( "--- Save As window ---" & @CRLF )

  ; Note that $UIA_ClassNamePropertyId maybe ia a CASE SENSITIVE condition
  		$oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Popup", $pCondition1 )
  		If Not $pCondition1 Then
			  ConsoleWrite( "$pCondition1 ERR" & @CRLF )
		      ContinueLoop
		EndIf
		ConsoleWrite( "$pCondition1 OK" & @CRLF )

		  $oUIAutomation.CreatePropertyCondition( $UIA_WindowIsTopmostPropertyId, true, $pCondition2 )
		  If Not $pCondition2 Then
			  ConsoleWrite( "$pCondition2 ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$pCondition2 OK" & @CRLF )

		  ; And condition
		  $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition )
		  If Not $pCondition Then
			  ConsoleWrite( "$pCondition ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$pCondition OK" & @CRLF )

		  $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "#32770", $pCondition0 )
		  If Not $pCondition0 Then
			  ConsoleWrite( "$pCondition0 ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$pCondition0 OK" & @CRLF )


		  Local $pSaveAsWin, $oSaveAsWin
		  $oElement1.FindFirst( $TreeScope_Descendants, $pCondition, $pSaveAsWin )
		  $oSaveAsWin = ObjCreateInterface( $pSaveAsWin, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
		  If Not IsObj( $oSaveAsWin ) Then
			  ConsoleWrite( "$oSaveAsWin ERR" & @CRLF )
		  	  $oElement1.FindFirst( $TreeScope_Descendants, $pCondition0, $pSaveAsWin )
		 	  $oSaveAsWin = ObjCreateInterface( $pSaveAsWin, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
		  	  If Not IsObj( $oSaveAsWin ) Then
			 	   ConsoleWrite( "$oSaveAsWin ERR" & @CRLF )
		      	  ContinueLoop
		  	  EndIf
		  EndIf
		  ConsoleWrite( "$oSaveAsWin OK" & @CRLF )
		  $oSaveAsWin.GetCurrentPropertyValue( $UIA_NamePropertyId, $sValue2 )
          	If StringInStr($sValue2, "開く")  Then Sleep(20000)
          	If StringInStr($sValue2, "保存")  Then Sleep(20000)

		  _Btn($oTreeWalker, $oSaveAsWin )


#comments-start

		  ConsoleWrite( "--- Save Button ---" & @CRLF )

		  $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition1 )
		  If Not $pCondition1 Then
			  ConsoleWrite( "$pCondition1 ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$pCondition1 OK" & @CRLF )

		  $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Button", $pCondition2 )
		  If Not $pCondition2 Then
			  ConsoleWrite( "$pCondition2 ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$pCondition2 OK" & @CRLF )

		  ; And condition
		  $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition )
		  If Not $pCondition Then
			  ConsoleWrite( "$pCondition ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$pCondition OK" & @CRLF )
		  Local $pSave, $oSave
$UIA_PaneControlTypeId
開く
		  $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pCondition, $pSave )
		  $oSave = ObjCreateInterface( $pSave, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
		  If Not IsObj( $oSave ) Then
			  ConsoleWrite( "$oSave ERR" & @CRLF )
		      ContinueLoop
		  EndIf
		  ConsoleWrite( "$oSave OK" & @CRLF )
#comments-end
	EndIf


  Next
	ConsoleWrite( $cnt & $cnt2 & @CRLF )
	If $cnt = $cnt2 Then $cnt = 0
	$cnt2 = $cnt

EndFunc
Func _osu($oSave1)
	$pos1 = MouseGetPos()

;		$oSave1.SetFocus()
		Local $vValue, $sValue
		Local $fScale = 0
		; Rectangle
		Local $aRect ; l, t, w, h
		$oSave1.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $aRect )
		If Not IsArray( $aRect ) Then
		  ConsoleWrite( "$aRect ERR" & @CRLF )
	      Return
	    EndIf
	    ConsoleWrite( "$aRect OK" & @CRLF )

		If $fScale > 1.00 Then
			$aRect[0] = Round( $aRect[0] * $fScale )
			$aRect[1] = Round( $aRect[1] * $fScale )
			$aRect[2] = Round( $aRect[2] * $fScale )
			$aRect[3] = Round( $aRect[3] * $fScale )
		EndIf
		$sValue = ""
		If IsArray( $aRect ) And UBound( $aRect ) Then
				$sValue = $aRect[0]
				For $j = 1 To UBound( $aRect ) - 1
					$sValue &= "," & $aRect[$j]
				Next
		EndIf
		ConsoleWrite( $sValue & @CRLF )
		ConsoleWrite( $pos1[0] & "," & $pos1[1] & @CRLF )
		if $pos1[0]>=$aRect[0] AND $pos1[0]<=$aRect[0]+$aRect[2] Then
			if $pos1[1]>=$aRect[1] AND $pos1[1]<=$aRect[1]+$aRect[3] Then Return
		EndIf

		If $cnt <2 Then MouseMove( $aRect[0]+$aRect[2]/2, $aRect[1]+$aRect[3]/2 )
		Sleep(3000)

EndFunc
Func _Btn($oTreeWalker, $oSaveAsWin )

	  Local $pElement,$oElement
	  $oTreeWalker.GetFirstChildElement($oSaveAsWin, $pElement)
	  $oElement = ObjCreateInterface($pElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
	    Local $i = 0
	    Local $i2 = 0
	    While IsObj($oElement) = True
	;        $oElement.SetFocus()
	        Local $sValue1, $sValue2, $i, $sValue3, $sValue4
	        $oElement.GetCurrentPropertyValue($UIA_ClassNamePropertyId, $sValue1)
	        $oElement.GetCurrentPropertyValue($UIA_NamePropertyId, $sValue2)
	        $oElement.GetCurrentPropertyValue($UIA_LocalizedControlTypePropertyId, $sValue3)
	        ConsoleWrite("<" & $i & ">  "  & _
	                    "Class=" & $sValue1 & _
	                    " Title=" & $sValue2 & _
	                    " T=" & $sValue3 & _
	                     @CRLF)
			Select
               Case $sValue1 = "Button" or  $sValue1 = "ListViewItem"
				If $cnt <2 Then $oElement.SetFocus()
				_osu($oElement)
		        $i2 += 1
		        If $i2 = 1 Then   $cnt += 1
              Case $sValue3 = "ボタン"
				If $cnt <2 Then $oElement.SetFocus()
				_osu($oElement)
		        $i2 += 1
		        If $i2 = 1 Then   $cnt += 1

			EndSelect



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


	    WEnd

EndFunc

-AutoIt

Copyright© eightban's memo , 2024 All Rights Reserved Powered by STINGER.