Page 1 of 2

A simple resizable image as window bg

Posted: Thu May 30, 2019 10:12 am
by scanfff
Is there a simple easy to use an image as the window or dialogs background. I'm sure using win api it's possible. But feel like I'm missing something, surely there's an easy way in pb.


Thanks just started out with pb.
Aaron

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 10:49 am
by ShadowStorm
ImageGadget !

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 10:56 am
by scanfff
ShadowStorm wrote:ImageGadget !
I tired that but it does not draw on the window, only frames with in it. am I missing a flag.

Thanks,
Aaron

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 11:08 am
by Bisonte
He means like this :

Code: Select all

LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")

OpenWindow(0, 0, 0, 255, 255, "Test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)

ImageGadget(1, 0, 0, 255, 255, ImageID(1))
DisableGadget(1, #True)

ButtonGadget(2, 20, 20, 100, 25, "Button")

Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_CloseWindow
      Break
  EndSelect
  
ForEver

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 11:37 am
by scanfff
Bisonte wrote:He means like this :

Code: Select all

LoadImage(1, #PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")

OpenWindow(0, 0, 0, 255, 255, "Test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)

ImageGadget(1, 0, 0, 255, 255, ImageID(1))
DisableGadget(1, #True)

ButtonGadget(2, 20, 20, 100, 25, "Button")

Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_CloseWindow
      Break
  EndSelect
  
ForEver
Thanks, I''l try it out.. I've been having the weird "cannot load compiler error on this computer. Everty thing was working great, I'm sure it was windows updates :( about 3 weeks ago my main computer can't compile PB code. I've reinstalled several time. I did noticed a thread on this but the link I found was old.

https://drive.google.com/file/d/11wiLNm ... AblWLQIHC/

I don't think I can embed the problems. but a screeny is on my drive above.

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 2:34 pm
by mk-soft
Thanks Shardik

Update

Code: Select all

;-TOP
; Comment: SetWindowBackgroundImage
; Authors: ts-soft (Windows), Shardik (Linux , masOS)
; Create : ??.??.2014
; Update : 30.05.2019
; Link   : https://www.purebasic.fr/german/viewtopic.php?f=16&t=28467&start=10#p352932

; OS     : All

EnableExplicit

InitNetwork()
UsePNGImageDecoder()

Procedure SetWindowBackgroundImage(hWnd.i, hImage.i)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected Container.I
      Protected *Name
      Protected Widget.I = gtk_bin_get_child_(hWnd)
      
      Container = g_list_nth_data_(gtk_container_get_children_(0 +
                                                               gtk_bin_get_child_(WindowID(0))), 0)
      
      If Container
        If PeekS(gtk_widget_get_name_(Container), -1, #PB_UTF8) = "GtkFixed"
          gtk_fixed_put_(Container, gtk_image_new_from_pixbuf_(hImage), 0, 0)
        Else
          gtk_layout_put_(Container, gtk_image_new_from_pixbuf_(hImage), 0, 0)
        EndIf
        
        gtk_widget_show_all_(hWnd)
      EndIf
      
    CompilerCase #PB_OS_Windows
      Protected hBrush = CreatePatternBrush_(hImage)
      If hBrush
        SetClassLongPtr_(hWnd, #GCL_HBRBACKGROUND, hBrush)
        InvalidateRect_(hWnd, 0, #True)
        UpdateWindow_(hWnd)
      EndIf
      
    CompilerCase #PB_OS_MacOS
      CocoaMessage(0, hWnd, "setBackgroundColor:",
                   CocoaMessage(0, 0, "NSColor colorWithPatternImage:", hImage))
  CompilerEndSelect
EndProcedure

If LoadImage(0, #PB_Compiler_Home + "examples/3d/Data/Textures/spheremap.png")
  OpenWindow(0, 100, 100, ImageWidth(0), ImageHeight(0),
             "Window with background image")
  SetWindowBackgroundImage(WindowID(0), ImageID(0))
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 6:42 pm
by Bisonte
scanfff wrote:I've been having the weird "cannot load compiler error on this computer. Everty thing was working great, I'm sure it was windows updates :( about 3 weeks ago my main computer can't compile PB code. I've reinstalled several time. I did noticed a thread on this but the link I found was old.

https://drive.google.com/file/d/11wiLNm ... AblWLQIHC/

I don't think I can embed the problems. but a screeny is on my drive above.
This mostly is the result of an av system. Put Purebasic and his compiler to the whitelist.

Re: A simple resizable image as window bg

Posted: Thu May 30, 2019 7:34 pm
by Shardik
mk-soft wrote:Windows, macOS, Linux GTK2
Author Unknown
The Windows part is from ts-soft, the Linux and Mac parts are from me. Your posted example is from the German forum. In PB 5.31 and 5.40 that example worked in Linux with GTK2 and GTK3. But because of internal changes in PureBasic the GTK3 subsystem now utilizes GtkLayout instead of GtkFixed and I therefore have modified my example to support both GTK2 and GTK3 again...:wink:

Re: A simple resizable image as window bg

Posted: Fri May 31, 2019 5:50 am
by scanfff
Bisonte wrote:
scanfff wrote:I've been having the weird "cannot load compiler error on this computer. Everty thing was working great, I'm sure it was windows updates :( about 3 weeks ago my main computer can't compile PB code. I've reinstalled several time. I did noticed a thread on this but the link I found was old.

https://drive.google.com/file/d/11wiLNm ... AblWLQIHC/

I don't think I can embed the problems. but a screeny is on my drive above.
This mostly is the result of an av system. Put Purebasic and his compiler to the whitelist.
I've always had AVG, it only freaks about the compiled binary. Never before about the environment. I whitelisted it, no change so I turned off AVG still the same. The only difference I can think of is I did upgrade ( well just changed the product key ) win10 from pro to win10 enterprise. but meh I also did that on my laptop and pb works fine.

Re: A simple resizable image as window bg

Posted: Fri May 31, 2019 4:09 pm
by scanfff
mk-soft wrote:Thanks Shardik

Update

Code: Select all

;-TOP
; Comment: SetWindowBackgroundImage
; Authors: ts-soft (Windows), Shardik (Linux , masOS)
; Create : ??.??.2014
; Update : 30.05.2019
; Link   : https://www.purebasic.fr/german/viewtopic.php?f=16&t=28467&start=10#p352932

; OS     : All

EnableExplicit

InitNetwork()
UsePNGImageDecoder()

Procedure SetWindowBackgroundImage(hWnd.i, hImage.i)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected Container.I
      Protected *Name
      Protected Widget.I = gtk_bin_get_child_(hWnd)
      
      Container = g_list_nth_data_(gtk_container_get_children_(0 +
                                                               gtk_bin_get_child_(WindowID(0))), 0)
      
      If Container
        If PeekS(gtk_widget_get_name_(Container), -1, #PB_UTF8) = "GtkFixed"
          gtk_fixed_put_(Container, gtk_image_new_from_pixbuf_(hImage), 0, 0)
        Else
          gtk_layout_put_(Container, gtk_image_new_from_pixbuf_(hImage), 0, 0)
        EndIf
        
        gtk_widget_show_all_(hWnd)
      EndIf
      
    CompilerCase #PB_OS_Windows
      Protected hBrush = CreatePatternBrush_(hImage)
      If hBrush
        SetClassLongPtr_(hWnd, #GCL_HBRBACKGROUND, hBrush)
        InvalidateRect_(hWnd, 0, #True)
        UpdateWindow_(hWnd)
      EndIf
      
    CompilerCase #PB_OS_MacOS
      CocoaMessage(0, hWnd, "setBackgroundColor:",
                   CocoaMessage(0, 0, "NSColor colorWithPatternImage:", hImage))
  CompilerEndSelect
EndProcedure

If LoadImage(0, #PB_Compiler_Home + "examples/3d/Data/Textures/spheremap.png")
  OpenWindow(0, 100, 100, ImageWidth(0), ImageHeight(0),
             "Window with background image")
  SetWindowBackgroundImage(WindowID(0), ImageID(0))
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf



Thanks I got it working, I installed the 5.71 Beta and compiler loading error is no more. Funny the release 5.70 seems glitcher to me :P

Thanks for all the help :)

Re: A simple resizable image as window bg

Posted: Fri May 31, 2019 10:32 pm
by Justin
You can also use a canvas container as the window background and use DrawImage(), without api.

Re: A simple resizable image as window bg

Posted: Fri May 31, 2019 11:24 pm
by scanfff
Justin wrote:You can also use a canvas container as the window background and use DrawImage(), without api.
nice to know, can you point me to an example. Thanks :)

Re: A simple resizable image as window bg

Posted: Sat Jun 01, 2019 1:31 pm
by Shardik
scanfff wrote:
Justin wrote:You can also use a canvas container as the window background and use DrawImage(), without api.
nice to know, can you point me to an example. Thanks :)
Justin is right. The flag #PB_Canvas_Container is relatively new and was implemented in PureBASIC 5.60 in March 2017.

Code: Select all

UsePNGImageDecoder()

Procedure SetWindowBackgroundImage(CanvasID.I, ImageID.I)
  If StartDrawing(CanvasOutput(CanvasID))
    DrawImage(ImageID(ImageID), 0, 0, ImageWidth(ImageID),
      ImageHeight(ImageID))
    StopDrawing()
  EndIf
EndProcedure

If LoadImage(0, #PB_Compiler_Home + "examples/3d/Data/Textures/spheremap.png")
  OpenWindow(0, 100, 100, ImageWidth(0), ImageHeight(0),
    "Window with background image")
  CanvasGadget(0, 0, 0, ImageWidth(0), ImageHeight(0), #PB_Canvas_Container)
  SetWindowBackgroundImage(0, 0)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: A simple resizable image as window bg

Posted: Sun Jun 02, 2019 6:57 pm
by wombats
To expand on Shardik's example, if you want a resizable window, bind the CanvasGadget's #PB_EventType_Resize event to a procedure and do the drawing there instead of when you resize the CanvasGadget. I have experienced issues (mostly with the Dialog library, admittedly), that were resolved by doing this.

Code: Select all

UsePNGImageDecoder()

Procedure SetWindowBackgroundImage(CanvasID.I, ImageID.I)
  If StartDrawing(CanvasOutput(CanvasID))
    DrawImage(ImageID(ImageID), 0, 0, GadgetWidth(CanvasID),
      GadgetHeight(CanvasID))
    StopDrawing()
  EndIf
EndProcedure

Procedure OnWindowResize()
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0))
EndProcedure

Procedure OnCanvasResize()
  SetWindowBackgroundImage(0, 0)
EndProcedure

If LoadImage(0, #PB_Compiler_Home + "examples/3d/Data/Textures/spheremap.png")
  OpenWindow(0, 100, 100, ImageWidth(0), ImageHeight(0),
    "Window with background image", #PB_Window_SizeGadget)
  CanvasGadget(0, 0, 0, ImageWidth(0), ImageHeight(0), #PB_Canvas_Container)
  BindEvent(#PB_Event_SizeWindow, @OnWindowResize(), 0)
  BindGadgetEvent(0, @OnCanvasResize(), #PB_EventType_Resize)
  SetWindowBackgroundImage(0, 0)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: A simple resizable image as window bg

Posted: Sun Jun 02, 2019 7:11 pm
by Mijikai
wombats wrote:To expand on Shardik's example, if you want a resizable window, bind the CanvasGadget's #PB_EventType_Resize event to a procedure and do the drawing there instead of when you resize the CanvasGadget. I have experienced issues (mostly with the Dialog library, admittedly), that were resolved by doing this.

Code: Select all

...
This does not work for me:
Image