REAL ImageButton control!

BlitzPlus Forums/BlitzPlus Programming/REAL ImageButton control!

One simple command:
SetButtonImage(button,image)



I chose this so that you can change the button image at any time. The function accepts a regular old Blitz image. Use SetButtonImage(button,0) to change the ImageButton back into a regular button.

win=CreateWindow("ImageButton",200,200,400,300,0,3)

i=LoadImage("blitzlogo.bmp")
w=ImageWidth(i)
h=ImageHeight(i)

button=CreateButton("",(ClientWidth(win)-w)/2,(ClientHeight(win)-h)/2,w,h,win)
SetButtonImage button,i

Repeat
Until WaitEvent()=$803


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,image)
hbutton=QueryObject(button,1)
flags=GetWindowLong(hbutton,GWL_STYLE)
If image
	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(image)
h=ImageHeight(image)
ibuffer=ImageBuffer(image)
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
i=CreateBitmap(w,h,1,32,pixels)
FreeBank pixels
If Not i Return
oldimage=SendMessage(hbutton,BM_SETIMAGE,IMAGE_BITMAP,i)
If oldimage DeleteObject(oldimage)
Return True
End Function