Your choice of variable labels makes me think you have taken this from the documentation?
You haven't given x2,y2 any values. They need to be relative to the top/left corner of the rectangle x,y
Code: Select all
dim x as integer
dim y as integer
dim x2 as integer
dim y2 as integer
dim res as integer
dim buttons as integer
screenres 640, 480, 8
do
locate 1, 1
' result = GetMouse (x, y, wheel,buttons)
res = getmouse(x,y, ,buttons)
if buttons and 2 then 'if menu button down
cls 'clear screen to redraw rectangle else where
line (x, y)-(x+200, y+16), 15, b 'rectangle top/left at x,y with width 200 and height 16
end if
print x, y
sleep 2
loop while inkey = ""
I hope you don't mind that this response is more than you asked for but it may be useful to others reading the thread who might have also thought about writing their own simple GUI.
GUI objects are just displayed as rectangle areas be they buttons of any kind (eg. menu button) or labels, drop down menu, picture boxes and so on.
This is what I would consider a minimal start to a GUI based menu.
Code: Select all
screenres 640, 480, 32
dim as integer mx,my,mb 'mouse variables
color rgb(0,0,0),rgb(255,255,255) 'black ink, white paper
type RECTANGLE
as integer x 'position
as integer y
as integer w 'dimensions
as integer h
as string text 'rectangle text
as ulong c 'color
as integer a 'rectangle selected
end type
'make a rectangle
dim shared as RECTANGLE rect
'initialize rectangle
rect.x = 100
rect.y = 100
rect.text = "MY RECTANGLE"
rect.w = len(rect.text)*8+16 'width to fit text with standard size font
rect.h = 16 'hight to fit text with standard size font
sub update()
screenlock
cls
'draw the rectangle
line (rect.x,rect.y)-(rect.x+rect.w,rect.y+rect.h),rgb(200,200,255),bf
line (rect.x,rect.y)-(rect.x+rect.w,rect.y+rect.h),rgb(0,0,0),b
'change text if left mouse button down within rectangle
if rect.a = 0 then
draw string (rect.x+4,rect.y+4),rect.text
else
draw string (rect.x+4,rect.y+4)," GOT ME! "
end if
screenunlock
end sub
update()
Do
update()
getmouse (mx, my, ,mb)
if mb = 1 then 'left mouse button down
if mx>rect.x and my>rect.y and mx<rect.x+rect.w and my<rect.y+rect.h then 'mouse down on button
rect.a = 1 'activate button
end if
else
rect.a = 0 'deactivate button
end if
sleep 2
Loop While Inkey = ""
This is easy to expand to a list of rectangles.
Code: Select all
screenres 640, 480, 32
dim as integer mx,my,mb 'mouse variables
color rgb(0,0,0),rgb(255,255,255) 'black ink, white paper
type RECTANGLE
as integer x 'position
as integer y
as integer w 'dimensions
as integer h
as string text 'button text
as ulong c 'color
as integer a 'button selected
end type
dim shared as integer ID 'currently selected rectangle
'make a rectangle
dim shared as RECTANGLE rect(0 to 3) 'make four rectangles
'initialize rectangle
for i as integer = 0 to 3
rect(i).x = 100
rect(i).y = 32 + i*16 'position down y axis
rect(i).text = "MENU ITEM " & str(i+1)
rect(i).w = len(rect(i).text)*8+16 'width to fit text with standard size font
rect(i).h = 12 'height to fit text with standard size font
next i
sub update()
screenlock
cls
'draw the rectangles
for i as integer = 0 to 3
if ID = i then 'show this is selected by changing its color
line (rect(i).x,rect(i).y)-(rect(i).x+rect(i).w,rect(i).y+rect(i).h),rgb(200,200,255),bf
else
line (rect(i).x,rect(i).y)-(rect(i).x+rect(i).w,rect(i).y+rect(i).h),rgb(255,200,200),bf
end if
'border rectagle if needed
'line (rect(i).x,rect(i).y)-(rect(i).x+rect(i).w,rect(i).y+rect(i).h),rgb(0,0,0),b
draw string (rect(i).x+4,rect(i).y+4),rect(i).text
next i
screenunlock
end sub
update()
Do
update()
getmouse (mx, my, ,mb)
ID = -1 'none selected
if mb = 1 then 'left mouse button down
for i as integer = 0 to 3
if mx>rect(i).x and my>rect(i).y and mx<rect(i).x+rect(i).w and my<rect(i).y+rect(i).h then
ID = i 'active button
end if
next i
end if
sleep 2
Loop While Inkey = ""