Sample PowerBASIC Programs
Getting right to the code, this page gives two different program examples.
The first is a very simple program, showing how minimal a PowerBASIC program
can be. The second is also small, but shows the basics of creating
a graphical user interface (a window and a button) and shows the basics of
handling events.
Simple Program
In just four lines you can write a PowerBASIC program. Just place the
following lines of code into a file, open it in the IDE, and press the
Compile/Execute button. A popup window will be displayed with the words
"Hello World". Clicking the OK button will end the program.
#COMPILE EXE
Function PBMain() as Long
MsgBox "Hello World"
End Function
Programs in PowerBASIC start execution in the PBMain function, which is the
only function this simple program includes. Once the message box OK button
is pressed the PBMain function ends, which also ends the PowerBASIC program.
Sample Program
This slightly expanded example provides a better feel for what a complete
PowerBASIC program is all about. In this example, a single window is
created which contains a single button.
Unlike code created using the drag and drop capabilities of a Visual Basic
program, the interface-defining PowerBASIC code is written manually. Event code
(such as responding to a button click) is also written manually.
A companion product, PowerBASIC Forms, is available which supports VB-like drag
and drop creation of the graphical user interface.
Here's the entire program, consisting of just 11 lines of code. Just place this text
into a file, open it in the IDE, and press the Compile/Execute button. The program
consists of a window containing a single button. When the button is pressed the
program ends.
#Compile Exe
#Include "Win32API.inc"
Function PBMain() As Long
Dim hDlg As Dword, hCtl As Dword
Dialog New 0, "PowerBASIC",300,300,100,75, %WS_SysMenu,0 To hDlg
Control Add Button, hDlg, 2, "Cancel", 25, 15, 40, 20
Dialog Show Modal hDlg Call DlgProc To Result&
End Function
CallBack Function DlgProc() As Long
If CbMsg = %WM_Command Then Dialog End CbHndl, 0
End Function
Here's a line-by-line analysis of what the code does.
- #COMPILE EXE
Tells PowerBASIC compiler to create an EXE (rather than a DLL)
- #INCLUDE "Win32API.inc"
Imports Windows API declarations and constants, such as %WS_SYSMENU
- Function PBMain() as Long
Starting point for all PowerBASIC programs
- DIM hDlg AS DWORD, hCtl AS DWORD
Declares variables to hold the dialog and control handles
- DIALOG NEW 0, "Caption", 300, 300, 200, 200, %WS_SYSMENU, 0 TO hDlg
Create new dialog (window) in memory. The dialog will not be
displayed until the SHOW statement below is executed.
- CONTROL ADD BUTTON, hDlg, 2, "Cancel", 100, 100, 40, 20
Adds a control button onto the dialog (window)
- DIALOG SHOW MODAL hDlg CALL DlgProc TO Result&
Displays the dialog (window) on the screen and defines the
callback function as "DlgProc"
- END FUNCTION
Ends the PBMain function
- CALLBACK FUNCTION DlgProc() AS LONG
Declares the callback function that handles Windows messages
- IF CBMSG = %WM_COMMAND THEN DIALOG END CBHNDL, 0
Validates that a valid message (a command) was received, then closes
the dialog (window)
- END FUNCTION
Ends the callback function
Because the DIALOG SHOW statement was executed as MODAL, the dialog window
is displayed and no further statements in the PBMain() function are
executed until the dialog window is closed. At that point, there are no
more statements to execute in the PBMain() function so the PowerBASIC
program ends.
During a modal display of a dialog, however, Windows will continue to
send messages to the PowerBASIC application. In the example above,
the CallBack Function is called whenever a Windows message is received
by the PowerBASIC application. In that way, the application can respond
to user events, such as the user clicking on the button with a mouse.
|