Jump to content

_EnumChildWindows


Ascend4nt
 Share

Recommended Posts

_EnumChildWindows

Enumerate controls/children of a Window

Posted Image

This code enumerates all controls/children of a given Window and returns them in an array.

Example code is included.

*small change 9/15/2010:

  • removed a #include line from the example code posted here (not required)
*update 3/28/2010:
  • Control ID is now retrieved. Unfortunately the indexing was reworked (sorry). But at least now I believe all the relevant information needed is retrieved.
  • Parameters to the function now have a slightly different meaning. Basically, this was done to allow searching for controls with an empty-string for a Title. All defaults are now 0, so a call can be made like this to find all Controls with a 'Button' classname:

    _EnumChildWindows($hWnd,0,0,"Button")

#include <_EnumChildWindows.au3>
; ===============================================================================================================================
; <TestEnumChildWindows.au3>
;
;   Test for _EnumChildWindows UDF.
;
; Author: Ascend4nt
; ===============================================================================================================================

; ===================================================================================================================
;   TEST
; ===================================================================================================================

#include <Array.au3>

Local $hWnd="",$hControl="",$sTitle="",$sClass="",$aEnumList
Local $iCalcPID,$hNumber1Button=-1,$hNumber4Button=-1,$hPlusButton=-1,$hEqualButton=-1
$iCalcPID=Run("calc.exe")
If @error Then Exit
$hWnd=WinWait("Calculator")

If $hWnd=0 Then Exit ProcessClose($iCalcPID)

; Important to wait for the window to fully 'create' itself before getting child windows!
; Note that other processes that become activated somewhere between _WinWaitEx and this will cause WinWaitActive() to wait for manual activation
;WinWaitActive($hWnd)   ; bad idea in busy environment
WinActivate($hWnd)  ; this seems to be a better alternative, the window seems fully created after this is called in my tests

; Parameters to function

;$hControl=HWnd(0x########)
;$sTitle="^(\d|\+|=){:content:}quot;
;$sClass="Button"

$aEnumList=_EnumChildWindows($hWnd,$hControl,$sTitle,$sClass)   ;,2) for RegExp Title
If @Error Then Exit ProcessClose($iCalcPID)

; Find specific items [Certain versions of Calc won't return any text]
For $i=1 to $aEnumList[0][0]
    Switch $aEnumList[$i][3]
        Case "1"
            $hNumber1Button=$aEnumList[$i][0]
            ConsoleWrite("'1' Advanced Mode Name (in current state): [CLASS:Button; INSTANCE:"&$aEnumList[$i][2]&"]"&@CRLF)
        Case "4"
            $hNumber4Button=$aEnumList[$i][0]
            ConsoleWrite("'4' Advanced Mode Name (in current state): [CLASS:Button; INSTANCE:"&$aEnumList[$i][2]&"]"&@CRLF)
        Case "+"
            $hPlusButton=$aEnumList[$i][0]
            ConsoleWrite("'+' Advanced Mode Name (in current state): [CLASS:Button; INSTANCE:"&$aEnumList[$i][2]&"]"&@CRLF)
        Case "="
            $hEqualButton=$aEnumList[$i][0]
            ConsoleWrite("'=' Advanced Mode Name (in current state): [CLASS:Button; INSTANCE:"&$aEnumList[$i][2]&"]"&@CRLF)
    EndSwitch
Next

; Add Headers
$aEnumList[0][0]="Handle"
$aEnumList[0][1]="Classname"
$aEnumList[0][2]="Iteration"
$aEnumList[0][3]="Title/Text"

; Bring the window forward
WinActivate($hWnd)

; Perform a simple calculation to show interaction
If $hNumber1Button<>-1 Then
    ControlClick($hWnd,"",$hNumber1Button,"primary",3)
    ControlClick($hWnd,"",$hNumber4Button)
    Sleep(1000)
    ControlClick($hWnd,"",$hPlusButton)
    Sleep(1000)
    ControlClick($hWnd,"",$hNumber4Button,"primary",2)  ; double click, but that's fine
    Sleep(1000)
    ControlClick($hWnd,"",$hEqualButton)
EndIf

; And Display ALL Enumerated Windows
_ArrayDisplay($aEnumList,"Enumerated controls for 'Calculator'")
ProcessClose($iCalcPID)

Download the ZIP from my site

Ascend4nt's AutoIT Code License agreement:

While I provide this source code freely, if you do use the code in your projects, all I ask is that:

  • If you provide source, keep the header as I have put it, OR, if you expand it, then at least acknowledge me as the original author, and any other authors I credit
  • If the program is released, acknowledge me in your credits (it doesn't have to state which functions came from me, though again if the source is provided - see #1)
  • The source on it's own (as opposed to part of a project) can not be posted unless a link to the page(s) where the code were retrieved from is provided and a message stating that the latest updates will be available on the page(s) linked to.
  • Pieces of the code can however be discussed on the threads where Ascend4nt has posted the code without worrying about further linking.
Edited by Ascend4nt
Link to comment
Share on other sites

Updated :(

*update 3/28/2010:

  • Control ID is now retrieved. Unfortunately the indexing was reworked (sorry). But at least now I believe all the relevant information needed is retrieved.
  • Parameters to the function now have a slightly different meaning. Basically, this was done to allow searching for controls with an empty-string for a Title. All defaults are now 0, so a call can be made like this to find all Controls with a 'Button' classname:

    _EnumChildWindows($hWnd,0,0,"Button")

Link to comment
Share on other sites

  • 5 months later...

dude u rock.

Is There A Life BEFORE Death?im stupidSaved warn logs: cheateraSmOke_N Note Added 17 January 2009 - 02:54 PM Added to warn level Posting a way to hack the registry and force sending someones desktop via TCP. Jos Note Added 25 November 2008 - 02:52 PM Added to warn level for being an impolite rookie.once a year i go bad ... what will happen in 2010??[u]Its GOOD to be BAD ... (Warlock's Succubus - World of Warcraft)[/u]

Link to comment
Share on other sites

wakillon, sorry - that was a remnant from the example before it was modified. It's not required for the code to run, but I realized I left that '#include' statement in there. It's removed now, however, and should run. The code in the ZIP file on my site shouldn't have it either.

Thanks for pointing it out.

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