I am looking for a code for counting the number of each type gadget.
A procedure that return the number of button StringGadget, TextGadget, etc. ....
If they return the ID gadget, it would be paradise

Thank you for your help
Code: Select all
Structure _countgadgets
className$
count.l
EndStructure
Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets)
Protected buffer$
buffer$=Space(100)
GetClassName_(hwnd, @buffer$, 100)
If buffer$ = *cg\className$
*cg\count + 1
EndIf
ProcedureReturn 1
EndProcedure
;The following function takes a window handle and a gadget handle and returns a count of the
;number of similar controls on the given window.
;This count includes the specified gadget.
Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd)
Protected buffer$
Protected cg._countgadgets
If IsWindow_(winhWnd) And IsWindow_(gadgethWnd)
buffer$=Space(100)
GetClassName_(gadgethWnd, @buffer$, 100)
cg\className$ = buffer$
EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg)
EndIf
ProcedureReturn cg\count
EndProcedure
If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
count = CountSimilarGadgets(WindowID(0), GadgetID(0))
Debug "There are " + Str(count) + " buttons!"
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
EndIf
End
Code: Select all
; http://www.purebasic.fr/english/viewtopic.php?t=30961
; Code de SROD
; Agrandi par KCC pour montrer plusieurs exemples
Structure _countgadgets
className$
count.l
EndStructure
Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets)
Protected buffer$
buffer$=Space(100)
GetClassName_(hwnd, @buffer$, 100)
If buffer$ = *cg\className$
*cg\count + 1
EndIf
ProcedureReturn 1
EndProcedure
;The following function takes a window handle and a gadget handle and returns a count of the
;number of similar controls on the given window.
;This count includes the specified gadget.
Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd)
Protected buffer$
Protected cg._countgadgets
If IsWindow_(winhWnd) And IsWindow_(gadgethWnd)
buffer$=Space(100)
GetClassName_(gadgethWnd, @buffer$, 100)
cg\className$ = buffer$
EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg)
EndIf
ProcedureReturn cg\count
EndProcedure
If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
StringGadget(5, 10, 200, 100, 20, "Coucou")
ComboBoxGadget(6, 10,240, 100, 50)
ComboBoxGadget(7, 10, 300, 100, 50)
ComboBoxGadget(8, 10, 350, 100, 50)
ListViewGadget(9, 10, 400, 100, 30)
ListViewGadget(10, 10, 450, 100, 30)
CountButton = CountSimilarGadgets(WindowID(0), GadgetID(4))
CountText = CountSimilarGadgets(WindowID(0), GadgetID(5))
CountBox = CountSimilarGadgets(WindowID(0), GadgetID(6))
CountList = CountSimilarGadgets(WindowID(0), GadgetID(9))
Debug "There are " + Str(countButton) + " buttons!"
Debug "There are " + Str(countText) + " Texte!"
Debug "There are " + Str(countBox) + " ComboBox!"
Debug "There are " + Str(countList) + " ListBox!"
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
EndIf
End
Code: Select all
Import "gadget.lib"
PB_Gadget_Objects
PB_Object_EnumerateStart (*Object)
PB_Object_EnumerateNext (*Object, *id)
EndImport
Macro ForEachObject(obj,cursor)
PB_Object_EnumerateStart(obj)
While PB_Object_EnumerateNext(obj,@cursor)
EndMacro
Macro NextObject : Wend : EndMacro
If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
ButtonGadget(3, 10,100, 200, 60, "Multiline Button (longer text gets automatically wrapped)", #PB_Button_MultiLine)
ButtonGadget(99, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
ButtonGadget(-1, 10,210, 200, 20, "Toggle Button", #PB_Button_Toggle)
ForEachObject(PB_Gadget_Objects,id)
Debug id
Debug GadgetType(id)
Debug "-----------"
NextObject
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
EndIf
End
There are 2 TextGadget![]()
There are 2 Combo![]()
There are 4 String![]()
Code: Select all
; http://www.purebasic.fr/english/viewtopic.php?t=30961
; Codé par SROD
Structure _countgadgets
className$
count.l
EndStructure
Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets)
Protected buffer$
buffer$=Space(100)
GetClassName_(hwnd, @buffer$, 100)
If buffer$ = *cg\className$
*cg\count + 1
EndIf
ProcedureReturn 1
EndProcedure
;The following function takes a window handle and a gadget handle and returns a count of the
;number of similar controls on the given window.
;This count includes the specified gadget.
Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd)
Protected buffer$
Protected cg._countgadgets
If IsWindow_(winhWnd) And IsWindow_(gadgethWnd)
buffer$=Space(100)
GetClassName_(gadgethWnd, @buffer$, 100)
cg\className$ = buffer$
EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg)
EndIf
ProcedureReturn cg\count
EndProcedure
If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
For o = 1 To 2
TextGadget(100 + o, 10, 30 + (o * 30), 47, 20, "Base", #PB_Text_Center)
ComboBoxGadget(120 + o, 50, 30 + (o * 30), 90, 300, #PB_ComboBox_Editable)
StringGadget(140 + o, 150, 30 + (o * 30), 330, 20, "")
Next
Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(101))) + " TextGadget"
Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(121))) + " Combo"
Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(141))) + " String"
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
EndIf
End
Code: Select all
; http://www.purebasic.fr/english/viewtopic.php?t=30961
; Codé par SROD
Structure _countgadgets
win.l
className$
count.l
EndStructure
Procedure.l EnumChildWindowProc(hwnd, *cg._countgadgets)
Protected buffer$
If GetParent_(hWnd) = *cg\win
buffer$=Space(100)
GetClassName_(hwnd, @buffer$, 100)
If buffer$ = *cg\className$
*cg\count + 1
EndIf
EndIf
ProcedureReturn 1
EndProcedure
;The following function takes a window handle and a gadget handle and returns a count of the
;number of similar controls on the given window.
;This count includes the specified gadget.
Procedure.l CountSimilarGadgets(winhWnd, gadgethWnd)
Protected buffer$
Protected cg._countgadgets
If IsWindow_(winhWnd) And IsWindow_(gadgethWnd)
buffer$=Space(100)
GetClassName_(gadgethWnd, @buffer$, 100)
cg\win = winhWnd
cg\className$ = buffer$
EnumChildWindows_(winhWnd, @EnumChildWindowProc(),@cg)
EndIf
ProcedureReturn cg\count
EndProcedure
If OpenWindow(0, 100, 100, 600, 600, "Count gadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
For o = 1 To 2
TextGadget(100 + o, 10, 30 + (o * 30), 47, 20, "Base", #PB_Text_Center)
ComboBoxGadget(120 + o, 50, 30 + (o * 30), 90, 300, #PB_ComboBox_Editable)
StringGadget(140 + o, 150, 30 + (o * 30), 330, 20, "")
Next
Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(101))) + " TextGadget"
Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(121))) + " Combo"
Debug "There are " + Str(CountSimilarGadgets(WindowID(0), GadgetID(141))) + " String"
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
EndIf
End