Change background colour of button gadget for Windows

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 947
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Change background colour of button gadget for Windows

Post by marcoagpinto »

Hello!

Could someone tell me how to change the background colour of a button gadget?

The documentation doesn't have this kind of gadgets as supported with the SetGadgetColor command.

Thank you!

Kind regards,
>Marco A.G.Pinto
---------------
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Change background colour of button gadget for Windows

Post by Bisonte »

Only with API.

If you want colored ButtonGadgets on all OS, you have to create your own with a CanvasGadget.
It's easier than you think, and the buttons have the same look on every OS ;)
PureBasic 6.10 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.)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Change background colour of button gadget for Windows

Post by Dude »

marcoagpinto wrote:Could someone tell me how to change the background colour of a button gadget?
Here's some bug-fixed code from another thread in this forum. The only downside is that you can't disable the buttons with it, as they look ugly if you do. :?

You can remove the #PB_Default parts if you want a non-API cross-platform version. ;)

Code: Select all

Procedure ButtonGadgetColor(gad,x,y,w,h,text$="",flags=0,fgcolor=#PB_Default,bgcolor=#PB_Default,font=#PB_Default)
  If bgcolor=#PB_Default : bgcolor=GetSysColor_(#COLOR_BTNFACE) : EndIf
  If fgcolor=#PB_Default : fgcolor=GetSysColor_(#COLOR_BTNFACE) : EndIf
  If font=#PB_Default : font=GetStockObject_(#DEFAULT_GUI_FONT) : EndIf
  text=CreateImage(#PB_Any,w,h)
  If StartDrawing(ImageOutput(text))
    DrawingFont(font) : Box(0,0,w,h,bgcolor)
    DrawText(w/2-TextWidth(text$)/2,h/2-TextHeight(text$)/2,text$,fgcolor,bgcolor)
    StopDrawing() : ok=ButtonImageGadget(gad,x,y,w,h,ImageID(text),flags)
  EndIf
  ProcedureReturn ok
EndProcedure

OpenWindow(0,100,100,340,100,"Colored Buttons",#PB_Window_SystemMenu)

ButtonGadgetColor(1,10,10,150,30,"toggle",#PB_Button_Toggle,#White,#Red)
ButtonGadgetColor(2,10,50,150,30,"courier",0,#Blue,#Yellow,LoadFont(0,"courier",12))
ButtonGadget(#PB_Any,170,10,150,30,"normal")
ButtonGadgetColor(#PB_Any,170,50,150,30,"normal + red",0,#Red)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Change background colour of button gadget for Windows

Post by mk-soft »

Since XP-Theme not longer over API available for change button colors

There are many examples for button over canvas gadget.

Link https://www.purebasic.fr/german/viewtop ... =8&t=28903
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
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Change background colour of button gadget for Windows

Post by blueb »

Here's something basic by said. (TI-994A)

Code: Select all

;==============================================================
;   ButtonGadgetEx() enables foreground & background colours
;   and text formatting including italics, bold & underline
;   
;   tested & working on WinXP x86, Win7 x64, OSX x64
;   running PureBasic v5.21, v5.22, v5.20 respectively
;
;   by TI-994A - free to use, improve, share...
;
;   20th April 2014
;==============================================================

;ButtonGadgetEx() wrapped into a single procedure
Procedure ButtonGadgetEx(gadgetNo, buttonXPos, buttonYPos, buttonWidth, buttonHeight, caption.s, foreColor = 0, backColor = 16777215)
  regular = LoadFont(#PB_Any, "Arial", 10)
  bold = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Bold) 
  italics = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Italic)
  underline = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Underline)
  italicBold = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Italic | #PB_Font_Bold)
  boldUnderline = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Bold | #PB_Font_Underline)
  italicUnderline = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Italic | #PB_Font_Underline)
  fullFormat = LoadFont(#PB_Any, "Arial", 10, #PB_Font_Italic | #PB_Font_Bold | #PB_Font_Underline)
  buttonText = CreateImage(#PB_Any, buttonWidth, buttonHeight)
  If StartDrawing(ImageOutput(buttonText))
    Box(0, 0, buttonWidth, buttonHeight, backColor)
    For pass = 1 To 2
      For captionLoop = 1 To Len(caption)
        Select Mid(caption, captionLoop, 3)
          Case "<i>"
            iFlag = 1
            captionLoop + 2
          Case "<b>"
            bFlag = 1
            captionLoop + 2
          Case "<u>"
            uFlag = 1
            captionLoop + 2
          Case "</i"
            iFlag = 0
            captionLoop + 3
          Case "</b"
            bFlag = 0
            captionLoop + 3
          Case "</u"
            uFlag = 0
            captionLoop + 3
          Default
            noFormat = 1
        EndSelect
        If noFormat
          noFormat = 0
          If Not drawFont
            drawFont = FontID(regular)
            DrawingFont(drawFont)
          EndIf
          If pass = 1
            captionWidth + TextWidth(Mid(caption, captionLoop, 1))
          Else
            captionX = DrawText(captionX, captionY, Mid(caption, captionLoop, 1), foreColor, backColor)
          EndIf
        Else
          If iFlag And bFlag And uFlag
            drawFont = FontID(fullFormat)
          ElseIf iFlag And bFlag
            drawFont = FontID(italicBold)
          ElseIf iFlag And uFlag
            drawFont = FontID(italicUnderline)
          ElseIf bFlag And uFlag
            drawFont = FontID(boldUnderline)
          ElseIf iFlag
            drawFont = FontID(italics)
          ElseIf bFlag
            drawFont = FontID(bold)
          ElseIf uFlag
            drawFont = FontID(underline)
          Else
            drawFont = FontID(regular)
          EndIf
          DrawingFont(drawFont)
        EndIf
      Next captionLoop
      captionX = (buttonWidth - captionWidth) / 2
      captionY = (buttonHeight - TextHeight(caption)) / 2
      drawFont = 0 : iFlag = 0 : bFlag = 0 : uFlag = 0
    Next pass
    StopDrawing()
  EndIf
  FreeFont(regular)
  FreeFont(italics)
  FreeFont(bold)
  FreeFont(underline)
  FreeFont(italicBold)
  FreeFont(italicUnderline)
  FreeFont(boldUnderline)
  FreeFont(fullFormat)
  ProcedureReturn ButtonImageGadget(gadgetNo, buttonXPos, buttonYPos, buttonWidth, buttonHeight, ImageID(buttonText))
EndProcedure

;demo code
Enumeration
  #MainWindow
  #textButton1
  #textButton2
EndEnumeration

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 500, 220, "ButtonGadgetEx() by TI-994A", wFlags)

;instantiated with constant - colour parameters not set
ButtonGadgetEx(#textButton1, 175, 30, 150, 30, "<b>Default</b> <i>format</i>")

;instantiated with constant - foreground and background colours set
formattedCaption.s = "<i>Italics</i>, <b>Bold</b> or <u>Underlined</u>"
ButtonGadgetEx(#textButton2, 125, 75, 250, 30, formattedCaption, RGB(0, 0, 100), RGB(220, 220, 250))

;instantiated with #PB_Any - foreground and background colours set
formattedCaption = "<i><b>Italics & Bold</i></b>   or   <i><u>Italics & Underlined</i></u>"
textButton3 = ButtonGadgetEx(#PB_Any, 75, 120, 350, 30, formattedCaption, RGB(100, 0, 0), RGB(250, 220, 220))

;instantiated with #PB_Any - foreground and background colours set
formattedCaption = "<b><u>Bold & Underlined</b></u>   or   <i><b><u>Italics, Bold & Underlined</i></b></u>"
textButton4 = ButtonGadgetEx(#PB_Any, 25, 165, 450, 30, formattedCaption, RGB(0, 100, 0), RGB(200, 250, 220))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #textButton1
          Debug "button 1 pressed..."
        Case #textButton2
          Debug "button 2 pressed..."
        Case textButton3
          Debug "button 3 pressed..."
        Case textButton4
          Debug "button 4 pressed..."
      EndSelect
  EndSelect
Until appQuit = 1
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
marcoagpinto
Addict
Addict
Posts: 947
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Change background colour of button gadget for Windows

Post by marcoagpinto »

Thank you my friends.

I still haven't tested as I had the weekend job and was out most of the day.

I will test it today.
Post Reply