Setting a container gadget color affects ALL containers

Everything else that doesn't fall into one of the other PB categories.
halo
Enthusiast
Enthusiast
Posts: 104
Joined: Mon Jan 26, 2004 2:49 am

Setting a container gadget color affects ALL containers

Post by halo »

I am using this code to change the background color of a container gadget. Unfortunately, it affects all container gadgets, even the ones in the parent application window. How do I resolve this?

Code: Select all

Procedure SetGadgetColor(object,r,g,b)
hBrush.l=CreateSolidBrush_(RGB(r,g,b))
SetClassLong_(object,#GCL_HBRBACKGROUND,hBrush)
InvalidateRect_(object,0,#TRUE)
EndProcedure
Thanks.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

SetClassLong_(object,#GCL_HBRBACKGROUND,hBrush) changes the background brush for all gadgets (ContainerGadgets) that are of the class (PureContainer). You could either create your own class for each ContainerGadget and then use the SetClassLong_() or you can try this code.

I'm not sure if this is the preferred method, but it does work here with minimal testing on WinXPhome and PB 3.92.

Code: Select all

blueBrush.l = CreateSolidBrush_(RGB(0, 0, 255)) 
greenBrush.l = CreateSolidBrush_(RGB(0, 255, 0)) 
Global blueBrush, greenBrush

Procedure myWindowCallback(hwnd, msg, wparam, lparam)
  result = #PB_ProcessPureBasicEvents
  
  Select msg
    
    Case #WM_ERASEBKGND 
      container.RECT
      If IsGadget(0) And hwnd = GadgetID(0)
        container\left = 0
        container\top = 0
        container\right = GadgetWidth(0)
        container\bottom = GadgetHeight(0)
      FillRect_(wparam, container, blueBrush)
      result = 1
    EndIf
    
    If IsGadget(2) And hwnd = GadgetID(2)
      container\left = 0
      container\top = 0
      container\right = GadgetWidth(2)
      container\bottom = GadgetHeight(2)
      FillRect_(wparam, container, greenBrush)
      result = 1
    EndIf
    
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 400, 230, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget,"Colored ContainerGadgets") And CreateGadgetList(WindowID(0)) 
  SetWindowCallback(@myWindowCallback())
  ContainerGadget (0, 10, 10, 380, 100) 
  ; --> This sets a #Null brush for all ContainerGadgets
  SetClassLong_(GadgetID(0), #GCL_HBRBACKGROUND, 0)
  ButtonGadget(1, 90, 40, 180, 20,"Button in Blue Container") 
  CloseGadgetList() 
  ContainerGadget (2, 10, 120, 380, 100) 
  ButtonGadget(3, 90, 40, 180, 20,"Button in Green Container") 
  CloseGadgetList() 
  
  Repeat
    
    event = WaitWindowEvent()
    
    Select event
      
      Case #PB_EventSizeWindow
        ResizeGadget(0, -1, -1, WindowWidth()-20, -1)
        ResizeGadget(2, -1, -1, WindowWidth()-20, -1)
        
    EndSelect
    
  Until event = #PB_Event_CloseWindow 
  
  DeleteObject_(blueBrush)
  DeleteObject_(greenBrush)
  
EndIf 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
halo
Enthusiast
Enthusiast
Posts: 104
Joined: Mon Jan 26, 2004 2:49 am

Post by halo »

Thank you!

No offense, but you PB programmers write the worst spaghetti code I have ever seen. If I wrote code like that, I would spend more time formatting than thinking:

Code: Select all

Global blueBrush
Global greenBrush
blueBrush=CreateSolidBrush_(RGB(0,0,255))
greenBrush=CreateSolidBrush_(RGB(0,255,0))

Procedure myWindowCallback(hwnd, msg, wparam, lparam)
result=#PB_ProcessPureBasicEvents
Select msg
  Case #WM_ERASEBKGND
    container.RECT
    Select hwnd
      Case GadgetID(0)
        container\right=GadgetWidth(0)
        container\bottom=GadgetHeight(0)
        FillRect_(wparam,container,blueBrush)
        result=1
      Case GadgetID(2)
        container\right=GadgetWidth(2)
        container\bottom=GadgetHeight(2)
        FillRect_(wparam,container,greenBrush)
        result=1
      EndSelect
  EndSelect
ProcedureReturn result
EndProcedure

win=OpenWindow(0,0,0,400,230,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget,"Colored ContainerGadgets")
CreateGadgetList(win)
ContainerGadget (0,10,10,380,100)
SetClassLong_(GadgetID(0),#GCL_HBRBACKGROUND,0)
ButtonGadget(1,90,40,180,20,"Button in Blue Container")
CloseGadgetList()
ContainerGadget (2,10,120,380,100)
ButtonGadget(3,90,40,180,20,"Button in Green Container")
CloseGadgetList()
SetWindowCallback(@myWindowCallback())

Repeat
  Select WaitWindowEvent()
    Case #PB_EventSizeWindow
      ResizeGadget(0,-1,-1,WindowWidth()-20,-1)
      ResizeGadget(2,-1,-1,WindowWidth()-20,-1)
    Case #PB_Event_CloseWindow
      DeleteObject_(blueBrush)
      DeleteObject_(greenBrush)      
      End
    EndSelect
  ForEver
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

halo wrote:Thank you!

No offense, but you PB programmers write the worst spaghetti code I have ever seen. If I wrote code like that, I would spend more time formatting than thinking:
You're welcome and no offense taken :)

I format my code to suit my eyes. Much of the code I post here is cut and paste from various sources I've accumulated. Sometimes I don't have time to clean it up so I post it as is. I figure posting results is more important than posting pretty code. I leave it up to the user to spend the time cleaning up the code to suit their coding style. ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
halo
Enthusiast
Enthusiast
Posts: 104
Joined: Mon Jan 26, 2004 2:49 am

Post by halo »

I got it working.

Dude, just trust me, Blitz3D+PureBasic is one WEIRD development environment.
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

Re: Setting a container gadget color affects ALL containers

Post by GenRabbit »

I'm trying the code above with a few fixes so it would compile, but I can't get it working.

Code: Select all

Global.l blueBrush=CreateSolidBrush_(RGB(0,0,255))
Global.l greenBrush=CreateSolidBrush_(RGB(0,255,0))

Procedure myWindowCallback(hwnd.i, msg.i, wparam.i, lparam.i)
result=#PB_ProcessPureBasicEvents
Select msg
  Case #WM_ERASEBKGND
    container.RECT
    Select hwnd
      Case GadgetID(0)
        container\right=GadgetWidth(0)
        container\bottom=GadgetHeight(0)
        FillRect_(wparam,container,blueBrush)
        result=1
      Case GadgetID(2)
        container\right=GadgetWidth(2)
        container\bottom=GadgetHeight(2)
        FillRect_(wparam,container,greenBrush)
        result=1
      EndSelect
  EndSelect
ProcedureReturn result
EndProcedure

win=OpenWindow(0,0,0,400,230,"Colored ContainerGadgets", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
ContainerGadget (0,10,10,380,100)
SetClassLong_(GadgetID(0),#GCL_HBRBACKGROUND,0)
ButtonGadget(1,90,40,180,20,"Button in Blue Container")
CloseGadgetList()
ContainerGadget (2,10,120,380,100)
ButtonGadget(3,90,40,180,20,"Button in Green Container")
CloseGadgetList()
SetWindowCallback(@myWindowCallback())

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_SizeWindow
      ResizeGadget(0,-1,-1,WindowWidth(0)-20,-1)
      ResizeGadget(2,-1,-1,WindowWidth(0)-20,-1)
    Case #PB_Event_CloseWindow
      DeleteObject_(blueBrush)
      DeleteObject_(greenBrush)     
      End
    EndSelect
  ForEver
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Setting a container gadget color affects ALL containers

Post by Paul »

Why not use SetGadgetColor() command ??

Code: Select all

  If OpenWindow(0, 0, 0, 322, 150, "ContainerGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ContainerGadget(0, 8, 8, 306, 133, #PB_Container_Raised)
      ButtonGadget(1, 10, 15, 80, 24, "Button 1")
      ButtonGadget(2, 95, 15, 80, 24, "Button 2")
    CloseGadgetList()
    
    SetGadgetColor(0,#PB_Gadget_BackColor,$ff0000)
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Image Image
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

Re: Setting a container gadget color affects ALL containers

Post by GenRabbit »

Because I had forgotten all about these and where sure I needed to use WinAPI. What I was looking for was a way to color/Add a picture as background picture in a window. But your post made me check if there was a setWindowColor, and it was. So that solved my problem so far. (openWindow did not show any link to commands related to it, So I quickly thought There where not any related to what I needed.)
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Setting a container gadget color affects ALL containers

Post by Bisonte »

Use a CanvasGadget with #PB_Canvas_Container Flag.
Simple way to put images, drawings etc on it and by the way .... crossplatform ;)
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.)
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

Re: Setting a container gadget color affects ALL containers

Post by GenRabbit »

Thanks for the tip. That makes it easier.
I don't think I get around using API as I also use Editorgadget. And some of my code there is using sendmessage.
Post Reply