Function to set background image of window or container

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Function to set background image of window or container

Post 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. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Function to set background image of window or container

Post by IdeasVacuum »

Another excellent and very useful code TI-994A. You are filling the gaps left behind by Gnozal's libs? :wink:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Function to set background image of window or container

Post by BasicallyPure »

Very good!

Thanks for posting.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Function to set background image of window or container

Post by TI-994A »

Thank you guys - truly appreciated. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Function to set background image of window or container

Post by minimy »

Thanks, very nice!
If translation=Error: reply="Sorry, Im Spanish": Endif
Amilcar Matos
User
User
Posts: 43
Joined: Thu Nov 27, 2014 3:10 pm
Location: San Juan, Puerto Rico

Re: Function to set background image of window or container

Post by Amilcar Matos »

Thank you TI-994A. Very useful. Happy holidays!
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Function to set background image of window or container

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: Function to set background image of window or container

Post 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
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Function to set background image of window or container

Post by VB6_to_PBx »

mestnyi
Thanks for sharing !

works well on Win 7-64bit and PB v5.20LTS (x86)
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: Function to set background image of window or container

Post by mestnyi »

My example to work purebasic 531 does not work now purebasic 542
How to fix it?
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Function to set background image of window or container

Post 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
Last edited by infratec on Thu Apr 07, 2016 10:55 am, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Function to set background image of window or container

Post 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.
Last edited by infratec on Thu Apr 07, 2016 10:55 am, edited 1 time in total.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: Function to set background image of window or container

Post 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 "
Post Reply