Simple image buttons

Share your advanced PureBasic knowledge/code with the community.
mrjiles
Enthusiast
Enthusiast
Posts: 238
Joined: Fri Aug 18, 2006 7:21 pm
Location: IL

Simple image buttons

Post 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
Amnesty
User
User
Posts: 54
Joined: Wed Jul 04, 2007 4:34 pm
Location: Germany

Post by Amnesty »

Hmmm.... it's the same like #PB_EventType_LeftClick on imagegadget(), isn't it ?
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post 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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PB4.1 - Win: MacBook black 2008 2,4 GHz, 4 GB RAM, MacOSX 10.5/VMWare/WinXP
PB4.1 - Mac: MacMini G4 1,4 GHz, 512 MB RAM, MacOSX 10.4
mrjiles
Enthusiast
Enthusiast
Posts: 238
Joined: Fri Aug 18, 2006 7:21 pm
Location: IL

Post by mrjiles »

I just found Srod's event include file... much better than this!
Post Reply