A simple resizable image as window bg

Windows specific forum
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

A simple resizable image as window bg

Post 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
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: A simple resizable image as window bg

Post by ShadowStorm »

ImageGadget !
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

Re: A simple resizable image as window bg

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: A simple resizable image as window bg

Post 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
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

Re: A simple resizable image as window bg

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: A simple resizable image as window bg

Post 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
Last edited by mk-soft on Thu May 30, 2019 9:26 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: A simple resizable image as window bg

Post 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.
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A simple resizable image as window bg

Post 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:
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

Re: A simple resizable image as window bg

Post 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.
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

Re: A simple resizable image as window bg

Post 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 :)
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: A simple resizable image as window bg

Post by Justin »

You can also use a canvas container as the window background and use DrawImage(), without api.
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

Re: A simple resizable image as window bg

Post 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 :)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A simple resizable image as window bg

Post 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
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: A simple resizable image as window bg

Post 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
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: A simple resizable image as window bg

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