Page 1 of 1

Basic Understanding Of Writing GUI Applications

Posted: Wed Apr 17, 2013 1:34 am
by Blankname
I had trouble at first understanding exactly which way to write my GUI applications in PureBasic. So below is a outlined template of how to properly code a GUI application with PureBasic.

this template is for purebasic 5.xx and above

First we design our form in the form designer, and then save it as "Form.pbf" on our computer.

Code: Select all

Global Window_0

Global Button_0

Enumeration #PB_Compiler_EnumerationValue
  #MenuItem_2
EndEnumeration


Procedure OpenWindow_0()
  Window_0 = OpenWindow(#PB_Any, 0, 0, 270, 80, "GUI Code Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CreateMenu(0, WindowID(Window_0))
  MenuTitle("File")
  MenuItem(#MenuItem_2, "Derp Im'a Menu Item!")
  Button_0 = ButtonGadget(#PB_Any, 10, 10, 250, 40, "Derp Im'a Button!")
EndProcedure
Then we create a source file in that same directory, and write our application.

Code: Select all

XIncludeFile "Form.pbf" ;We include our form file made with the form designer

OpenWindow_0() ;Call the OpenWindow_0() function to initiate the window

Repeat ;Begin our main loop
  
  EventID = WaitWindowEvent() ;Wait for a window event, and store the event ID into the EventID variable
  
  Select EventID ;Once an event is triggered select the EventID
      
    Case #PB_Event_Menu ;If the triggered event was a Menu event
      
      Select EventMenu() ;Select the menu item responsible for the event
          
        Case #MenuItem_2 ;If the menu item was #MenuItem_1
          MessageRequester("Notice", "Menu Item Selected!")
          
      EndSelect
      
    Case #PB_Event_Gadget ;If the triggered event was a Gadget event
      
      Select EventGadget() ;Select the Gadget responsible for the event
          
        Case Button_0 ;If the Gadget was Button_0
          MessageRequester("Notice", "Button Gadget Selected!")
          
      EndSelect
      
    Case #PB_Event_CloseWindow ;If the triggered event was a close window event
      End
      
  EndSelect
  
ForEver
I hope this helps new people to the language, as it can be a headache having to go back and rewrite most of your code because of slight GUI alterations. Plus I find this code layout to be by far the easiest to understand and makes the most sense.

Have fun! :D