Example for FormDesigner. For Menu Exit and CanvasGadget I have assigned a procedure in the FormDesigner.
For me, I switch off under Settings -> Form the "New gadget use #PB_Any by default".
MainForm.pbf
Code: Select all
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;
Enumeration FormWindow
#MainWindow
EndEnumeration
Enumeration FormGadget
#Canvas_Gad
EndEnumeration
Enumeration FormMenu
#MenuItem_Exit
EndEnumeration
Declare DoMenuEvent(Event)
Declare DoCanvasEvent(EventType)
Procedure OpenMainWindow(x = 0, y = 0, width = 600, height = 400)
OpenWindow(#MainWindow, x, y, width, height, "", #PB_Window_SystemMenu)
CreateMenu(0, WindowID(#MainWindow))
MenuTitle("File")
MenuItem(#MenuItem_Exit, "Exit")
CanvasGadget(#Canvas_Gad, 10, 10, 580, 380)
EndProcedure
Procedure MainWindow_Events(event)
Select event
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_Menu
Select EventMenu()
Case #MenuItem_Exit
DoMenuEvent(EventMenu())
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #Canvas_Gad
DoCanvasEvent(EventType())
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
Edit: MainFile.pb
Code: Select all
IncludeFile "MainForm.pbf" ; Include the first window definition
Enumeration FormMenu ; <- Same name of form file
#PopupMenu_1
#PopupMenu_2
EndEnumeration
; Create PopupMenu
CreatePopupMenu(1)
MenuItem(#PopupMenu_1, "Menu 1")
MenuItem(#PopupMenu_2, "Menu 2")
; The event procedures, as specified in the 'event procedure' property of each gadget
Procedure DoCanvasEvent(EventType)
Select EventType
Case #PB_EventType_RightClick
DisplayPopupMenu(1, WindowID(#MainWindow))
EndSelect
EndProcedure
Procedure DoMenuEvent(EventMenu)
Select EventMenu
Case #MenuItem_Exit
PostEvent(#PB_Event_CloseWindow, #MainWindow, 0)
EndSelect
EndProcedure
Procedure DoPopupMenuEvent(EventMenu)
Select EventMenu
Case #PopupMenu_1
Debug "Popup Menu 1"
Case #PopupMenu_2
Debug "Popup Menu 2"
EndSelect
EndProcedure
;- Open Main
OpenMainWindow() ; Open the first window. This procedure name is always 'Open' followed by the window name
If IsWindow(#MainWindow)
; The main event loop as usual, the only change is to call the automatically
; generated event procedure for each window.
Repeat
Event = WaitWindowEvent()
Select EventWindow()
Case #MainWindow
If MainWindow_Events(Event) = #False ; This procedure name is always window name followed by '_Events'
Break
EndIf
EndSelect
; Own Event management
Select Event
Case #PB_Event_Menu
DoPopupMenuEvent(EventMenu())
EndSelect
ForEver
EndIf
But you can also switch off the "Generate event procedures" under Settings -> Form and write the event management yourself. (Which is what many people do)