Here's a commented example for ya.
Rem Project: Dark Basic Pro Project
Rem Created: Sunday, June 15, 2014
Rem ***** Main Source File *****
rem define a box object
Type MyBox
x1 as integer
y1 as integer
x2 as integer
y2 as integer
color as dword
EndType
rem create array to store boxes
dim boxes() as MyBox
sync on
DO
cls
rem draw all boxes stored in array
rem call this before the stored boxes so it appears on top.
drawMyBoxes()
rem If mouse down, temporarily store position of mouse
if mouseclick() = 1
if flag = 0
flag = 1
x1 = mousex()
y1 = mousey()
endif
rem mouse is still down but initial point already set.
rem Store current mouse position for ending point of box
if flag = 1
x2 = mousex()
y2 = mousey()
endif
// Only draw this box while the mouse is down
drawBox(x1, y1, x2, y2)
endif
rem on mouse release, save the box
if mouseclick() = 0
if flag = 1
flag = 0
saveBox(x1, y1, x2, y2)
endif
endif
sync
LOOP
rem ////////////////////////////////////////////////
rem Sort coordinates before drawing so box shows up
rem ////////////////////////////////////////////////
function drawBox(x1, y1, x2, y2)
if x1 > x2 then temp = x1 : x1 = x2 : x2 = temp
if y1 > y2 then temp = y1 : y1 = y2 : y2 = temp
rem draw the box
rem By setting the color of all 4 corners of the box in this way,
rem we can use an alpha transparency on the box.
box x1, y1, x2, y2, 0x88FFFFFF, 0x88FFFFFF, 0x88FFFFFF, 0x88FFFFFF
endfunction
rem ////////////////////////////////////////////////
rem save box to array
rem ////////////////////////////////////////////////
function saveBox(x1, y1, x2, y2)
rem sort the coordinates so [x1,y1] is the lowest number
if x1 > x2 then temp = x1 : x1 = x2 : x2 = temp
if y1 > y2 then temp = y1 : y1 = y2 : y2 = temp
rem insert new empty element at end of array
array insert at bottom boxes()
rem set the new elements values for box
boxes().x1 = x1
boxes().y1 = y1
boxes().x2 = x2
boxes().y2 = y2
rem give box a random color
boxes().color = rgb(rnd(255),rnd(255),rnd(255))
endfunction
rem ////////////////////////////////////////////////
rem Draws all saved/stored boxes
rem ////////////////////////////////////////////////
function drawMyBoxes()
n = array count(boxes())
for i = 0 to n
ink boxes(i).color, 0
box boxes(i).x1, boxes(i).y1, boxes(i).x2, boxes(i).y2
next i
endfunction