Why not use the canvas? I'm an avid fan of screen(), but sometimes there's a better way.
Perhaps something like this? Then you got all the eventtypes available from the canvasgadget too.
Code: Select all
Structure my_buttons
x.i
y.i
im.i[3]
selected.i
EndStructure
Global Dim bt.my_buttons(8)
Global bw.i = 638/8
Procedure.i buttoncanvas()
Protected newnum.i, et.i
newnum = GetGadgetAttribute(0, #PB_Canvas_MouseX)
et = EventType()
Select et
Case #PB_EventType_MouseMove
StartDrawing(CanvasOutput(0))
For x = 0 To 7
If newnum > bt(x)\x And newnum < bt(x)\x+bw
If bt(x)\selected = 1
DrawImage(ImageID(bt(x)\im[2]),bt(x)\x,0)
Else
DrawImage(ImageID(bt(x)\im[1]),bt(x)\x,0)
EndIf
Else
If bt(x)\selected = 1
DrawImage(ImageID(bt(x)\im[2]),bt(x)\x,0)
Else
DrawImage(ImageID(bt(x)\im[0]),bt(x)\x,0)
EndIf
EndIf
Next x
StopDrawing()
Case #PB_EventType_LeftClick
StartDrawing(CanvasOutput(0))
For x = 0 To 7
If newnum > bt(x)\x And newnum < bt(x)\x+bw
If et = #PB_EventType_LeftClick And bt(x)\selected = 0
bt(x)\selected = 1
DrawImage(ImageID(bt(x)\im[2]),bt(x)\x,0)
Else
DrawImage(ImageID(bt(x)\im[1]),bt(x)\x,0)
EndIf
Else
DrawImage(ImageID(bt(x)\im[0]),bt(x)\x,0)
bt(x)\selected = 0
EndIf
Next x
StopDrawing()
EndSelect
EndProcedure
Procedure.i ButtonEvents()
If EventType() = #PB_EventType_LeftClick
For x = 0 To 7
If bt(x)\selected = 1
Debug "Button " + Str(x)
EndIf
Next x
EndIf
EndProcedure
Procedure.i CreateButtons()
For x = 0 To 7
With bt(x)
\im[0] = CreateImage(#PB_Any, bw, 20) ;Normal
\im[1] = CreateImage(#PB_Any, bw, 20) ;Hovered
\im[2] = CreateImage(#PB_Any, bw, 20) ;Selected---Not used in example
StartDrawing(ImageOutput(\im[0]))
Box(0, 0, bw, 20, $777777)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText((bw/2)-(TextWidth("Button X")/2), 10-(TextHeight("XXXX")/2),"Button " + Str(x), $0)
LineXY(1, 1, bw-2, 1, $AAAAAA)
LineXY(1, 1, 1, 18, $AAAAAA)
LineXY(1,18, bw-2, 18, $555555)
LineXY(bw-2, 1, bw-2, 18, $555555)
StopDrawing()
StartDrawing(ImageOutput(\im[1]))
Box(0, 0, bw, 20, $999999)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText((bw/2)-(TextWidth("Button X")/2), 10-(TextHeight("XXXX")/2),"Button " + Str(x), $0)
LineXY(1, 1, bw-1, 1, $AAAAAA)
LineXY(1, 1, 1, 18, $AAAAAA)
LineXY(1,18, bw-1, 18, $555555)
LineXY(bw-2, 1, bw-2, 18, $555555)
StopDrawing()
bt(x)\x = x*bw
StartDrawing(ImageOutput(\im[2]))
Box(0, 0, bw, 20, $CCCCCC)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText((bw/2)-(TextWidth("Button X")/2), 10-(TextHeight("XXXX")/2),"Button " + Str(x), $333333)
LineXY(1, 1, bw-2, 0, $AAAAAA)
LineXY(1, 1, 0, 18, $AAAAAA)
LineXY(1,18, bw-2, 18, $555555)
LineXY(bw-2, 1, bw-2, 18, $555555)
StopDrawing()
bt(x)\x = x*bw
EndWith
Next x
EndProcedure
CreateButtons()
OpenWindow(0, 0, 0, 640, 480, "Small buttons", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0, 2, 2,638,20)
BindGadgetEvent(0,@buttoncanvas())
buttoncanvas()
ButtonGadget(1, 40, 40, 150, 20, "Check selected button")
BindGadgetEvent(1, @ButtonEvents())
Repeat
ev = WaitWindowEvent()
Until ev = #PB_Event_CloseWindow