Jump to content

UIA automation event handler


ahmet
 Share

Recommended Posts

Hi,


I am trying to catch closing of a window via the UIA. End goal is to detect closing of a Chrome's tab, but currently I am having issues with the window created with GUICreate.
I have modified example from UI Automation Events .

I added GUI to show issues. Inside this GUI AutomationEventHandler is attached to all buttons. There are two buttons created by the script. One displays the message while the other closes the GUI.
There are also buttons from the title bar. I attached automation event handlers to those buttons too.
Currently script notifies me if I click on "OK" button. If I click on any of "Close", "X" or "Minimize" button then I do not get any notification.

Is it possible for the script to notify when any of the buttons mentioned above is clicked?

#include "..\Includes\UIAEH_AutomationEventHandler.au3"


$hMain=GUICreate("UI Automation Event Handler")
$idButtonOK=GUICtrlCreateButton("OK",10,10)
$idButtonClose=GUICtrlCreateButton("Close",70,10)
$idOutput=GUICtrlCreateEdit("",10,35,300,300)
GUISetState()

UIAEH_AutomationEventHandlerCreate()
If Not IsObj( $oUIAEH_AutomationEventHandler ) Then Exit ConsoleWrite( "$oUIAEH_AutomationEventHandler ERR" & @CRLF )
ConsoleWrite( "$oUIAEH_AutomationEventHandler OK" & @CRLF )
Local $oUIAutomation = ObjCreateInterface($sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtag_IUIAutomation)
;~ Local $oUIAutomation = ObjCreateInterface( $sCLSID_CUIAutomation8, $sIID_IUIAutomation6, $dtag_IUIAutomation6 )
If Not IsObj($oUIAutomation) Then Exit ConsoleWrite("$oUIAutomation ERR" & @CRLF)
ConsoleWrite("$oUIAutomation OK" & @CRLF)

Local $pIUIAutomationCacheRequest, $oIUIAutomationCacheRequest
$oUIAutomation.CreateCacheRequest( $pIUIAutomationCacheRequest )
$oIUIAutomationCacheRequest = ObjCreateInterface( $pIUIAutomationCacheRequest, $sIID_IUIAutomationCacheRequest, $dtag_IUIAutomationCacheRequest )
If Not IsObj( $oIUIAutomationCacheRequest ) Then Exit ConsoleWrite("$oIUIAutomationCacheRequest Error" & @CRLF)
If $oIUIAutomationCacheRequest.AddPattern($UIA_InvokePatternId) Then Exit ConsoleWrite("$oIUIAutomationCacheRequest.AddPattern Error" & @CRLF)
If $oIUIAutomationCacheRequest.put_AutomationElementMode( $AutomationElementMode_Full) Then Exit ConsoleWrite("$oIUIAutomationCacheRequest.put_AutomationElementMode Error" & @CRLF)
;~ $pIUIAutomationCacheRequest=0


ConsoleWrite("--- Find window/control ---" & @CRLF)
Local $pPane1, $oGUI
$oUIAutomation.ElementFromHandle($hMain,$pPane1)
$oGUI = ObjCreateInterface($pPane1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
If Not IsObj($oGUI) Then Exit ConsoleWrite("$oGUI ERR" & @CRLF)
ConsoleWrite("$oGUI OK" & @CRLF)
Local $sNamePropperty, $sClassName1
$oGUI.GetCurrentPropertyValue($UIA_NamePropertyId, $sNamePropperty)
ConsoleWrite("Name:" & $sNamePropperty & @CRLF)
$oGUI.GetCurrentPropertyValue($UIA_ClassNamePropertyId, $sClassName1)
ConsoleWrite("ClassName:" & $sClassName1 & @CRLF)

ConsoleWrite("--- Find buttons ---" & @CRLF)
Local $pCondition0
$oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition0)
If Not $pCondition0 Then Exit ConsoleWrite("$pCondition0 ERR" & @CRLF)
ConsoleWrite("$pCondition0 OK" & @CRLF)

Local $pButtons, $oButton
$oGUI.FindAll($TreeScope_Descendants, $pCondition0, $pButtons)
ConsoleWrite("$oGUI FindAll()" & @CRLF)

Local $oUIElementArray1, $iLength1  ; $pButtons is a pointer to an UI Automation element array
$oUIElementArray1 = ObjCreateInterface($pButtons, $sIID_IUIAutomationElementArray, $dtag_IUIAutomationElementArray)
$oUIElementArray1.Length($iLength1)
If Not $iLength1 Then Exit ConsoleWrite("$iLength1 = 0 ERR" & @CRLF)
ConsoleWrite("$iLength1 = " & $iLength1 & @CRLF)

