Here is my First PureBasic Program - Window, Menu and Gadget
Posted: Sat Jan 09, 2010 1:12 pm
Hi,
This is REALLY my first PureBasic Program . Its being Written while READING and LEARNING all about this language
I have used other Coding languages before (as you can hopefully tell .. and for the better I hope)
Its a combination of what i've learned so far from reading the book , manual , and here on the forums
I hope i havn't made any totally noob errors or coding fopa's
Let me know what you think
Edited , Tweaked with all suggestions i've had so far. and even re-organised and added a little more
This is REALLY my first PureBasic Program . Its being Written while READING and LEARNING all about this language
I have used other Coding languages before (as you can hopefully tell .. and for the better I hope)
Its a combination of what i've learned so far from reading the book , manual , and here on the forums
I hope i havn't made any totally noob errors or coding fopa's
Let me know what you think
Code: Select all
;{ How To open a window,ADD menus,some gadgets And use window events then close everything.
; Also lets you press ENTER key IN string Gadget And respond the same As pressing the button.
; Original examples taken from
; "PureBasic - A Beginners Guide To Computer Programming";
; The PureBasic help file and the PureBasic Forums
; Code merged , fixed (Depreciated commands replaced or removed) , modified
; To work on PureBasic 4.40 by Cameron Arnott AKA Blue Steel
; Extra advive Given at the PureBasic forums to better structure my program
; This program is my First PureBasic Program,
; Written in about 12 hours from totaly knowing nothing about PureBasic,
; while reading and searching all of the above
; If I can do it so can anyone ;)
; PS: Expect this to Grow as time goes on to include Different things
; as I experiment and learn
;} The {} after the start and end remarks make them into a foldable block
;Compiler Directives
EnableExplicit ; make sure all variables are explicitly defined before use
; Declare Procedures
Declare UPDATE_LIST_VIEW_GADGET() ; ; and reset the String Gadget back to defaults
; Setup the About box content
Define About.s = "How To open a window,ADD menus,some gadgets And use window events then close everything."
About + " Also lets you press ENTER key IN string Gadget And respond the same As pressing the button."
;Setup vairables the window
Define Event.l ; Set to = WaitWindowEvent() later on in event loop
Define Title.s = "My Window"
Define WindowHeight.i = 200
Define WindowLength.i = 300
; Setup window type Flags
#FLAGS = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
; Setup global variables
Global Quit.i = #False
Global Taunt.s = "Go on I dare you"
; Setup constants for Purebasic Libraries using enumerate
; starting at 0 in each block
Enumeration ; Windows
#WIN_MAIN
EndEnumeration
Enumeration ; Gadgets
#TEXT_INPUT
#STRING_INPUT
#LIST_INPUT
#BUTTON_INTERACT
#BUTTON_CLOSE
EndEnumeration
Enumeration ; Menus
#MENU_MAIN
#MENU_QUIT
#MENU_ABOUT
#MENU_CHILD1
#RETURN_KEY ; Dummy menu item to process Enter key in string gadget
EndEnumeration
;Try to open a window if it can't fail and quit out
If OpenWindow(#WIN_MAIN, 0, 0, WindowLength, WindowHeight + MenuHeight(), Title, #FLAGS)
; ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
; Internal Size of Window Not External
; Still need ^ ^ even if Window is going to be centered
; Set up menu, NOTE: the &Q and &A are only underlined if accessed by the F10 Key and arrows
If CreateMenu(#MENU_MAIN, WindowID(#WIN_MAIN))
MenuTitle("File")
OpenSubMenu("Parent Menu Item")
MenuItem(#MENU_CHILD1, " Sub Menu Item")
CloseSubMenu()
MenuBar()
MenuItem(#MENU_QUIT, "&Quit" + #TAB$ + "Ctrl+Q")
MenuTitle("Help")
MenuItem(#MENU_ABOUT, "&About..." + #TAB$ + "Ctrl+A")
; Add shortcut keys to menu's
AddKeyboardShortcut(#WIN_MAIN,#PB_Shortcut_Control|#PB_Shortcut_Q,#MENU_QUIT)
AddKeyboardShortcut(#WIN_MAIN,#PB_Shortcut_Control|#PB_Shortcut_A,#MENU_ABOUT)
EndIf
; Setup Gadgets
ButtonGadget(#BUTTON_CLOSE, 190, 170, 100, 20, "Close window")
ButtonGadget(#BUTTON_INTERACT, 10, 170, 120, 20, "Enter text")
ListViewGadget(#LIST_INPUT, 10, 60, 280, 100)
StringGadget(#STRING_INPUT, 10, 30, 280, 20, Taunt)
TextGadget(#TEXT_INPUT, 10, 10, 280, 20, "Enter text here:")
; ADD some colour To the gadgets IN form $BBGGRR Or even using RGB(Red,Green,Blue)
SetGadgetColor(#LIST_INPUT, #PB_Gadget_BackColor, $A40000)
SetGadgetColor(#LIST_INPUT, #PB_Gadget_FrontColor, RGB(0, 255, 255))
SetGadgetColor(#STRING_INPUT, #PB_Gadget_BackColor, $004B85)
SetGadgetColor(#STRING_INPUT, #PB_Gadget_FrontColor, RGB(255, 255, 0))
; Set Active Gadget
SetActiveGadget(#STRING_INPUT)
; Start of main Program LOOP
Repeat
; Check if something has happened in the window
Event = WaitWindowEvent()
Select Event
; Check to see if any menu items have been accessed
Case #PB_Event_Menu
Select EventMenu()
Case #MENU_ABOUT
MessageRequester("About", About)
Case #MENU_CHILD1
AddGadgetItem(#LIST_INPUT, -1, "You selected the Sub Memnu Item")
Case #MENU_QUIT
Quit = #True
Case #RETURN_KEY
; I used a procedure here so that I wouldn't have to double up my code
UPDATE_LIST_VIEW_GADGET()
EndSelect
; Check to see if any gadgets have been accessed
Case #PB_Event_Gadget
Select EventGadget()
Case #BUTTON_CLOSE
Quit = #True
Case #BUTTON_INTERACT
; I used a procedure here so that I wouldn't have to double up my code
UPDATE_LIST_VIEW_GADGET()
Case #STRING_INPUT
Select EventType()
Case #PB_EventType_Focus
; Keyboard Shortcuts are only linked to the Menu's so this ads shortcut to a dummy menu item
; Add Enter key shortcut only if the String Gadget is in focus
AddKeyboardShortcut(0,#PB_Shortcut_Return, #RETURN_KEY)
; If its not in focus then Remove the Enter key shortcut
Case #PB_EventType_LostFocus
RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
EndSelect
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow Or Quit = #True
EndIf
End
Procedure UPDATE_LIST_VIEW_GADGET() ; and reset the String Gadget back to defaults
; copy the text in the string Gadget into the list gadget
AddGadgetItem(#LIST_INPUT, -1, GetGadgetText(#STRING_INPUT))
; Reset the Message displayed in the string gedget
SetGadgetText(#STRING_INPUT, Taunt)
; make sure that the string gadget has the focus again
SetActiveGadget(#STRING_INPUT)
EndProcedure