GetParent() - (for Windows only)
Posted: Thu Dec 18, 2014 2:14 pm
				
				A simple function to get the number of the parent window or container of a gadget:For some reason, the GetDlgCtrlID() API function does not work with the PanelGadget() or ScrollAreaGadget(). If anyone knows how to do this, please update the code.
Thank you.
			Code: Select all
;==============================================================
;   GetParent() returns a gadget's parent type and number
;   through a delimited string - Windows only
;
;   works only if parent is a Window or ContainerGadget()
;   
;   tested & working on Win 8.1 with PureBasic v5.31 (x64)
;
;   by TI-994A - free to use, improve, share...
;
;   18th December 2014
;==============================================================
Procedure.s GetParent(gadgetNo)
  parentID = GetParent_(GadgetID(gadgetNo))
  windowNo = GetProp_(parentID, StringField("PB_WINDOWID", 1, ","))
  If windowNo
    result$ = "w," + Str(windowNo - 1)
  Else
    result$ = "g," + Str(GetDlgCtrlID_(parentID))
  EndIf
  ProcedureReturn result$
EndProcedure
;demo code
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 240, 180, "GetParent() Win0", wFlags)
ResizeWindow(0, WindowX(0) - 130, #PB_Ignore, #PB_Ignore, #PB_Ignore)
TextGadget(10, 10, 10, 220, 20, "Window & Container with same numbers:")
ButtonGadget(1, 10, 40, 220, 30, "Button in Window 0")
ContainerGadget(0, 0, 80, 240, 50)
SetGadgetColor(0, #PB_Gadget_BackColor, #Cyan)
ButtonGadget(2, 10, 10, 220, 30, "Button in Container 0")
CloseGadgetList()
ButtonGadget(3, 10, 140, 220, 30, "Button in Window 0")
OpenWindow(123, 0, 0, 240, 180, "GetParent() Win123", wFlags)
ResizeWindow(123, WindowX(123) + 130, #PB_Ignore, #PB_Ignore, #PB_Ignore)
TextGadget(11, 10, 10, 220, 20, "Window & Container with different numbers:")
ButtonGadget(4, 10, 40, 220, 30, "Button in Window 123")
ContainerGadget(69, 0, 80, 240, 50)
SetGadgetColor(69, #PB_Gadget_BackColor, #Magenta)
ButtonGadget(5, 10, 10, 220, 30, "Button in Container 69")
CloseGadgetList()
ButtonGadget(6, 10, 140, 220, 30, "Button in Window 123")
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 1 To 6
          Select EventType()
            Case #PB_EventType_LeftClick
              parentType$ = StringField(GetParent(EventGadget()), 1, ",")
              parentNo$ = StringField(GetParent(EventGadget()), 2, ",")
              If parentType$ = "w"
                result$ = "Window #" + parentNo$
              Else
                result$ = "Gadget #" + parentNo$
              EndIf
              SetGadgetText(EventGadget(), "My Papa is " + result$)
            Case #PB_EventType_LeftButtonUp
          EndSelect
      EndSelect
  EndSelect
Until appQuitThank you.