SetButtonPixmap() ?

BlitzMax Forums/BlitzMax GUI Programming/SetButtonPixmap() ?

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.

This is what I use:
SuperStrict

?win32
Extern "Win32"
	Function SetWindowLong:Int(HWND:Int,nIndex:Int,dwNewLong:Int) = "SetWindowLongA@12"
	Function GetWindowLong:Int(HWND:Int,nIndex:Int) = "GetWindowLongA@8"
	Function _LoadImage:Int(hInstance:Int,lpcstr:Byte Ptr,a:Int,b:Int,c:Int,d:Int) = "LoadImageA@24"
End Extern

Const GWL_STYLE:Int		= -16
Const GWL_HINSTANCE:Int  = -6

Const BM_SETIMAGE:Int	= 247

Const BS_ICON:Int		= 64
Const BS_BITMAP:Int		= 128

Const IMAGE_BITMAP:Int	= 0
Const IMAGE_ICON:Int	= 1

Const LR_LOADFROMFILE:Int = 16
Const LR_LOADTRANSPARENT:Int = 32
Const LR_DEFAULTSIZE:Int	= 64
?

Function SetButtonIcon(button:TGadget,url:String)
	
	?win32
	If TWin32Gadget(button).class <> GADGET_BUTTON Then Return

	Local hwnd:Int	= QueryGadget(Button,QUERY_HWND)
	Local mode:Int = IMAGE_BITMAP
	Local style:Int = BS_BITMAP
	
	If ExtractExt(url).ToLower() = "ico"
		mode = IMAGE_ICON
		style = BS_ICON
	EndIf
	
	Local hInstance:Int = GetWindowLong(hwnd,GWL_HINSTANCE)
	Local hIcon:Int = _LoadImage(hInstance, url.ToCString(), mode, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT | LR_DEFAULTSIZE )

	If hIcon = 0 Then Return
	SetWindowLong(hwnd,GWL_STYLE,GetWindowLong(hwnd,GWL_STYLE) | style)

	Local old:Int = SendMessageA(hwnd,BM_SETIMAGE,mode,hIcon)
	If old Then	DeleteObject(old)
	?
	
End Function

Loads a .bmp or .ico file and applies it to the button. If you use .ico files, it will use the transparency mask as well.

I'm not sure if it's possible to get both text and icon, any combo I tried gives either one or the other, regardless of what msdn says.