Jump to content

Get controlID for ControlGetText with RegExp


Recommended Posts

I am attempting to get the text of a control who's ID changes whenever you open the application. Fortunately, I was able to write a regular expression that will match to the control's ID because it is always very similar, and only differs by a few characters each time. The problem is, StringRegExp needs test strings in order to match. Is there a way to iterate through all of the controls in a window or perhaps some other way I can get the control ID I need?

Link to comment
Share on other sites

There are probably better ways than position.  It's possible that different resolutions will cause a diff position for the control.  Or it might end up working on one computer and not another.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

 Reviewing the array returned from the _CtrlInfo() function, note the handle and the ClassnameNN of each control are unique within the one window.
The controls on the AutoIt Help window, show duplicate control id's.   Provided all the tabs have been opened at least once.

You can also use the "AU3Info" tool under SciTE > Tools menu to check the script's returned array for accuracy.
 

#include <Array.au3>
#include <WinAPIDlg.au3>

; http://www.autoitscript.com/forum/index.php?s=&showtopic=86287&view=findpost&p=619295
; Be certain AutoIt Help is running. (If all tabs are clicked on before running, I get 38 controls present.)

Local $WinTitle = "AutoIt Help" ;

$aInfo = _CtrlInfo($WinTitle) ; Sort Array method
_ArrayToClip($aInfo)
ConsoleWrite(ClipGet() & @CRLF) ; For comparison of $aInfo2 array. They should be the same.
_ArrayDisplay($aInfo, "ArrayDisplay", "", 0, 8, "Ctrl HWnd|ClassnameNN|Ctrl id|Ctrl class")

$aInfo2 = _CtrlInfoB($WinTitle) ; Unsorted method
_ArrayDisplay($aInfo2, "ArrayDisplay", "", 0, Default, "Ctrl HWnd|ClassnameNN|Ctrl id|Ctrl class")


Func _CtrlInfo($WinTitle)
    Local $iCount = 0
    $class = StringStripWS(WinGetClassList($WinTitle, ""), 2) ;   $STR_STRIPTRAILING (2) = strip trailing white space
    $aClass = StringSplit($class, @LF, 2) ; $STR_NOCOUNT (2) = disable the return count in the first element - effectively makes the array 0-based.
    Local $aArray[UBound($aClass)][4] ; New 2D array.

    ; ================ Get ClassnameNN ==================
    _ArraySort($aClass)
    For $i = 0 To UBound($aClass) - 1
        $iCount = (($i = 0 Or $aClass[$i] == $aClass[$i - 1]) ? $iCount + 1 : 1)
        $aArray[$i][1] = $aClass[$i] & $iCount                            ; ClassnameNN
    ; ============= End of Get ClassnameNN ===============

        $aArray[$i][0] = ControlGetHandle($WinTitle, "", $aArray[$i][1]) ; Handle
        $iCtrlid       = _WinAPI_GetDlgCtrlID($aArray[$i][0])
        $aArray[$i][2] = (($iCtrlid = 0) ? "" : $iCtrlid)                ; Control id
        $aArray[$i][3] = ControlGetText($WinTitle, "", $aArray[$i][1])   ; Control text
    Next
    Return $aArray
EndFunc   ;==>_CtrlInfo

Func _CtrlInfoB($WinTitle)
    Local $iInstance = 0, $sUnique = "|"
    $class = StringStripWS(WinGetClassList($WinTitle, ""), 2) ;   $STR_STRIPTRAILING (2) = strip trailing white spaces
    $aClass = StringSplit($class, @LF, 2) ; $STR_NOCOUNT (2) = disable the return count in the first element - effectively makes the array 0-based.
    Local $aArray[UBound($aClass)][4] ; New 2D array.

    ; ================ Get ClassnameNN ==================
    For $i = 0 To UBound($aClass) - 1
        $iInstance = 0
        Do
            $iInstance += 1
        Until StringInStr($sUnique, "|" & $aClass[$i] & $iInstance & "|") = 0
        $aArray[$i][1] = $aClass[$i] & $iInstance                         ; ClassnameNN
        $sUnique &= $aArray[$i][1] & "|"  ; Update $sUnique string for future comparisons (loops)
    ; ============= End of Get ClassnameNN ===============

        $aArray[$i][0] = ControlGetHandle($WinTitle, "", $aArray[$i][1]) ; Handle
        $iCtrlid       = _WinAPI_GetDlgCtrlID($aArray[$i][0])
        $aArray[$i][2] = (($iCtrlid = 0) ? "" : $iCtrlid)                ; Control id
        $aArray[$i][3] = ControlGetText($WinTitle, "", $aArray[$i][1])   ; Control text
    Next
    Return $aArray
EndFunc   ;==>_CtrlInfoB

 

Edited by Malkey
Added leading "|" to 'Until StringInStr($sUnique, "|" & $aClass[$i] & $iInstance & "|") = 0' to ensure uniqueness.
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

  • Recently Browsing   0 members

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