REAL ImageButton control!

BlitzPlus Forums/BlitzPlus Programming/REAL ImageButton control!

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I like the code.. Thanks! I hope you don't mind but I made an adjustment to it. Give this a try also:
win=CreateWindow("ImageButton",200,200,400,300,0,3)
i=LoadImage("test2.bmp"); These two images should be the same size but look different
j=LoadImage("test1.bmp"); (other image)
w=ImageWidth(i)
h=ImageHeight(i)
button=CreateButton("",(ClientWidth(win)-w)/2,(ClientHeight(win)-h)/2,w,h,win)
SetButtonImage button,i
current = i
Repeat
ID = WaitEvent(1)
	Select ID
		Case $401
			Select EventSource()
				Case button
				If current = j Then current = i Else current = j
					SetButtonImage button,current
			End Select
		Case $803
			End
	End Select
Forever
Const BM_SETIMAGE=247
Const IMAGE_BITMAP=0
Const IMAGE_ICON=1
Const BS_BITMAP=128
Const GWL_STYLE=-16
Const LR_DEFAULTSIZE=64
Const LR_LOADFROMFILE=16
Const LR_SHARED=32768

Function SetButtonImage(button,NEWimage)
hbutton=QueryObject(button,1)
flags=GetWindowLong(hbutton,GWL_STYLE)
If (Not image = NEWimage)
		If Not (BS_BITMAP And flags) SetWindowLong hbutton,GWL_STYLE,flags+BS_BITMAP
	Else
		If (BS_BITMAP And flags) SetWindowLong hbutton,GWL_STYLE,flags-BS_BITMAP
		Return
EndIf
w=ImageWidth(NEWimage)
h=ImageHeight(NEWimage)
ibuffer=ImageBuffer(NEWimage)
LockBuffer ibuffer
pixels=CreateBank(w*h*4)
For y=0 To h-1
	For x=0 To w-1
		PokeInt pixels,4*(y*w+x)+0,ReadPixelFast(x,y,ibuffer)
	Next
Next
UnlockBuffer ibuffer
tmp=CreateBitmap(w,h,1,32,pixels)
FreeBank pixels
If Not tmp Return
oldimage=SendMessage(hbutton,BM_SETIMAGE,IMAGE_BITMAP,tmp)
If oldimage DeleteObject(oldimage)
image = NEWimage
Return True
End Function