I wrote this code to answer a post in the windows forum, but since it is a neat trick, i figured more people might see it in this section.
Its called double subclassing. You basically subclass a window or control to trap messages you want, in this case a container gadget. you "steal" the #wm_command message and do your own events. the button click in the container never reaches PB's event loop.
then, later on, you decide that you dont want to do those events anymore, or let events naturally pass to the eventloop, so you change the window procedure to a defined callback to let any event you didnt let pass before, now go on to PB's main loop.
you can actually do this any number of times, and just have to have procedures defined for each "state" of a callback routine you want to process.
EXAMPLE:
Code: Select all
Global oldproc.l
Procedure notpossibleproc(hwnd,msg,wParam,lParam)
Select msg
Case #WM_COMMAND
Debug "The control I got the message from is" + Space(1) +Str(lParam)
Debug "the gadgetid of button 1 is" + Space(1) + Str(GadgetID(1))
Debug "I just Punk'd the eventloop"
ProcedureReturn 0 ;don't let the eventloop get this message
;alternatively, you can let this message pass to the eventloop
EndSelect
ProcedureReturn DefWindowProc_(hwnd,msg,wParam,lParam)
EndProcedure
Procedure letitpassproc(hwnd,msg,wParam,lParam)
Select msg
;trap anyother messages you want here, EXCEPT the #wm_command
EndSelect
ProcedureReturn CallWindowProc_(oldproc,hwnd,msg,wParam,lParam)
EndProcedure
If OpenWindow(0, 869, 108, 237, 471, "Robbing and Stealing", #PB_Window_SystemMenu | #PB_Window_TitleBar )
; If CreateGadgetList(WindowID(0))
ContainerGadget(200,20,20,150,90,#PB_Container_Raised)
oldproc=GetWindowLong_(GadgetID(200), #GWL_WNDPROC)
ButtonGadget(1, 10, 15, 80, 24, "Button 1")
oldproc=SetWindowLong_(GadgetID(200),#GWL_WNDPROC,@notpossibleproc())
CloseGadgetList()
CheckBoxGadget(2,10,150,180,20,"Allow Container Events to Pass")
; EndIf
EndIf
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Debug "PB event loop got the message!!!"
Debug "Localmotion34 Rocks!!!"
Case 2
State=GetGadgetState(2)
Select State
Case 0
SetWindowLong_(GadgetID(200),#GWL_WNDPROC,@notpossibleproc())
Case 1
SetWindowLong_(GadgetID(200),#GWL_WNDPROC,@letitpassproc())
EndSelect
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
End