Page 1 of 1

Simple image buttons

Posted: Sun Nov 11, 2007 6:20 pm
by mrjiles
Have been trying to get some sort of image button to work. This is a very simple try that seems to work good. Maybe it could be expanded further. CodeArchive had some examples but they didn't work as as well.

Replace up.bmp and down.bmp with your own images:

Code: Select all

enumeration
#window
#imgbutton
#btn_up
#btn_down
endenumeration
openwindow(#window, 100, 100, 300, 300, "test")
     creategadgetlist(windowid(#window))
          loadimage(#btn_up, "up.bmp")
          loadimage(#btn_down, "down.bmp")
          imagegadget(#imgbutton, 10, 10, 100, 30, imageid(#btn_up))
          disablegadget(#imgbutton, #true)


repeat
     event = waitwindowevent()
     
     win_width = windowwidth(#window)
     win_height = windowheight(#window)
     mouse_x = windowmousex(#window)
     mouse_y = windowmousey(#window)
     
     button_x = gadgetx(#imgbutton)
     button_y = gadgetx(#imgbutton)
     button_h = gadgetheight(#imgbutton)
     button_w = gadgetwidth(#imgbutton)
     
     select event
          case #WM_LBUTTONDOWN
               if mouse_x >= button_x and mouse_x <= button_x+button_w and mouse_y >= button_y and mouse_y <= button_y+button_h
                    setgadgetstate(#imgbutton, imageid(#btn_down))
               endif
          case #WM_LBUTTONUP
               setgadgetstate(#imgbutton, imageid(#btn_up))
     endselect
     
until event = #pb_event_closewindow
end

Posted: Mon Nov 12, 2007 5:50 pm
by Amnesty
Hmmm.... it's the same like #PB_EventType_LeftClick on imagegadget(), isn't it ?

Posted: Wed Nov 14, 2007 12:05 pm
by CSAUER
You will get in trouble, when you have a nested gadget inside a container or splitter. Then you need the absolute position to the window and not the relative to the parent gadget.
Maybe I should request a flag to get the absolute position.

Posted: Thu Nov 15, 2007 6:15 am
by mrjiles
I just found Srod's event include file... much better than this!