Code: Select all
;========================================================
; SetBackgroundImage() sets the background image of a
; window or container gadget through API functions and
; eliminates the need of using disabled image gadgets
;
; for WINDOWS ONLY
;
; tested with PureBasic v5.31 on Windows 8.1 Pro (x64)
;
; by TI-994A - free to use, improve, share...
;
; 30th November 2014
;========================================================
Procedure SetBackgroundImage(containerNo, imageNo)
If IsWindow(containerNo)
hWnd = WindowID(containerNo)
imgWidth = WindowWidth(containerNo)
imgHeight = WindowHeight(containerNo)
result = #True
ElseIf IsGadget(containerNo)
hWnd = GadgetID(containerNo)
imgWidth = GadgetWidth(containerNo)
imgHeight = GadgetHeight(containerNo)
result = #True
Else
result = #False
EndIf
If result
ResizeImage(imageNo, imgWidth, imgHeight)
image = CreatePatternBrush_(ImageID(imageNo))
If image
SetClassLongPtr_(hWnd, #GCL_HBRBACKGROUND, image)
RedrawWindow_(hWnd, #Null, #Null, #RDW_INVALIDATE)
result = #True
Else
result = #False
EndIf
EndIf
ProcedureReturn result
EndProcedure
;demo code
Enumeration
#window1
#window2
#container
#bgImage
#text1
#text2
EndEnumeration
UseJPEGImageDecoder()
UsePNGImageDecoder()
fileTypes.s = "Images (*.bmp, *.jpg, *.png)|*.jpg;*.png;*.bmp;"
LoadImage(#bgImage, OpenFileRequester("Select background image:", "", fileTypes, 0))
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#window1, #PB_Any, #PB_Any, 300, 300, "SetBackgroundImage()", wFlags)
ResizeWindow(#window1, WindowX(#window1) - 160, #PB_Ignore, #PB_Ignore, #PB_Ignore)
SetBackgroundImage(#window1, #bgImage)
TextGadget(#text1, 0, 250, 300, 20, "background image on window", #PB_Text_Center)
OpenWindow(#window2, #PB_Any, #PB_Any, 300, 300, "SetBackgroundImage()", wFlags)
ResizeWindow(#window2, WindowX(#window2) + 160, #PB_Ignore, #PB_Ignore, #PB_Ignore)
ContainerGadget(#container, 0, 0, 300, 300)
SetBackgroundImage(#container, #bgImage)
TextGadget(#text2, 0, 250, 300, 20, "background image on container", #PB_Text_Center)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
