EnumChildWindows() help.

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

EnumChildWindows() help.

Post 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)
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: EnumChildWindows() help.

Post 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.
Last edited by luis on Mon Oct 24, 2011 1:17 pm, edited 2 times in total.
"Have you tried turning it off and on again ?"
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: EnumChildWindows() help.

Post 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.
"Have you tried turning it off and on again ?"
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: EnumChildWindows() help.

Post by jassing »

Thank you for the lesson!
Post Reply