' PBWin 7.0 05/01/2002 - WinApi 03/07/2003 '
''http://www.powerbasic.com/support/forums/Forum7/HTML/002247.html
'*************************************************************************************'
'* *'
'* Bare Bones for Newbies *'
'* *'
'*************************************************************************************'
' '
'(Tip - Click on the center icon just above this (Edit/Delete Message) to Cut&Paste '
' from the Eidt box (ignore the password warning) into your program editor. '
' It will Preserve the spacing And formatting.) '
' '
' Below is, I believe, pretty simple code to help guide an absolute NewBy to PB_Win '
' to get an idea how a GUI is made and interacts with the program. '
' '
' (It's a really ugly GUI though {sigh}) '
' '
' The descriptively named variables should help. '
' '
' This sample should be especially helpful to someone coming from a DOS '
' environment. At least I know it would have been to me. '
' '
' With the exception of constants identified in the WinAPi all variables used here '
' have been defined by me and aren't necessary to the PB environment. '
' '
' All that needs to be done to get this to run is change the path in the #Include '
' in the second line to wherever you have Win32Api.Inc (supplied with PB Win) '
' located. '
' '
' Have also added a Tracer Option. This can really be handy to look At program Flow. '
' It creates a Log file "Your_Path:\BareBones_Tracer.txt". '
' It gets enabled in PBMAIN. (Just Set g_Tracer_File_Flag = 1) '
' '
' The Windows "message pump" concept In CallBacks was (and still is somewhat) '
' confusing For me. The Tracer file has cleared away much of the confusion. '
' '
' '
' Gösta H. Lovgren '
' gosta@SwedesDock.com '
' http://www.SwedesDock.com '
' http://www.PondersBible.com '
'*************************************************************************************'
'*************************************************************************************'
'
#Compile Exe
'
#Include "d:\Power Basic\WinApi\WIN32API.INC"
DefLng a-z ' forces all variables to be LONG unless explicitly identified
'
Declare Sub Tracing(tr$)
'
'
'
'Program constants I Use
%Character_Width = 4 ' used to calculate label widths, etc.
%Box_Height = 10 ' used to calculate text box and label heights, etc.
%Cream = 252 + (241 * 256) + (167 * 256 * 256) 'bg color for the main dialog
'
'
'
' Assign Id Numbers as Constants for Controls inside the Main Dialog.
' Tip - It may help you to think of "Dialogs" and "Controls" as separate "Windows" '
%Btn_Sample = 100
%Btn_Another_Sample = 101
'
%Sample_Label_Id = 200
'
%TB_Sample_Text_Box = 300
%TB_Sample_Text_Box_2 = 301 %Tracing_File_Label = 400
'
Global g_Tracer_File_Flag AS LONG 'Used to turn Trace on/off
Global g_Trace_Nest_Names$() 'used in tracer sub
Global g_Tracing_File_Name$
Global Control_Names$()
'*************************************************************************************'
'*************************************************************************************'
' Note CallBacks are somewhat analogous to INPUT routines in DOS. They are a lot more '
' than that, though, as you will see if the Tracer is enabled. '
'*************************************************************************************'
'Main Callback Processor
CallBack Function CB_Main_Processor AS LONG
' Rem tracer here as CallBack gets accessed repeatedly doing various things -
' drawing objects, painting the screen, checking keys, ...
' Tip - Rem next Call Tracing after seeing how much a CallBack gets called. (It's an awful lot.)
' note there is no Tracing("Exit") call at the bottom of this CallBack!
Call Tracing(Using$(" CallBack Message Pump CbCtl=# CbCtlMsg=#", CbCtl, CbCtlMsg))
'
Select Case CbMsg ' This is TO determine the message TYPE returned by the program
'
Case %WM_INITDIALOG '<- for processing that needs to occur when the program loads '
' It only gets hit once when the program starts.
Call Tracing(" %WM_INITDIALOG")
'
Global hDlg AS Dword 'Main dialog handle
'Note - "hDlg" is the conventional variable name used by'
' PB programmers for the main dialog identifier. '
'
Global Version$ 'Header used for Main Dialog and sometimes elsewhere by me
'
Call Tracing(" *** %WM_INITDIALOG done")
'
Case %WM_COMMAND 'This processes command messages
Call Tracing(Using$(" %WM_COMMAND CbCtl=# and is called ", CbCtl) & _
Control_Names$(CbCtl))
'
'msg sent from Controls (ie Buttons clicked in this example)
Select Case CbCtl 'CB-> CbMsg-> %Wm_Command-> CbCtl
'
Case %Btn_Sample
Call Sample("Button Activated")
'
Case %Btn_Another_Sample
Call Another_Sample("You can do your thing here too!")
'
Case %TB_Sample_Text_Box
Control Get Text hDlg, %TB_Sample_Text_Box To txt$
Call Tracing(" Txt$ = " & Txt$ & _
Using$(" CbCtlMsg = #", CbCtlMsg))
'change label color just for fun
Control Set Color hDlg, %Sample_Label_Id, %Red, %Yellow
'Change the text in the label
Control Set Text hDlg, %Sample_Label_Id, _
"First Text Box being used." & $CrLf & $CrLf & Txt$
'
Case %TB_Sample_Text_Box_2
Control Get Text hDlg, %TB_Sample_Text_Box_2 To txt$
Call Tracing(" Txt$ = " & Txt$ & _
Using$(" CbCtlMsg = #", CbCtlMsg))
'change label color just for fun
Control Set Color hDlg, %Sample_Label_Id, %Yellow, %Blue
'Change the text in the label
Control Set Text hDlg, %Sample_Label_Id, _
"Second Text box is now being used" & $CrLf & $CrLf & Txt$
'
Call Tracing(" *** %WM_COMMAND done")
End Select
End Select
End Function
'*************************************************************************************'
'*************************************************************************************'
'*************************************************************************************'
' '
' Programs always begin at PBMain even though it's not at the top of the code. '
' '
'*************************************************************************************'
Function PBMAIN () AS LONG
' Rem/UnRem the next line to enable program flow tracing
g_Tracer_File_Flag = 1
g_Tracing_File_Name$ = "Z:\BareBones_Tracer.Txt" 'Make SURE the correct path is set here
'
Call Tracing("New") 'starts new tracer file - does nothing if flag not set above
Call Tracing("Function PBMAIN () AS LONG")
'
Version$ = "Program version 1.03 from Carolelle Publishing®" & _
" (" & Chr$(169)& "2004) "
'
Call Main_Display_Setup 'where the main window is drawn '
'Note the main window can be drawn here. A procedure is '
' not required but I find it cleaner coding this way. '
'
Control Set Focus Hdlg, %TB_Sample_Text_Box 'start out with cursor here
'
' Tracer file display
If g_Tracer_File_Flag Then 'Tracer file is on
l$ = " Turned on!" & $CrLf & _
"Load it into your Power Basic Editor to see it!."
bg = %Blue
fg = %White
Else
l$ = " is not activated!!!" ' or not on.
bg = %Cream
fg = %Black
End If
Control Set Text hDlg, %Tracing_File_Label, _
g_Tracing_File_Name$ & l$
Control Set Color hDlg, %Tracing_File_Label, fg, bg 'change color
'
'display the main window and get to the action
Dialog Show Modal hDlg Call CB_Main_Processor
'
Call Tracing("exit")
End Function
'*************************************************************************************
'
'
'
'
'*************************************************************************************
'*************************************************************************************
Sub Main_Display_Setup 'where the main window is drawn
Call Tracing("Sub Main_Display_Setup")
Dim Control_Names$(301) '301 is highest Control Id we use in this example
'
Caption$ = "Bare Bones " & Version$
Row = %Box_Height * 3 'start 3 rows down
Col = %Character_Width * 10 'start 10 columns over
Dialog_Width = %Character_Width * 100 'make dialog 100 characters wide
Dialog_Depth = %Box_Height * 30 'make Dialog 30 rows deep
Style = _ 'contains the display properties of the dialog
%WS_CAPTION Or _ 'Note - not all these are absolutely necessary. '
%WS_SYSMENU Or _ ' but are included to just for demonstration '
%WS_THICKFRAME Or _
%WM_HELP Or _
%WS_Border 'doesn't seem to do anything '
' %WS_MINIMIZEBOX Or _
' %WS_MAXIMIZE Or _
Dialog New hDlg, Caption$, _
Col, Row, _ 'where to place dialog on the screen
Dialog_Width, Dialog_Depth, _ 'size to make it
Style, _
%WS_Ex_WindowEdge, _
To hDlg 'Assign a handle
'
'neither of next are really needed but demonstrate a hint of the visual power of PB '
Dialog Font "Arial", 8
Dialog Set Color hDlg, -1&, %Cream
'
'
Space_Between_Buttons = 35
'
Col = 50
Row = 1
Row = Row + Space_Between_Buttons 'move down a little
Button_Height = 25
l$ = " &Sample Button "
Wdth = Len(l$) * %Character_Width 'Make it the width of the string
Control Add Button, hDlg, %Btn_Sample, l$, _
Col, Row, _
Wdth, Button_Height
'
Control_Names$(%Btn_Sample) = l$ 'for tracer function display
'
Row = Row + Space_Between_Buttons
Button_Height = 35 'demonstrate bigger button
l$ = " &Another Sample Button "
Wdth = Len(l$) * %Character_Width
Control Add Button, hDlg, %Btn_Another_Sample, l$, _
Col, Row, _
Wdth, Button_Height
'
Control_Names$(%Btn_Another_Sample) = l$ 'for tracer function display
'
Row = Row + (Space_Between_Buttons * 2)
Height = %Box_Height 'regular height
l$ = "Text Box example "
Wdth = 60 * %Character_Width 'room for 60 characters
Control Add TextBox, hDlg, %TB_Sample_Text_Box, l$, _
Col, Row, _
Wdth, Height
'
Control_Names$(%TB_Sample_Text_Box) = l$ 'for tracer function display
'
Row = Row + %Box_Height + 2
Height = %Box_Height 'regular height
l$ = "Second Text Box example "
Wdth = 60 * %Character_Width 'room for 60 characters
Control Add TextBox, hDlg, %TB_Sample_Text_Box_2, l$, _
Col, Row, _
Wdth, Height
Control_Names$(%TB_Sample_Text_Box_2) = l$ 'for tracer function display
'
'
'
Row = Dialog_Depth - (%Box_Height * 3) - 2 'Show just at bottom of Dialog
l$ = $CrLf & "This is a label"
Wdth = 50 * %Character_Width 'make it wide
col = (Dialog_Width - Wdth) / 2 'center it
Control Add Label, hDlg, %Sample_Label_Id, l$, _
Col, Row, _
Wdth, %Box_Height * 3, _ 'make it 3 lines high
%SS_Center Or _ 'Center the text
%SS_Sunken, _ 'frame it
0
Control_Names$(%Sample_Label_Id) = l$ 'for tracer function display
' Tracer file notice
Row = 5
Col = 5
High = %Box_Height * 3' room for 3 rows of text
Wdth = Dialog_Width - (Col * 2) 'span the whole Dialog but inset a little
Control Add Label, hDlg, %Tracing_File_Label, " ", _
Col, Row, _
Wdth, High, _
%SS_Center, _ 'Center the text
0
'Now let's a a frame around the Label
Control Add Frame, hDlg, -1, "", _
Col - 2, Row - 4, _
Wdth + 4, High + 6, _
%SS_Center
' Change font for the %Tracing_File_Label just for fun
Local lf AS LogFont 'LogFont is UDT defined in WinAPI.Inc
lf.lfFaceName = "Comic Sans MS" 'use any font in your fonts list
If g_Tracer_File_Flag = 1 Then
lf.lfHeight = 24 'play with these numbers to see effects.
lf.lfWidth = 12 '
Else
lf.lfHeight = 48 'play with these numbers to see effects.
lf.lfWidth = 12 '
End If
Comic_Sans_Font = CreateFontIndirect(lf)
Control Send hDlg, %Tracing_File_Label, %Wm_SetFont, Comic_Sans_Font, 0
'
Call Tracing("Exit")
End Sub
'*************************************************************************************
'
'
'*************************************************************************************
'*************************************************************************************
Sub Sample(Hdr$) 'Do your code thing here
Call Tracing("Sub Sample(Hdr$) Hdr$= " & Hdr$)
'
MsgBox "You tripped on the Sub Sample",, Hdr$
'
'Tip - Get familiar with the MsgBox statement and the MsgBox function '
' You'll likely be using them a lot!. '
'
'change label color just for fun
Control Set Color hDlg, %Sample_Label_Id, %White, %Green
'
'Change the text in the label
Control Set Text hDlg, %Sample_Label_Id, _
$CrLf & _ 'move down one line
"Sub Sample was visited last."
Call Tracing("exit")
End Sub
'*************************************************************************************
'
'
'*************************************************************************************
'*************************************************************************************
Sub Another_Sample(Hdr$)'Do the code thing here
Call Tracing("Sub Another_Sample(Hdr$) Hdr$= " & Hdr$)
'
MsgBox "You tripped on the Sub Another Sample",, Hdr$
'
'change label color just for fun
Control Set Color hDlg, %Sample_Label_Id, %Yellow, %Red
'
'Change the text in the label
Control Set Text hDlg, %Sample_Label_Id, _
$CrLf & _ 'move down one line
"Sub Another_Sample was visited last."
'
Call Tracing("exit")
End Sub
'*************************************************************************************
'
'
'
'
'
'
'
'*************************************************************************************
'*************************************************************************************
' Note this is handy to trace variables. Just call it anytime. '
' '
' Tip - After the program is run the first time, load "Tracing.Txt" into your '
' PB Editor. Now, after each program run, the Editor will prompt you To reload '
' "Tracing.Txt". Just Ctl-Tab back And forth between the code And tracer To see '
' program Flow. '
' '
' Tip - At the top of each Sub/Function Call Tracing with the full name at the top, '
' then with Using$(" My_Variable= #", My_Variable) anywhere you need it (note '
' the spaces in the String), '
' then With "exit" the bottom. '
' It will create readable intentations. '
'*************************************************************************************
Sub Tracing(tr$)
Static Inset AS LONG, _ 'remember between calls
Nests AS LONG, _
Indent AS LONG, _
Nest_Counter AS LONG
If g_Tracer_File_Flag = 0 Then Exit Sub 'Tracer not on so beat feet.
Tracing_FileNum = FreeFile
If tr$ = "New" Then
Kill g_Tracing_File_Name$ 'In case it exists from a previous run
Open g_Tracing_File_Name$ For Append AS #Tracing_FileNum
Print #Tracing_FileNum,, "Starting New trace " & _
Date$ & " " & Time$ & $CrLf
Close #Tracing_FileNum
Exit Sub 'Don't print any more
End If
Open g_Tracing_File_Name$ For Append AS #Tracing_FileNum
'dunno why just now but this adds two blank lines so jump over
If InStr(Tr$, "CB_Main_Processor") Then GoTo Tracing_Skip
'Indents for readability
Indent = 4 'spaces to inset
Blank_Line$ = "" 'Blank line if needed
s$ = Space$(80) & $CrLf 'put spaces in blank line for scrolling in text editor
If Left$(UCase$(tr$), 3) = "SUB" Or _
Left$(UCase$(tr$), 3) = "FUN" Then 'New sub/function so inset spaces
Blank_Line$ = S$ 'insert blank line at top
Incr Nest_Counter
Inset = Inset + Indent
ReDim Preserve g_Trace_Nest_Names$(Nest_Counter)
g_Trace_Nest_Names$(Nest_Counter) = tr$ & " EXITED " 'remember for exit title
If Inset > 80 Then Inset = 80 'maximum inset in case of deeply nested subs/functions
tr$ = tr$ & Using$(" ENTERED # deep", Inset \ Indent)
End If
Exit_Flag = 0
If Left$(UCase$(tr$), 4) = "EXIT" Then 'exiting so add a line space
Incr Exit_Flag
tr$ = g_Trace_Nest_Names$(Nest_Counter) & $CrLf & Space$(80) 'get Sub/Function name
Decr Nest_Counter 'toss away this name
If Nest_Counter < 0 Then
Nest_Counter = 0
End If
End If
Tracing_Skip:
Print #Tracing_FileNum, Blank_Line$ & Space$(Inset) & Tr$ 'print to file
Close #Tracing_FileNum 'flush the buffer in case of eventual GPF
If Exit_Flag Then 'now remove last inset as we exit sub/function
Inset = Inset - Indent
If Inset < 0 Then Inset = 0
End If
End Sub
'*************************************************************************************
'
'
'
' end of Bare Bones '
' '
'Tip - Ask a lot of questions on the Power Basic Forums. '
' They are the best in the world. '
' '
'Tip - Invest in the JellyFish Pro editor. It's designed for Power Basic and '
' has a lot more features than the PB editor. '
' '
'Tip - Set the Background color of Remarks to a different color (Yellow is good) to '
' make them stand Out when reading code. Do the same (but use different '
' backgrounds) for Keywords And strings. It's a great help when scanning code. '
' '
' Feel free to email me (gosta@SwedesDock.com) with ANY questions you may have about '
' getting started In PB. Most posters in the Forums are experienced professional '
' programmers And I know how intimidating it can be to ask questions "in their '
' presence" {grin}. (And how frustrating their replies often are to a Newby.) '
' '
' Drop an email anyway. I'm curious to know if BareBones was helpful at all. '
' '