Changing Gadget Color

Just starting out? Need help? Post your questions and find answers here.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Changing Gadget Color

Post by chris319 »

For some reason, the gadget isn't changing color. What am I doing wrong? I am able to set the gadget's font and text.

Code: Select all

  ev = EventGadget()
           SetGadgetText(ev,"hi")
           SetGadgetColor(ev, #PB_Gadget_FrontColor,#White)
           SetGadgetColor(ev, #PB_Gadget_BackColor,RGB(0,255,0))
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Changing Gadget Color

Post by Caronte3D »

May be a refresh thing, try to put the SetGadgetText after SetGadgetColor
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Changing Gadget Color

Post by BarryG »

What type of gadget? Not all support coloring.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Changing Gadget Color

Post by chris319 »

Caronte3D wrote:May be a refresh thing, try to put the SetGadgetText after SetGadgetColor
No joy.

It's a regular button gadget.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Changing Gadget Color

Post by mk-soft »

ButtonGadget not support colors!

Search forum to ButtonColorGadget....

:wink:
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
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Changing Gadget Color

Post by netmaestro »

A common and very simple way (no libs or includes) is to use a ButtonImage gadget instead and make your own colored images to use on it. That's good enough for most uses.
BERESHEIT
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Changing Gadget Color

Post by chris319 »

Yeah, got to use a ButtonImageGadget.

Unfortunately I lose text centering, word wrap and must initialize a bunch of images for what I'm doing.

Bummer.

The other thing I could do is change the buttonimagegadget to a string gadget. What is the best way to remove a gadget and use a different one?

OK, I see FreeGadget.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Changing Gadget Color

Post by RASHAD »

Hi chris319
If you are a Windows user
You can keep Center Hal & center Val(single line only) beside Word wrap

Code: Select all

Global BBrush,TBrush,pen

BBrush = CreateSolidBrush_($FF3028)
TBrush = CreateSolidBrush_($D9FEFE)
;pen = CreatePen_(#PS_SOLID, 4, $23CEFE)

Procedure WCB(hWnd,uMsg,wParam,lParam)
    Select uMsg
        Case #WM_DRAWITEM
          *cb.DRAWITEMSTRUCT = lParam 
          id = *cb\CtlID
          Select id
            Case 1
              SetBkMode_(*cb\hDC,#TRANSPARENT)   
              If *cb\itemState & #ODS_SELECTED
                SelectObject_(*cb\hDC,BBrush)         
                SetTextColor_(*cb\hDC,$D9FEFE)
              Else
                SelectObject_(*cb\hDC,TBrush)         
                SetTextColor_(*cb\hDC,$FF3028)     
              EndIf 
              SelectObject_(*cb\hDC, pen)        
              Rectangle_(*cb\hDC,*cb\rcItem\left,*cb\rcItem\top,100,40)
              Text$ = GetGadgetText(id)
              DrawText_(*cb\hDC,@Text$,Len(Text$),*cb\rcItem,#DT_CENTER | #DT_VCENTER | #DT_WORDBREAK) ;#DT_SINGLELINE	for single line
          EndSelect       
       ProcedureReturn 0
    EndSelect
     
    ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0,0,0,400,200,"TEST",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(1,10,150,100,40," BUTTON   #1", #BS_OWNERDRAW)

SetClassLongPtr_(GadgetID(1),#GCL_STYLE, GetClassLongPtr_(GadgetID(1),#GCL_STYLE) | #CS_DBLCLKS)

LoadFont(1,"Georgia",12,#PB_Font_HighQuality|#PB_Font_Bold )
SetGadgetFont(1,FontID(1))

SetWindowCallback(@WCB())

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case  1  : Debug "Button 0 was pressed"
        EndSelect
    EndSelect
  Until Quit = 1
Or use ButtonImageGadget() with respect to Center Hal & Val
Simple as I can

Code: Select all

Procedure MyButtonGadget(Gadget, X, Y, Width, Height, Font ,Text.s, fColor,bColor)
  tex = TextGadget(#PB_Any,x,y,Width,Height,Text,#SS_CENTER| #SS_CENTERIMAGE)
  SetGadgetFont(tex,FontID(font))
  SetGadgetColor(tex,#PB_Gadget_BackColor,bColor)
  SetGadgetColor(tex,#PB_Gadget_FrontColor,fColor)
  img = Gadget+100
  Width = GadgetWidth(tex)
  Height = GadgetHeight(tex)
  CreateImage(img, Width, Height, 24)
  If img
    hdc = StartDrawing(ImageOutput(img))
      SendMessage_(GadgetID(tex),#WM_PRINT,hDC,#PRF_CHILDREN| #PRF_CLIENT|#PRF_NONCLIENT| #PRF_ERASEBKGND)
    StopDrawing()
    FreeGadget(tex)
    ButtonImageGadget(Gadget, X, Y, Width, Height, ImageID(img))
   EndIf

EndProcedure

LoadFont(0,"Tahoma",12)
OpenWindow (0, #PB_Ignore, #PB_Ignore, 200, 200, "Colored ButtonGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

MyButtonGadget(1, 10, 10, 80, 30, 0," Test Text ", $0000FF,$D7FEFB)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
#3 :

Code: Select all

Procedure MyButtonGadget(Gadget, X, Y, Width, Height, Font ,Text.s, fColor,bColor)
  tex = TextGadget(#PB_Any,0,0,0,0,Text, #PB_Text_Center)
  SetGadgetFont(tex,FontID(font))
  Width = GadgetWidth(tex,#PB_Gadget_RequiredSize)
  Height = GadgetHeight(tex,#PB_Gadget_RequiredSize)
  SetGadgetColor(tex,#PB_Gadget_BackColor,bColor)
  SetGadgetColor(tex,#PB_Gadget_FrontColor,fColor)
  img = Gadget+100
  ResizeGadget(tex,0,0,width,height)
  CreateImage(img, Width, Height, 24)
  If img
    hdc = StartDrawing(ImageOutput(img))
      SendMessage_(GadgetID(tex),#WM_PRINT,hDC,#PRF_CHILDREN| #PRF_CLIENT|#PRF_NONCLIENT| #PRF_ERASEBKGND)
    StopDrawing()
    FreeGadget(tex)
    ButtonImageGadget(Gadget, X, Y, Width, Height, ImageID(img))
   EndIf

EndProcedure

LoadFont(0,"Tahoma",12)
OpenWindow (0, #PB_Ignore, #PB_Ignore, 200, 200, "Colored ButtonGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

MyButtonGadget(1, 10, 10, 80, 30, 0," TextGadget Center "+ #CRLF$ +" Second Line ", $0000FF,$D7FEFB)

MyButtonGadget(2, 10, 60, 80, 30, 0," Test Text " + #CRLF$ + " #2 ", $FF0000,$D7FEFB)

MyButtonGadget(3, 10, 110, 80, 30, 0," Test Text "+ #CRLF$ + " #3 ", $00FF00,$D7FEFB)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Egypt my love
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: Changing Gadget Color

Post by AZJIO »

The DRAWITEMSTRUCT structure has an itemData.i field. Is it possible to somehow insert a pointer to the data structure about the button read from the ini-file into this field? That is, how to assign the data of this field in the event loop, because the structure is created in WinCallback() from lParam.
I want to make my own color for each button, but I need to check in WinCallback() that the button has a color, create a brush and fill it. And when you exit, remove the brushes. I need access to the DRAWITEMSTRUCT structure in the event loop.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Changing Gadget Color

Post by mk-soft »

You can use Set/GetGadgetData for own data
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
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Changing Gadget Color

Post by firace »

RASHAD wrote: Sat Mar 28, 2020 9:16 pm ...
Or use ButtonImageGadget() with respect to Center Hal & Val
Simple as I can

Code: Select all

Procedure MyButtonGadget(Gadget, X, Y, Width, Height, Font ,Text.s, fColor,bColor)
  tex = TextGadget(#PB_Any,x,y,Width,Height,Text,#SS_CENTER| #SS_CENTERIMAGE)
  SetGadgetFont(tex,FontID(font))
  SetGadgetColor(tex,#PB_Gadget_BackColor,bColor)
  SetGadgetColor(tex,#PB_Gadget_FrontColor,fColor)
  img = Gadget+100
  Width = GadgetWidth(tex)
  Height = GadgetHeight(tex)
  CreateImage(img, Width, Height, 24)
  If img
    hdc = StartDrawing(ImageOutput(img))
      SendMessage_(GadgetID(tex),#WM_PRINT,hDC,#PRF_CHILDREN| #PRF_CLIENT|#PRF_NONCLIENT| #PRF_ERASEBKGND)
    StopDrawing()
    FreeGadget(tex)
    ButtonImageGadget(Gadget, X, Y, Width, Height, ImageID(img))
   EndIf

EndProcedure

LoadFont(0,"Tahoma",12)
OpenWindow (0, #PB_Ignore, #PB_Ignore, 200, 200, "Colored ButtonGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

MyButtonGadget(1, 10, 10, 80, 30, 0," Test Text ", $0000FF,$D7FEFB)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
#3 :

Code: Select all

Procedure MyButtonGadget(Gadget, X, Y, Width, Height, Font ,Text.s, fColor,bColor)
  tex = TextGadget(#PB_Any,0,0,0,0,Text, #PB_Text_Center)
  SetGadgetFont(tex,FontID(font))
  Width = GadgetWidth(tex,#PB_Gadget_RequiredSize)
  Height = GadgetHeight(tex,#PB_Gadget_RequiredSize)
  SetGadgetColor(tex,#PB_Gadget_BackColor,bColor)
  SetGadgetColor(tex,#PB_Gadget_FrontColor,fColor)
  img = Gadget+100
  ResizeGadget(tex,0,0,width,height)
  CreateImage(img, Width, Height, 24)
  If img
    hdc = StartDrawing(ImageOutput(img))
      SendMessage_(GadgetID(tex),#WM_PRINT,hDC,#PRF_CHILDREN| #PRF_CLIENT|#PRF_NONCLIENT| #PRF_ERASEBKGND)
    StopDrawing()
    FreeGadget(tex)
    ButtonImageGadget(Gadget, X, Y, Width, Height, ImageID(img))
   EndIf

EndProcedure

LoadFont(0,"Tahoma",12)
OpenWindow (0, #PB_Ignore, #PB_Ignore, 200, 200, "Colored ButtonGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

MyButtonGadget(1, 10, 10, 80, 30, 0," TextGadget Center "+ #CRLF$ +" Second Line ", $0000FF,$D7FEFB)

MyButtonGadget(2, 10, 60, 80, 30, 0," Test Text " + #CRLF$ + " #2 ", $FF0000,$D7FEFB)

MyButtonGadget(3, 10, 110, 80, 30, 0," Test Text "+ #CRLF$ + " #3 ", $00FF00,$D7FEFB)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Thanks RASHAD for this great example. I especially like the fact that no callback is needed.
I'm trying to adapt it to have rounded corners, but no luck so far. Any ideas?

Code: Select all

Procedure MyButtonGadget(Gadget, X, Y, Width, Height, Font ,Text.s, fColor,bColor)
  
;   ContainerGadget(Gadget+200,x,y,Width,Height)
  tex = TextGadget(#PB_Any,0,0,Width,Height,Text,#SS_CENTER| #SS_CENTERIMAGE)
  
;   hrgn = CreateRoundRectRgn_(0,0,Width,Height,15,15)
;   SetWindowRgn_(GadgetID(Gadget+200),hrgn,1)
;   DeleteObject_(hrgn)
;   CloseGadgetList()
  
  SetGadgetFont(tex,FontID(font))
  SetGadgetColor(tex,#PB_Gadget_BackColor,bColor)
  SetGadgetColor(tex,#PB_Gadget_FrontColor,fColor)
  
  img = Gadget+100
  Width = GadgetWidth(tex)
  Height = GadgetHeight(tex)
  
  CreateImage(img, Width, Height, 24)
  If img
    hdc = StartDrawing(ImageOutput(img))
    SendMessage_(GadgetID(tex),#WM_PRINT,hDC,#PRF_CHILDREN| #PRF_CLIENT|#PRF_NONCLIENT| #PRF_ERASEBKGND)
    StopDrawing()
    FreeGadget(tex)
    ButtonImageGadget(Gadget, X, Y, Width, Height, ImageID(img))
    SetWindowTheme_(GadgetID(Gadget),"","SCROLLBAR")
    
  EndIf
  
EndProcedure


LoadFont(0,"Arial",14)
OpenWindow (0, #PB_Ignore, #PB_Ignore, 400, 200, "Colored ButtonGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

MyButtonGadget(3, 250, 50, 100, 42, 0," Stop " , $F3F3F3, $6570A5)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Changing Gadget Color

Post by RASHAD »

Hi
Circular Button as example

Code: Select all


OpenWindow(0, 0, 0, 200, 200, "ButtonImage",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;LoadImage(0, "g:\mmedia\pictures\girl24.bmp")
LoadImage(0, #PB_Compiler_Home + "examples/Sources/Data/PureBasicLogo.bmp")  
LoadFont(0, "Times New Roman", 16,#PB_Font_Bold )
ResizeImage(0, 150, 150)
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(FontID(0))
FrontColor(0)
DrawText(15, 60, "Click Me",$0000FF)
DrawingMode(#PB_2DDrawing_Outlined)
StopDrawing()
CopyImage(0,1)
StartDrawing(ImageOutput(1))
;Box(0,0,150,150,#Blue);GetSysColor_(#COLOR_BTNFACE))
Circle(72,72,35,#White)
DrawAlphaImage(ImageID(0),0,0,200)
StopDrawing()
CanvasGadget(1, 10,10, 150, 150,#PB_Canvas_Border)
SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(0))

Egn = CreateEllipticRgn_(12,12, 137,137)
SetWindowRgn_(GadgetID(1),Egn,1)

Repeat
  Select  WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1      
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Select EventType()
            Case #PB_EventType_MouseEnter
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(1))
              
            Case #PB_EventType_MouseLeave
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(0))
              
            Case #PB_EventType_LeftButtonDown
              ResizeGadget(1,12,12,137,137)
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(1))
            
            Case #PB_EventType_LeftButtonUp
              ResizeGadget(1,10,10,137,137)
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(1))
          EndSelect                         
      EndSelect
  EndSelect
Until Quit = 1

Egypt my love
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Changing Gadget Color

Post by firace »

RASHAD wrote: Tue Jul 05, 2022 1:28 pm Hi
Circular Button as example

Code: Select all


OpenWindow(0, 0, 0, 200, 200, "ButtonImage",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;LoadImage(0, "g:\mmedia\pictures\girl24.bmp")
LoadImage(0, #PB_Compiler_Home + "examples/Sources/Data/PureBasicLogo.bmp")  
LoadFont(0, "Times New Roman", 16,#PB_Font_Bold )
ResizeImage(0, 150, 150)
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(FontID(0))
FrontColor(0)
DrawText(15, 60, "Click Me",$0000FF)
DrawingMode(#PB_2DDrawing_Outlined)
StopDrawing()
CopyImage(0,1)
StartDrawing(ImageOutput(1))
;Box(0,0,150,150,#Blue);GetSysColor_(#COLOR_BTNFACE))
Circle(72,72,35,#White)
DrawAlphaImage(ImageID(0),0,0,200)
StopDrawing()
CanvasGadget(1, 10,10, 150, 150,#PB_Canvas_Border)
SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(0))

Egn = CreateEllipticRgn_(12,12, 137,137)
SetWindowRgn_(GadgetID(1),Egn,1)

Repeat
  Select  WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1      
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Select EventType()
            Case #PB_EventType_MouseEnter
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(1))
              
            Case #PB_EventType_MouseLeave
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(0))
              
            Case #PB_EventType_LeftButtonDown
              ResizeGadget(1,12,12,137,137)
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(1))
            
            Case #PB_EventType_LeftButtonUp
              ResizeGadget(1,10,10,137,137)
              SetGadgetAttribute(1, #PB_Canvas_Image , ImageID(1))
          EndSelect                         
      EndSelect
  EndSelect
Until Quit = 1

Great example; thanks a lot!
Post Reply