Jump to content

Recommended Posts

Posted (edited)

Hi -

How can I read text from a control by using the UIA implementation from LarsJ: https://www.autoitscript.com/forum/topic/197080-using-ui-automation-code-in-autoit/ .

Here's a code snippet:

$oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_TextControlTypeId, $pCondition1)
$oUIAutomation.CreatePropertyCondition($UIA_AutomationIdPropertyId, "error-label", $pCondition2)
$oUIAutomation.CreateAndCondition($pCondition1, $pCondition2, $pAndCondition)
If ($pAndCondition <> 0) Then

    ConsoleWrite("___1___" & @CRLF)

    Local $oText = 0, $pText = 0
    $oWindow.FindFirst($TreeScope_Descendants, $pAndCondition, $pText)
    $oText = ObjCreateInterface($pText, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
    If (IsObj($oText) = 1) Then

        ConsoleWrite("___2___" & @CRLF)

        Local $oRangeValuePattern = 0, $pRangeValuePattern = 0
        $oText.GetCurrentPattern($UIA_RangeValuePatternId, $pRangeValuePattern)
        $oRangeValuePattern = ObjCreateInterface($pRangeValuePattern, $sIID_IUIAutomationRangeValuePattern, $dtagIUIAutomationRangeValuePattern)
        If (IsObj($oRangeValuePattern) = 1) Then

            ConsoleWrite("___3___" & $oRangeValuePattern() & @CRLF)

        EndIf
    EndIf
EndIf

Steps 1 and 2 work fine. Maybe "RangeValuePattern" in step 3 is wrong?

Furthermore I would like to get text (foreground) color?

Anyone any idea?

Edited by supersonic
Posted (edited)

I'm still trying to fetch the needed information from this control (element?):

image.thumb.png.4c27fc1313b587cfa4e6a03a04c4e450.png

I need the control text (as shown in the picture "Ungültige Anmeldedetails" - can also be _different_ or _empty_). The text foreground color and control postion would also be helpful.

Here's a complete (non-working) reproducer to get control text:

#include ".\..\..\..\AUTOIT\Include\LarsJ\UIA\UIAIncludes\20200208\CUIAutomation2.au3"


Sleep(3000) ; Time to give window focus.
Local $hWnd = WinGetHandle("[ACTIVE]")
ConsoleWrite("___" & $hWnd & @CRLF)


Local $oUIAutomation = ObjCreateInterface($sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtagIUIAutomation)
If (IsObj($oUIAutomation) = 1) Then


    Local $pWindow = 0
    $oUIAutomation.ElementFromHandle($hWnd, $pWindow)
    If ($pWindow <> 0) Then


        Local $oWindow = ObjCreateInterface($pWindow, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
        If (IsObj($oWindow) = 1)  Then


            Local $pCondition = 0
            $oUIAutomation.CreatePropertyCondition($UIA_ClassNamePropertyId, "Internet Explorer_Server", $pCondition)
            If ($pCondition <> 0) Then


                Local $oInternetExplorerServer = 0, $pInternetExplorerServer = 0
                $oWindow.FindFirst($TreeScope_Descendants, $pCondition, $pInternetExplorerServer)
                $oInternetExplorerServer = ObjCreateInterface($pInternetExplorerServer, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
                If (IsObj($oInternetExplorerServer) = 1) Then


                    Local $pAndCondition = 0, $pCondition1 = 0, $pCondition2 = 0
                    $oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_TextControlTypeId, $pCondition1)
                    $oUIAutomation.CreatePropertyCondition($UIA_AutomationIdPropertyId, "error-label", $pCondition2)
                    $oUIAutomation.CreateAndCondition($pCondition1, $pCondition2, $pAndCondition)
                    If ($pAndCondition <> 0) Then


                        ConsoleWrite("___1___" & @CRLF)


                        Local $oText = 0, $pText = 0
                        $oWindow.FindFirst($TreeScope_Descendants, $pAndCondition, $pText)
                        $oText = ObjCreateInterface($pText, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
                        If (IsObj($oText) = 1) Then


                            ConsoleWrite("___2___" & @CRLF)


                            Local $oPattern = 0, $pPattern = 0
                            $oText.GetCurrentPattern($UIA_TextPatternId, $pPattern)
                            $oPattern = ObjCreateInterface($pPattern, $sIID_IUIAutomationTextPattern, $dtagIUIAutomationTextPattern)
                            If (IsObj($oPattern) = 1) Then


                                ConsoleWrite("___3___" & $oPattern.CurrenValue() & @CRLF)


                            EndIf
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    EndIf
EndIf

Any help greatly appreciated.

Edited by supersonic
Posted

I've found in LarsJ examples a way to retrieve control position:

; ...

ConsoleWrite("___2___" & @CRLF)

Local $hNativeWindowHandle1
$oText.GetCurrentPropertyValue($UIA_NativeWindowHandlePropertyId, $hNativeWindowHandle1)
ConsoleWrite("$hNativeWindowHandle1 = " & $hNativeWindowHandle1 & @CRLF)
Local $asBoundingRectangle1
$oText.GetCurrentPropertyValue($UIA_BoundingRectanglePropertyId, $asBoundingRectangle1)
UIA_GetArrayPropertyValueAsString($asBoundingRectangle1)
ConsoleWrite("$asBoundingRectangle1 = " & $asBoundingRectangle1 & @CRLF)

; ...

... with no luck - returning:

$hNativeWindowHandle1 = 0
$asBoundingRectangle1 = 0,0,0,0

Posted

You can obtain the information this way:

#include "UIA_Constants.au3" ; Can be copied from UIASpy Includes folder
#include "UIA_Functions.au3" ; Can be copied from UIASpy Includes folder

; --- Find window/control ---

ConsoleWrite( "--- Find window/control ---" & @CRLF )

Local $pCondition0
$oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "error-label", $pCondition0 ) ; $UIA_AutomationIdPropertyId indentifies control
If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF )
ConsoleWrite( "$pCondition0 OK" & @CRLF )

Local $pText1, $oText1
$oParent.FindFirst( $TreeScope_Descendants, $pCondition0, $pText1 )                                ; Find text control
$oText1 = ObjCreateInterface( $pText1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
If Not IsObj( $oText1 ) Then Return ConsoleWrite( "$oText1 ERR" & @CRLF )
ConsoleWrite( "$oText1 OK" & @CRLF )

; --- Element Properties ---

ConsoleWrite( "--- Element Properties ---" & @CRLF )

Local $sName1
$oText1.GetCurrentPropertyValue( $UIA_NamePropertyId, $sName1 )                                    ; $UIA_NamePropertyId extracts the text
ConsoleWrite( "$sName1 = " & $sName1 & @CRLF )

; --- Element Properties (information) ---

ConsoleWrite( "--- Element Properties (information) ---" & @CRLF )

Local $asBoundingRectangle1
$oText1.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $asBoundingRectangle1 )         ; Get bounding rect as array
UIA_GetArrayPropertyValueAsString( $asBoundingRectangle1 )                                         ; Get bounding rect as string
ConsoleWrite( "$asBoundingRectangle1 = " & $asBoundingRectangle1 & @CRLF )

You cannot get colors in a text control with UIA code.

Posted

LarsJ,

thank you! Works fine. I was already going this way (half done) :)

Isn't there any use for '$UIA_ForegroundColorAttributeId' to figure out colors? Can't find anything concret online...

Is it possible to retrieve colors by using '_WinAPI_*()' (and '$UIA_NativeWindowHandlePropertyId')?

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...