Here's some simple code for adding buttons to your program. It allows you to create lots of different buttons, customizing everything from the colors to the font, and it will do the handling for you.
The project is attached, but here is the main program code:
`AMAZING BUTTONS!!!
`Controller File
`©2007 Nick Aldwin aka NinJA999
`06/13/07
`------------------------------------------
`Adapt this code to your own main program.
`------------------------------------------
`===== your initial number of buttons =====
dim buttons(3) as btn
`+++++ these must be called +++++
gosub my_setupbuttons
gosub drawbuttons
`===== uncomment this if you aren't including the file with the DBPro file includer =====
`#include "buttons.dba"
do
`+++++ these must be called +++++
cls
gosub buttonclick
gosub drawbuttons
ink rgb(255,255,255),0
set cursor 0,0
set text font "Courier New"
set text size 20
print str$(mousex())
print str$(mousey())
`===== put your button handling here =====
if buttons(3).over=TRUE and buttons(3).clicked=FALSE
print "MAKE A FOURTH BUTTON"
endif
if buttons(3).clicked=TRUE
print "MAKING BUTTON!"
`check to make sure there isn't already a button four
if array count(buttons(0))<4
`add button four
dim buttons(4) as btn
buttons(4).x1=50
buttons(4).y1=200
buttons(4).x2=140
buttons(4).y2=220
buttons(4).color=rgb(0,100,0)
buttons(4).textcolor=rgb(0,255,0)
buttons(4).ovrcolor=rgb(100,0,0)
buttons(4).ovrtextcolor=rgb(255,0,0)
buttons(4).clkcolor=rgb(255,0,0)
buttons(4).clktextcolor=rgb(100,0,0)
buttons(4).title = "Delete Me!"
buttons(4).font = "Comic Sans MS"
buttons(4).fsize = 22
endif
endif
`if there is a button four, and if it's clicked, delete it.
if array count(buttons(0))>3
if buttons(4).clicked=TRUE
dim buttons(3) as btn
endif
endif
loop
my_setupbuttons:
`===== put your initial button setup here =====
buttons(1).x1=30
buttons(1).y1=10
buttons(1).x2=100
buttons(1).y2=40
buttons(1).color=rgb(0,100,0)
buttons(1).textcolor=rgb(0,255,0)
buttons(1).ovrcolor=rgb(100,0,0)
buttons(1).ovrtextcolor=rgb(255,0,0)
buttons(1).clkcolor=rgb(255,0,0)
buttons(1).clktextcolor=rgb(100,0,0)
buttons(1).title = "Hello"
buttons(1).font = "Comic Sans MS"
buttons(1).fsize = 22
buttons(2).x1=30
buttons(2).y1=50
buttons(2).x2=100
buttons(2).y2=80
buttons(2).color=rgb(0,0,100)
buttons(2).textcolor=rgb(0,0,255)
buttons(2).ovrcolor=rgb(100,0,0)
buttons(2).ovrtextcolor=rgb(255,0,0)
buttons(2).clkcolor=rgb(255,0,0)
buttons(2).clktextcolor=rgb(100,0,0)
buttons(2).title = "Goodbye"
buttons(2).font = "Courier New"
buttons(2).fsize = 16
buttons(3).x1=30
buttons(3).y1=90
buttons(3).x2=200
buttons(3).y2=120
buttons(3).color=rgb(100,100,0)
buttons(3).textcolor=rgb(255,255,0)
buttons(3).ovrcolor=rgb(255,0,0)
buttons(3).ovrtextcolor=rgb(100,0,0)
buttons(3).clkcolor=rgb(255,0,0)
buttons(3).clktextcolor=rgb(100,0,0)
buttons(3).title = "Create 4th Button"
buttons(3).font = "Verdana"
buttons(3).fsize = 22
return
and here's the IMPORTANT FILE buttons.dba:
`AMAZING BUTTONS!!!
`Include File
`©2007 Nick Aldwin aka NinJA999
`06/13/07
`------------------------------------------
`Change nothing here, just include it.
`------------------------------------------
type btn
x1 as integer
y1 as integer
x2 as integer
y2 as integer
color as integer
textcolor as integer
ovrcolor as integer
ovrtextcolor as integer
clkcolor as integer
clktextcolor as integer
over as boolean
clicked as boolean
title as string
font as string
fsize as integer
endtype
#constant TRUE = 1
#constant FALSE = 0
buttonclick:
for i=1 to array count(buttons(0))
if mousex()>=buttons(i).x1 and mousex()<=buttons(i).x2 and mousey()>=buttons(i).y1 and mousey()<=buttons(i).y2
buttons(i).over=TRUE
if mouseclick()=1
buttons(i).clicked=TRUE
else
buttons(i).clicked=FALSE
endif
else
buttons(i).over=FALSE
buttons(i).clicked=FALSE
endif
next i
return
drawbuttons:
for i=1 to array count(buttons(0))
if buttons(i).over=FALSE
ink buttons(i).textcolor,0
box buttons(i).x1,buttons(i).y1,buttons(i).x2,buttons(i).y2
ink buttons(i).color,0
box buttons(i).x1+1,buttons(i).y1+1,buttons(i).x2-1,buttons(i).y2-1
ink buttons(i).textcolor,0
set text font buttons(i).font
set text size buttons(i).fsize
center text (buttons(i).x1+buttons(i).x2)/2,((buttons(i).y1+buttons(i).y2)/2)-text height(buttons(i).title)/2,buttons(i).title
else
ink buttons(i).ovrtextcolor,0
box buttons(i).x1,buttons(i).y1,buttons(i).x2,buttons(i).y2
ink buttons(i).ovrcolor,0
box buttons(i).x1+1,buttons(i).y1+1,buttons(i).x2-1,buttons(i).y2-1
ink buttons(i).ovrtextcolor,0
set text font buttons(i).font
set text size buttons(i).fsize
center text (buttons(i).x1+buttons(i).x2)/2,((buttons(i).y1+buttons(i).y2)/2)-text height(buttons(i).title)/2,buttons(i).title
endif
if buttons(i).clicked=TRUE
ink buttons(i).clktextcolor,0
box buttons(i).x1,buttons(i).y1,buttons(i).x2,buttons(i).y2
ink buttons(i).clkcolor,0
box buttons(i).x1+1,buttons(i).y1+1,buttons(i).x2-1,buttons(i).y2-1
ink buttons(i).clktextcolor,0
set text font buttons(i).font
set text size buttons(i).fsize
center text (buttons(i).x1+buttons(i).x2)/2,((buttons(i).y1+buttons(i).y2)/2)-text height(buttons(i).title)/2,buttons(i).title
endif
next i
return
Though I have had DBC for a number of years, I just got DBPro, so this is my first contribution in DBPro. Hope you like it!
42
http://www.nictitatingrabbit.com
http://penchantpages.nictitatingrabbit.com