How to set a background image for a window?

Everything else that doesn't fall into one of the other PB categories.
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

How to set a background image for a window?

Post by berryoxide »

Hi!

I've been looking endlessly through the forums but still can't find what I want... I'd like to have this working on both Windows and Linux and it is a simple "hello world" like application for which I want to have a custom background image. All gadgets and stuff then becomes placed "on top" of the background image.

Can anyone point me in the right direction? Still a noob in these things, but any help is appreciated! Thanks!
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Try this

Code: Select all

OpenWindow(0,0,0,200,200,"",$ca0001)
CreateImage(0,200,200,32)
StartDrawing(ImageOutput(0))
For y=0 To 200
Line(0,y,200,0,RGB(y+50,0,0))
Next
StopDrawing()
CreateGadgetList(WindowID(0))
ImageGadget(1,0,0,200,200,ImageID(0))
DisableGadget(1,1)
ButtonGadget(2,10,10,180,30,"Press me")
StringGadget(3,10,100,180,20,"Type in here")
Repeat
Until WaitWindowEvent()=16
I think there are some issues with panelgadgets() or containers or something but it's a start. :wink:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

dz dz dz! :?

Derek, how could you?
Gadgets don't have a z-order, there have been hundrets of discussions about it,
and it was ALWAYS! said that
thou shalt not put Gadgets over each other!


@berryoxide

you have to go the API way, there is no cross-platform solution for a background image.
oh... and have a nice day.
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post by berryoxide »

Kaeru Gaman wrote:dz dz dz! :?

Derek, how could you?
Gadgets don't have a z-order, there have been hundrets of discussions about it,
and it was ALWAYS! said that
thou shalt not put Gadgets over each other!


@berryoxide

you have to go the API way, there is no cross-platform solution for a background image.
The API way... Well ok, as long as it runs under Wine I guess it's good. But I still don't know how to do that the "API way" let alone understand that MessageBox_ is a function of the API.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Kaeru Gaman wrote:dz dz dz! :?

Derek, how could you?
Gadgets don't have a z-order, there have been hundrets of discussions about it,
and it was ALWAYS! said that
thou shalt not put Gadgets over each other!
Yeah, but if you disable the imagegadget then it works. :wink:

That's good enough for me. :lol:
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

The same code but with a gadget reporter, seems to work ok.

Code: Select all

OpenWindow(0,0,0,200,200,"",$ca0001) 
CreateImage(0,200,200,32) 
StartDrawing(ImageOutput(0)) 
For y=0 To 200 
Line(0,y,200,0,RGB(y+50,0,0)) 
Next 
StopDrawing() 
CreateGadgetList(WindowID(0)) 
ImageGadget(1,0,0,200,200,ImageID(0)) 
DisableGadget(1,1) 
ButtonGadget(2,10,10,180,30,"Press me") 
StringGadget(3,10,100,180,20,"Type in here") 
Repeat 
e=WaitWindowEvent()
If e=#PB_Event_Gadget
gad=EventGadget()
Debug Gad
EndIf
Until e=16 
@berryoxide, does it work on your setup?
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post by berryoxide »

@Derek: It indeed does and fantastically! I wrapped the code in a procedure "SetWindowBackground(WindowID, ImageID)" so if there is a better approach I can swiftly change the code.

I'm getting the grip of this and I really like the logic behind PureBasic :)
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Glad I could help, I have no idea if it works on linux though and as Kaeru said, you might get into problems with the z-order of gadgets so bear it in mind. :)
Last edited by Derek on Sat Aug 30, 2008 9:33 pm, edited 1 time in total.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

@berryoxide

Code: Select all

