Page 1 of 1

EnumChildWindows() help.

Posted: Sun Oct 23, 2011 7:46 pm
by jassing
I expected this code to return 2 titles -- the child window ("Child") and the messagebox.
instead, it returns none.

Code: Select all

Procedure.l EnumProcedure(WindowHandle.l, Parameter.l)  
  Protected cTitle.s = Space(200)  
  GetWindowText_(WindowHandle, @cTitle, 200)
  If Len(Trim(cTitle))>0
	  Debug  Trim(cTitle)
  EndIf
	ProcedureReturn  #True ; Keep enumerating
EndProcedure  
 
Procedure test(n)
	Delay(1000) ; just wait for msgbox to display
	Debug "Enumerating child windows"
	EnumChildWindows_(WindowID(0),@EnumProcedure(), 0) 
EndProcedure

OpenWindow(0,0,0,10,10,"Parent")
OpenWindow(1,10,10,20,20,"Child",0,WindowID(0))

CreateThread(@test(),0)

MessageBox_(WindowID(1),"test","test",0)

Re: EnumChildWindows() help.

Posted: Sun Oct 23, 2011 7:58 pm
by luis
Ahhh.. yes... your concept of child window is wrong.

If we add a button...

Code: Select all


Procedure.l EnumProcedure(WindowHandle.l, Parameter.l) 
  Protected cTitle.s = Space(200) 
  GetWindowText_(WindowHandle, @cTitle, 200)
  If Len(Trim(cTitle))>0
     Debug  Trim(cTitle)
  EndIf
   ProcedureReturn  #True ; Keep enumerating
EndProcedure 

Procedure test(n)
   Delay(1000) ; just wait for msgbox to display
   Debug "Enumerating child windows"
   EnumChildWindows_(WindowID(0),@EnumProcedure(), 0)
EndProcedure

OpenWindow(0,10,10,100,100,"Parent", #PB_Window_MinimizeGadget)
ButtonGadget(2,10,10,100,20, "Button") ; <---- added a button (this is a child)
OpenWindow(1,100,100,20,20,"Child",#PB_Window_MinimizeGadget,WindowID(0))

CreateThread(@test(),0)

MessageBox_(WindowID(1),"test","test",0)
API docs wrote: The EnumChildWindows function does not enumerate top-level windows owned by the specified window, nor does it enumerate any other owned windows
The button is enumerated, so that's the reason.

I looked at the actual styles and the only child of the first window is the button.

The confusion is probably generated by this:

Code: Select all

OpenWindow(1,10,10,20,20,"Child",0,WindowID(0))

The last param means window 0 is the "owner" of window 1, not that window 1 is the "child" of window 0.


To confirm this, look at the help of CreateWindowEx_()
API docs wrote: hWndParent

Identifies the parent or owner window of the window being created. A valid window handle must be supplied when a child window or an owned window is created. A child window is confined to the client area of its parent window. An owned window is an overlapped window that is destroyed when its owner window is destroyed or hidden when its owner is minimized; it is always displayed on top of its owner window. Although this parameter must specify a valid handle if the dwStyle parameter includes the WS_CHILD style, it is optional if dwStyle includes the WS_POPUP style.

Re: EnumChildWindows() help.

Posted: Sun Oct 23, 2011 8:46 pm
by luis

Code: Select all


Procedure.l EnumProcedure(WindowHandle.l, Parameter.l) 
  Protected cTitle.s = Space(200) 
  
  If GetWindow_(WindowHandle, #GW_OWNER) = Parameter
    GetWindowText_(WindowHandle, @cTitle, 200)
    If Len(Trim(cTitle))>0
        Debug  Trim(cTitle)
    EndIf
  EndIf
  
  ProcedureReturn  #True ; Keep enumerating
EndProcedure 

Procedure test(n)
   Delay(1000) ; just wait for msgbox to display
   Debug "Enumerating owned windows"
   EnumWindows_(@EnumProcedure(), WindowID(0))
EndProcedure

OpenWindow(0,10,10,100,100,"Parent", #PB_Window_MinimizeGadget)
OpenWindow(1,100,100,20,20,"Owned",#PB_Window_MinimizeGadget,WindowID(0))

CreateThread(@test(),0)

MessageBox_(WindowID(1),"test","test",0)


This code enumerates "Owned" which is the only window who has window 0 as its owner.

The message box is certainly not a child (like the Button was) and window 0 it's not its owner either.

Hope this helps.

Re: EnumChildWindows() help.

Posted: Mon Oct 24, 2011 7:15 pm
by jassing
Thank you for the lesson!