Jump to content

listing child windows


Tomb
 Share

Recommended Posts

i am trying to use WinList to list all the child windows of a parent window which is the child of a main window.

my example should list $child1 and $child2. but its not working correctly.

i tried _WinAPI_GetAncestor but still had no luck.

#Include <GUIConstants.au3>
#Include <WindowsConstants.au3>
#Include <WinAPI.au3>

$Main = GUICreate("Main GUI", 400, 400, 100, 100)
GUISetState(@SW_SHOW)

$Parent = GUICreate("Parent GUI", 300, 300, 100, 100)
GUISetState(@SW_SHOW)

$Child1 = GUICreate("", 100, 100, 0, 0)
GUISetState(@SW_SHOW)

$Child2 = GUICreate("", 100, 100, 100, 0)
GUISetState(@SW_SHOW)

DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Parent, "hwnd", $Main)
DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Child1, "hwnd", $Parent)
DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Child2, "hwnd", $Parent)


Opt("WinSearchChildren", 1)
$WinList = WinList()

For $i = 1 to $WinList[0][0]
    If _WinAPI_GetParent($WinList[$i][1]) = WinGetHandle("Parent GUI", "") then
        MsgBox(0, $WinList[0][0], $WinList[$i][1])
    EndIf
Next
Link to comment
Share on other sites

Hmmmm. Guess it's time to update the Dialog.au3 UDF on my site. I thought I had that posted.

;===============================================================================
; Function Name:   _WinGetChildren()
; Description:   
; Syntax:       
; Parameter(s):   $hParent - Hwnd of the parent window to find (Top Level) children of
; Requirement(s):   
; Return Value(s):   success - a 2 dimensional array
;                           [0] = Title of child window if any
;                           [1] = HWND of child window
;                   failure - Sets @Error to 1 if no top level child windows found
; Author(s):   SmOke_N
; Modification(s):   
; Note(s):   Keep in mind this is only going to return top level child windows
;           Needed UDF function: _WinGetParent
;           On some GUIs this function will fail by listing too many windows.
;              In that case use _WinChildList()
; Example(s):   
;===============================================================================

Func _WinGetChildren($hParent)
    Local $optWSC = Opt("WinSearchChildren", 1)
    Local $aWL = WinList()
    Opt("WinSearchChildren", $optWSC)
    Local $sHoldTitle, $sHoldHwnd
    Local $vDelim = Chr(1)
    
;~ Loop through and compare parent to found parent win
;~ If there is a match, separate for a 2 dim array return
    For $iCC = 1 To $aWL[0][0]
        If $hParent <> $aWL[$iCC][1] And (_WinGetParent($aWL[$iCC][1]) == $hParent) Then
            $sHoldTitle &= $aWL[$iCC][0] & $vDelim
            $sHoldHwnd &= $aWL[$iCC][1] & $vDelim
        EndIf
    Next
    
    If $sHoldHwnd = "" Then Return SetError(1, 0, "")
    
;~ Create 2 dimensional array
    Local $aSplitTitle = StringSplit(StringTrimRight($sHoldTitle, StringLen($vDelim)), $vDelim, 1)
    Local $aSplitHwnd = StringSplit(StringTrimRight($sHoldHwnd, StringLen($vDelim)), $vDelim, 1)
    
;~ Create 2 dimen array
    Local $avArray[$aSplitHwnd[0] + 1][2]
    $avArray[0][0] = $aSplitHwnd[0]
    $avArray[0][1] = 2

    
;~ Concat arrays to 2 dimensional array
    For $iCC = 1 To $avArray[0][0]
        $avArray[$iCC][0] = $aSplitTitle[$iCC]
        $avArray[$iCC][1] = $aSplitHwnd[$iCC]
    Next

    
    Return $avArray
EndFunc

Func _WinChildList($hWnd)
   Local $wsOpt = Opt("WinSearchChildren",1)
   Local $wArray, $sTitle = "", $sHwnd = "", $Dim
   $wArray = WinList("[CLASS:AutoIt v3 GUI]")
   If IsArray($wArray) Then
      For $I = 1 To Ubound($wArray)-1
         If _WinGetParent($wArray[$i][1]) = $hWnd Then
            $sTitle &= $wArray[$i][0] & Chr(1)
            $sHwnd &= $wArray[$i][1] & Chr(1)
         EndIf
      Next
      Local $aTitle = StringSplit(StringTrimRight($sTitle, 1), Chr(1), 2)
      Local $aHwnd = StringSplit(StringTrimRight($sHwnd, 1), Chr(1), 2)
      Local $aRtn[Ubound($aTitle)][2]
      For $I = 0 To Ubound($aTitle)-1
         $aRtn[$i][0] = $aTitle[$i]
         $aRtn[$i][1] = $aHwnd[$i]
      Next
      Opt("WinSearchChildren",$wsOpt)
      Return $aRtn
   EndIf
   Opt("WinSearchChildren",$wsOpt)
   Return SetError(1)
EndFunc

;===============================================================================
; Function Name:   _WinGetParent()
; Description:   
; Syntax:       
; Parameter(s):   $hWnd - Hwnd of the window to find the parent of
; Requirement(s):   
; Return Value(s):   success - hWnd of the parent
;                   failure - Sets @Error to 1 if there is no parent window
; Author(s):   SmOke_N
; Modification(s):   
; Note(s):   
; Example(s):   
;===============================================================================

Func _WinGetParent($hWnd)
    Local $aResult = DllCall("User32.dll", "hwnd", "GetParent", "hwnd", $hWnd)
    If IsArray($aResult) = 0 Then Return SetError(1, 0, '')
    Return $aResult[0]
EndFunc

If _WinGetChildren() fails then (as per the header note) replace it with this one. I still don't know why it fails on some (not all) of my scripts.

;===============================================================================
; Function Name:   _WinChildList()
; Description:   
; Syntax:       
; Parameter(s):   $hParent - Hwnd of the parent window (Top Level) to find children of
; Requirement(s):   
; Return Value(s):   success - a 2 dimensional array where element
;                           [0] = Title of child window if any
;                           [1] = HWND of child window
;                   failure - Sets @Error to 1 if no top level child windows found
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s):   Keep in mind this is only going to return top level child windows
;           Needed UDF function: _WinGetParent()
; Example(s):   
;===============================================================================
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Tomb

Works fine for me:

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Opt("WinSearchChildren", 1)

$Main = GUICreate("Main GUI", 400, 400, 100, 100)
GUISetState(@SW_SHOW)

$Parent = GUICreate("Parent GUI", 300, 300, 100, 100)
GUISetState(@SW_SHOW)

$Child1 = GUICreate("Child1", 200, 100, 0, 0)
GUISetState(@SW_SHOW)

$Child2 = GUICreate("Child2", 200, 100, 100, 0)
GUISetState(@SW_SHOW)

DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Parent, "hwnd", $Main)
DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Child1, "hwnd", $Parent)
DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Child2, "hwnd", $Parent)

$WinList = WinList()

For $i = 1 to $WinList[0][0]
    If _WinAPI_GetAncestor($WinList[$i][1]) = $Parent Then
        MsgBox(0, $WinList[$i][0], $WinList[$i][1])
    EndIf
Next

Do
Until GUIGetMsg() = -3

:)

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