MessageBox_(#Window_Num_or_Null, "This is something you want to say", "Information - this is the title (caption)", #MB_ICONEXCLAMATION)

MessageBox_(#Null, "This is something you want to say", "Information - this is the title (caption)", #MB_ICONEXCLAMATION)

http://msdn.microsoft.com/en-us/library/ms645505.aspx

berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post by berryoxide »

SFSxOI wrote:

Code: Select all

MessageBox_(#Window_0, "This is something you want to say", "Information - this is the title (caption)", #MB_ICONEXCLAMATION)


http://msdn.microsoft.com/en-us/library/ms645505.aspx

No no you got me wrong... I know that already, it's just that I found Visual Basic code which I wanted to port to PureBasic for fun, and I by mistake added an underscore (finger slipped on the shift key) just before the parentheses began in front of the MessageBox procedure. That mistake was in there so long until I compiled and in my debugging phrase I saw that the underscore didn't make an error... Well, I tried to do it again and "o m g" it works! :D
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

http://www.purebasic.fr/german/viewtopi ... 497#210497

Code: Select all

OpenWindow(0,0,0,520,400,"void",#WS_OVERLAPPEDWINDOW | #WS_VISIBLE | 1)

LoadImage(0,#PB_Compiler_Home + "Examples\Sources\Data\Background.bmp")
hbrBackground = CreatePatternBrush_(ImageID(0))
SetClassLong_(WindowID(0),#GCL_HBRBACKGROUND,hbrBackground)

HideWindow(0,0)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend

DeleteObject_(hbrBackground)

Code: Select all

; German forum:
; Author: Unknown (updated for PB4.00 by blbltheworm), modified by hardfalcon
; OS: Windows
; Demo: No

hWnd = OpenWindow(0, 100, 200, 250, 260, "Image brush", #PB_Window_SystemMenu )

  UsePNGImageDecoder()
  UseJPEGImageDecoder()
  hImage = LoadImage(#PB_Any,"Z:\usr\share\pixmaps\backgrounds\gnome\background-default.jpg")
  Debug ImageDepth(hImage)
 
  ExamineDesktops()
  Debug DesktopDepth(0)
  If ImageDepth(hImage) <> DesktopDepth(0) ;sonst gehts nicht, Farbtiefe des Bildes MUSS der Desktopfarbtiefe entsprechen!
    hImage2 = CreateImage(#PB_Any,ImageWidth(hImage),ImageHeight(hImage),#PB_Image_DisplayFormat)
    Debug ImageDepth(hImage2)
    StartDrawing(ImageOutput(hImage2))
      Debug DrawImage(ImageID(hImage),0,0)
    StopDrawing()
    FreeImage(hImage)
    hImage = hImage2
  EndIf
 
  hBrush = CreatePatternBrush_(ImageID(hImage))
  SetClassLong_(hWnd, #GCL_HBRBACKGROUND, hBrush)
  InvalidateRect_(hWnd, #Null, #True)

  Repeat
    EventID.l = WaitWindowEvent()

    If EventID = #PB_Event_CloseWindow
      Quit = 1
    EndIf

  Until Quit = 1
 
  DeleteObject_(hBrush) ; Brush löschen/freigeben!
  FreeImage(hImage)
 
End
http://www.purebasic.fr/english/viewtopic.php?t=32162

Derek wrote:Yeah, but if you disable the imagegadget then it works.
interesting, wonder why I didn't read about it yet... where you got this from?
oh... and have a nice day.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Kaeru Gaman wrote:interesting, wonder why I didn't read about it yet... where you got this from?
Read this, scroll down to netmaestro's comment.

http://www.purebasic.fr/english/viewtop ... magegadget

If it's good enough for netmaestro then it's more than good enough for me. :lol: :lol:

But the code you posted above is good as it seems to be doing what Sparkie says in the same link above, but as Sparkie says it's not cross-platform whereas the disable gadget trick is. :wink:
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post by berryoxide »

I'll stick with Derek's suggestion for now... Works in GNU/Linux as well by the way. Just QEMUed into OSx86 and it works there too :)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Berikco wrote:The grid of the Visual Designer is just a disabled imagegadget :D
Thanks Derek, and if it's good enough for Berikco it's good enough for me :wink:
BERESHEIT
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post by berryoxide »

If it's good enough for Berikco, it's good enough for netmaestro, and if it's good enough for netmaestro it's good enough for Derek, and if it's good enough for Derek it's good enough for me.

Sorry, didn't have something else to do :)
Post Reply