Jump to content

How to get correctt all controls handle?


acmecd
 Share

Go to solution Solved by jdelaney,

Recommended Posts

To get all controls in window I use script from forum, but there is problem if window have control with some names script get handle from first name like that:

 

   

[25]

Button1

0x000D0446

[28]

Button1

0x000D0446

[41]

Button1

0x000D0446

[17]

Button2

0x001902B0

[18]

Button3

0x003B0210

[9]

ComboBox1

0x004D02F4

[19]

ComboBox1

0x004D02F4

[21]

ComboBox1

0x004D02F4

[30]

ComboBox1

0x004D02F4

[35]

ComboBox1

0x004D02F4

[6]

Edit1

0x0017031A

[8]

Edit1

0x0017031A

[20]

Edit1

0x0017031A

[27]

Edit1

0x0017031A

[29]

Edit1

0x0017031A

[36]

Edit1

0x0017031A

Any ideas ?

#include "Array.au3"
Opt("WinTitleMatchMode", 4)
$aArray = _WinGetControls('[CLASS:.......]')
_ArrayDisplay($aArray)


Func _WinGetControls($Title, $Text="")
Local $WndControls, $aControls, $sLast="", $n=1
$WndControls = WinGetClassList($Title, $Text)
$aControls = StringSplit($WndControls, @CRLF)
Dim $aResult[$aControls[0]+1][2]
For $i = 1 To $aControls[0]
    If $aControls[$i] <> "" Then
        If $sLast = $aControls[$i] Then 
            $n+=1
        Else
            $n=1
        EndIf
        $aControls[$i] &= $n
        $sLast = StringTrimRight($aControls[$i],1)
    EndIf
    If $i < $aControls[0] Then 
        $aResult[$i][0] = $aControls[$i]
    Else ; last item in array
        $aResult[$i][0] = WinGetTitle($Title) ; return WinTitle
    EndIf
    $aResult[$i][1] = ControlGetHandle($Title, $Text, $aControls[$i])   
Next
$aResult[0][0] = "ClassnameNN"
$aResult[0][1] = "Handle"
Return $aResult
EndFunc
Link to comment
Share on other sites

  • Solution

There is a flaw in that function.  You need to sort the array retruned from splitting WinGetClassList.

Here is a function that does the same thing, that I created (you can change to add to an array...I just use it to debug, so output to console):

#include <WinAPI.au3>
#include <array.au3>

Func Var_GetAllWindowsControls($hCallersWindow)
    ; Get all list of controls
    $sClassList = WinGetClassList($hCallersWindow)
    ; Create array
    $aClassList = StringSplit($sClassList, @CRLF, 2)
    ; Sort array
    _ArraySort($aClassList)
    _ArrayDelete($aClassList, 0)

    ; Loop
    Local $iCurrentClass = "", $iCurrentCount = 1, $iTotalCounter = 1

    For $i = 0 To UBound($aClassList) - 1
        If $aClassList[$i] = $iCurrentClass Then
            $iCurrentCount += 1
        Else
            $iCurrentClass = $aClassList[$i]
            $iCurrentCount = 1
        EndIf

        $hControl = ControlGetHandle($hCallersWindow, "", "[CLASSNN:" & $iCurrentClass & $iCurrentCount & "]")
        $text = StringRegExpReplace(ControlGetText($hCallersWindow, "", $hControl), "[\n\r]", "{@CRLF}")
        $aPos = ControlGetPos($hCallersWindow, "", $hControl)
        $sControlID = _WinAPI_GetDlgCtrlID($hControl)
        If IsArray($aPos) Then
            ConsoleWrite("Func=[Var_GetAllWindowsControls]: ControlCounter=[" & StringFormat("%3s", $iTotalCounter) & "] ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[" & StringFormat("%4s", $aPos[0]) & "] YPos=[" & StringFormat("%4s", $aPos[1]) & "] Width=[" & StringFormat("%4s", $aPos[2]) & "] Height=[" & StringFormat("%4s", $aPos[3]) & "] Text=[" & $text & "]." & @CRLF)
        Else
            ConsoleWrite("Func=[Var_GetAllWindowsControls]: ControlCounter=[" & StringFormat("%3s", $iTotalCounter) & "] ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[winclosed] YPos=[winclosed] Width=[winclosed] Height=[winclosed] Text=[" & $text & "]." & @CRLF)
        EndIf
        If Not WinExists($hCallersWindow) Then ExitLoop
        $iTotalCounter += 1
    Next
EndFunc   ;==>Var_GetAllWindowsControls
Edited by jdelaney
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

His script is adding the instance, but the instance resets back to 1 when the class is not the same as the preceeding class, which occurs a lot, since the class list array was not sorted...which will duplicate many of the return array values, while missing multiple controls.

Edited by jdelaney
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

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...