; --- Code Snippets ---
ConsoleWrite("--- Code Snippets ---" & @CRLF)
Local $pElement1, $oElement1, $pCondition0
Local $sClassName1, $sLocalizedControlType1, $sName1, $hNativeWindowHandle1, $iError
For $i = 0 To $iLength1 - 1
    $oUIElementArray1.GetElement($i, $pElement1)
    $oElement1 = ObjCreateInterface($pElement1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
    $oElement1.GetCurrentPropertyValue($UIA_NamePropertyId, $sName1)
    ConsoleWrite("$sName1 = " & $sName1 & @CRLF)

;~  $iError = $oUIAutomation.AddAutomationEventHandler( $UIA_Invoke_InvokedEventId, $pElement1, $TreeScope_Children, 0, $oUIAEH_AutomationEventHandler )
;~  $iError = $oUIAutomation.AddAutomationEventHandler( $UIA_Invoke_InvokedEventId, $pElement1, $TreeScope_Element, 0, $oUIAEH_AutomationEventHandler )
    $iError = $oUIAutomation.AddAutomationEventHandler( $UIA_Invoke_InvokedEventId, $pElement1, $TreeScope_Element, $pIUIAutomationCacheRequest, $oUIAEH_AutomationEventHandler )
    If $iError Then Exit ConsoleWrite( "AddAutomationEventHandler() ERR" & @CRLF )
    ConsoleWrite( "AddAutomationEventHandler() OK" & @CRLF )
Next


While Sleep(10)
    Switch GUIGetMsg()
        Case -3, $idButtonClose
            GUIDelete($hMain)
            MsgBox(0,"About to exit","")
            Quit()
        Case $idButtonOK
            GUICtrlSetData($idOutput,"You clicked on button" & @CRLF,"a")
    EndSwitch
WEnd

Func Quit()
    UIAEH_AutomationEventHandlerDelete()
    Exit
EndFunc

; This is the function that receives events
Func UIAEH_AutomationEventHandler_HandleAutomationEvent( $pSelf, $pSender, $iEventId ) ; Ret: long  Par: ptr;int
    ConsoleWrite( @CRLF & "UIAEH_AutomationEventHandler_HandleAutomationEvent: $iEventId = " & $iEventId & @CRLF )
;~  Local $oSender = ObjCreateInterface( $pSender, $sIID_IUIAutomationElement,  $dtag_IUIAutomationElement  ) ; Windows 7
    ;Local $oSender = ObjCreateInterface( $pSender, $sIID_IUIAutomationElement2, $dtag_IUIAutomationElement2 ) ; Windows 8
    ;Local $oSender = ObjCreateInterface( $pSender, $sIID_IUIAutomationElement3, $dtag_IUIAutomationElement3 ) ; Windows 8.1
    ;Local $oSender = ObjCreateInterface( $pSender, $sIID_IUIAutomationElement4, $dtag_IUIAutomationElement4 ) ; Windows 10 First
    Local $oSender = ObjCreateInterface( $pSender, $sIID_IUIAutomationElement9, $dtag_IUIAutomationElement9 ) ; Windows 10 Last
    $oSender.AddRef()
    GUICtrlSetData($idOutput,"InvokedEvent:" & UIAEH_GetCurrentPropertyValue( $oSender, $UIA_NamePropertyId ) & @CRLF,"a")
    ConsoleWrite( "Title     = " & UIAEH_GetCurrentPropertyValue( $oSender, $UIA_NamePropertyId ) & @CRLF & _
                  "Class     = " & UIAEH_GetCurrentPropertyValue( $oSender, $UIA_ClassNamePropertyId ) & @CRLF & _
                  "Ctrl type = " & UIAEH_GetCurrentPropertyValue( $oSender, $UIA_ControlTypePropertyId ) & @CRLF & _
                  "Ctrl name = " & UIAEH_GetCurrentPropertyValue( $oSender, $UIA_LocalizedControlTypePropertyId ) & @CRLF & _
                  "Handle    = " & "0x" & Hex( UIAEH_GetCurrentPropertyValue( $oSender, $UIA_NativeWindowHandlePropertyId ) ) & @CRLF & _
                  "Value     = " & UIAEH_GetCurrentPropertyValue( $oSender, $UIA_ValueValuePropertyId ) & @CRLF  )
    MsgBox(0,"","Invoke event")
    Return 0x00000000 ; $S_OK
    #forceref $pSelf
EndFunc

; Auxiliary function (for simple properties only)
; There must be only one instance of this function
Func UIAEH_GetCurrentPropertyValue( $oSender, $iPropertyId )
    Local $vPropertyValue
    $oSender.GetCurrentPropertyValue( $iPropertyId, $vPropertyValue )
    Return $vPropertyValue
EndFunc

 

Link to comment
Share on other sites

What do you see happening within the tool UI Automation Event Monitor you gave in the link?
Probably not when its clicked but you can catch the closing/minimize events of the tab/window.

You should be able to catch below

UIA_Window_WindowClosedEventId
20017
Identifies the event that is raised when a window is closed.

Over here one of the first examples in the forum

 

Otherwise look for hooking the mouse https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_SetWindowsHookEx.htm

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...