Page 1 of 1

Function to set background image of window or container

Posted: Sun Nov 30, 2014 5:16 pm
by TI-994A
A small, encapsulated, Windows-only function to set background images onto a window or container gadget:

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 
Suggestions, feedback, and improvements are most welcome. Thank you. :)

Re: Function to set background image of window or container

Posted: Sun Nov 30, 2014 5:58 pm
by IdeasVacuum
Another excellent and very useful code TI-994A. You are filling the gaps left behind by Gnozal's libs? :wink:

Re: Function to set background image of window or container

Posted: Sun Nov 30, 2014 8:42 pm
by BasicallyPure
Very good!

Thanks for posting.

Re: Function to set background image of window or container

Posted: Tue Dec 02, 2014 6:41 am
by TI-994A
Thank you guys - truly appreciated. :)

Re: Function to set background image of window or container

Posted: Tue Dec 02, 2014 10:45 pm
by minimy
Thanks, very nice!

Re: Function to set background image of window or container

Posted: Wed Dec 03, 2014 5:00 am
by Amilcar Matos
Thank you TI-994A. Very useful. Happy holidays!

Re: Function to set background image of window or container

Posted: Wed Dec 03, 2014 6:03 am
by Fangbeast
IdeasVacuum wrote:Another excellent and very useful code TI-994A. You are filling the gaps left behind by Gnozal's libs? :wink:
And this is the problem with libs. Authors leave (or get run over by feral girl scouts with rock cookies) and we get so used to the libs that we can no longer unbundle them from big projects. Then what do we do?

I wish all of his stuff would be replaced like this because I haven't the talent to replace 98% of it by myself and I was a heavy user for a long while.

Re: Function to set background image of window or container

Posted: Fri Dec 05, 2014 8:22 pm
by mestnyi
For Linux and Window :wink:

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 and LINUX
;
;  tested with PureBasic v5.31 on Windows 8.1 Pro (x64)
;
;  by TI-994A - free to use, improve, share...
;
;  30th November 2014
;========================================================
ProcedureDLL.i SetWindowBackGroundImage(Image, Window) 
  Protected WindowID=WindowID(Window)
  
  Protected imgWidth = WindowWidth(Window)
  Protected imgHeight = WindowHeight(Window)
  ResizeImage(Image, imgWidth, imgHeight)
  
  ImageID=ImageID(Image)
  
  If WindowID
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux    
        Protected *Background, FixedBox.I, *Style.GtkStyle
        gdk_pixbuf_render_pixmap_and_mask_(ImageID, @*Background, 0, 0)
        *Style = gtk_style_new_()
        *Style\bg_pixmap[0] = *Background
        FixedBox = g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID)), 0)
        gtk_widget_set_style_(FixedBox, *Style)
      CompilerCase #PB_OS_Windows
        Protected BrushImageID = CreatePatternBrush_(ImageID)
        SetClassLongPtr_(WindowID, #GCL_HBRBACKGROUND, BrushImageID)
        ;InvalidateRect_(WindowID, 0, #True) 
        RedrawWindow_(WindowID, #Null, #Null, #RDW_INVALIDATE)
    CompilerEndSelect  
  EndIf
EndProcedure

ProcedureDLL.i SetGadgetBackGroundImage(Image, Gadget) 
  Protected GadgetID = GadgetID(Gadget)
  
  Protected imgWidth = GadgetWidth(Gadget)
  Protected imgHeight = GadgetHeight(Gadget)
  ResizeImage(Image, imgWidth, imgHeight)
  
  ImageID=ImageID(Image)
  If GadgetID
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux    
        Protected *Background, FixedBox.I, *Style.GtkStyle
        gdk_pixbuf_render_pixmap_and_mask_(ImageID, @*Background, 0, 0)
        *Style = gtk_style_new_()
        *Style\bg_pixmap[0] = *Background
        gtk_widget_set_style_(GadgetID, *Style)
      CompilerCase #PB_OS_Windows
        Protected BrushImageID = CreatePatternBrush_(ImageID)
        SetClassLongPtr_(GadgetID, #GCL_HBRBACKGROUND, BrushImageID)
        ;InvalidateRect_(WindowID, 0, #True) 
        RedrawWindow_(GadgetID, #Null, #Null, #RDW_INVALIDATE)
    CompilerEndSelect  
  EndIf
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)
SetWindowBackGroundImage( #bgImage,#window1)
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)
SetGadgetBackGroundImage(#bgImage,#container )
TextGadget(#text2, 0, 250, 300, 20, "background image on container", #PB_Text_Center)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend

Re: Function to set background image of window or container

Posted: Fri Dec 05, 2014 11:31 pm
by VB6_to_PBx
mestnyi
Thanks for sharing !

works well on Win 7-64bit and PB v5.20LTS (x86)

Re: Function to set background image of window or container

Posted: Thu Apr 07, 2016 10:15 am
by mestnyi
My example to work purebasic 531 does not work now purebasic 542
How to fix it?

Re: Function to set background image of window or container

Posted: Thu Apr 07, 2016 10:30 am
by infratec
Hi,

just tested your code in PB 5.42 x86 on win7 x64: it works.

But I run into a problem at the first time:
I selected a picture, but the load failed.

So I modified your example:

Code: Select all

If LoadImage(#bgImage, OpenFileRequester("Select background image:", "", fileTypes, 0)) = 0
  MessageRequester("Error", "Was not able to load the picture.")
  End
EndIf
Bernd

Re: Function to set background image of window or container

Posted: Thu Apr 07, 2016 10:34 am
by infratec
Very strange:

With

Code: Select all

OpenFileRequester("Select background image:", "", "jpg|*.jpg", 0)
I see a file named screen2.jpg of 14k in c:\
If I look with totalcommander (hidden and systemfiles are enabled), I see nothing of it.
Also Explorer does not show it.

Re: Function to set background image of window or container

Posted: Thu Apr 07, 2016 10:48 am
by mestnyi
Sorry does not work on Linux
Writes "purebasic.o: In function` PB_EndFunctions':
(.text + 0x32e): undefined reference to `gdk_pixbuf_render_pixmap_and_mask '
collect2: error: ld returned 1 exit status "