Page 1 of 1

Function to set colours of OptionGadget() & CheckBoxGadget()

Posted: Sun Nov 30, 2014 4:29 pm
by TI-994A
A small, re-usable, Windows-only function to set the colours of the OptionGadget() and CheckBoxGadget():

Code: Select all

;========================================================
;  CheckOptionColor() sets the foreground & background 
;  colours of the OptionGadget() and CheckBoxGadget()
;
;  requires a window-level callback - for WINDOWS ONLY
;
;  tested with PureBasic v5.31 on Windows 8.1 Pro (x64)
;
;  by TI-994A - free to use, improve, share...
;
;  30th November 2014
;========================================================

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_CTLCOLORSTATIC 
    gadgetNo = GetProp_(lParam, "PB_ID")
    fgColor = GetProp_(GadgetID(gadgetNo), "fgc")
    bgColor = GetProp_(GadgetID(gadgetNo), "bgc")
    If fgColor Or bgColor
      SetTextColor_(wParam, fgColor)
      SetBkMode_(wParam, #TRANSPARENT)
      ProcedureReturn bgColor
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

Procedure CheckOptionColor(gadgetNo, fgColor = #Black, bgColor = #White)
  SetProp_(GadgetID(gadgetNo), "fgc", fgColor)
  SetProp_(GadgetID(gadgetNo), "bgc", CreateSolidBrush_(bgColor))
 
  OpenLibrary(0, "uxtheme.dll")
  CallFunction(0, "SetWindowTheme", GadgetID(gadgetNo), @none, @none)
  CloseLibrary(0)  
EndProcedure

;demo code
Enumeration
  #MainWindow
  #option1
  #option2
  #option3
  #check1
  #check2
  #check3
EndEnumeration

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered 
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 300, 150, "Options & Checks", wFlags)

SetWindowColor(#MainWindow, #Gray)
SetWindowCallback(@WndProc())

OptionGadget(#option1, 20, 15, 120, 30, "Red/Yellow") 
CheckOptionColor(#option1, #Red, #Yellow)

OptionGadget(#option2, 160, 15, 120, 30, "Default/Cyan")
CheckOptionColor(#option2, #Null, #Cyan) ;default fore colour

CheckBoxGadget(#check1, 20, 55, 120, 30, "Blue/Default")
CheckOptionColor(#check1, #Blue) ;default back colour

CheckBoxGadget(#check2, 160, 55, 120, 30, "White/Red")
CheckOptionColor(#check2, #White, #Red)

OptionGadget(#option3, 160, 100, 120, 30, "Standard Option") 
CheckBoxGadget(#check3, 20, 100, 120, 30, "Standard Check")

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Suggestions, feedback, and improvements are most welcome. Thank you. :)

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Sun Nov 30, 2014 5:54 pm
by IdeasVacuum
Excellent TI-994A, very handy indeed 8)

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Mon Dec 01, 2014 1:57 am
by ozzie
Darn! And I've spent all that time building own-drawn option and checkbox gadgets using canvas gadgets! Thanks, TI-994A. When I get time I'll change my program to use your code.

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Tue Dec 02, 2014 6:39 am
by TI-994A
Thanks guys, for your great feedback. :)

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Wed Dec 03, 2014 10:59 am
by Kwai chang caine
Very useful !
The question who come immediately is (Like the ButtonGadget) : "Why that not exist natively ?" :cry:
Thanks for the sharing 8)

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Thu Dec 04, 2014 4:12 am
by IdeasVacuum
Depending on the choice of colours, Option Gadgets generally look too ragged and the pip colour is set to black (Tested WinXP), so the DIY Canvas Option Gadget is still the winner (cross platform too of course).

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Sun Dec 07, 2014 10:56 am
by es_91
Thank you a lot.

Wow, this also works for FrameGadgets! Geeenious. :)

/e: I have an application that shall allow the user to switch between an user-defined color style and the system's standard color scheme. However, how to find out what color the Gadget had before you changed it?

I would like to use GetSysColor_ (#COLOR_WINDOW) but it always returns #White! :x My windows are light grey!

So I rather use #COLOR_MENU but that is a very bold approach. I believe in Vista/7/8/10 style the menu has a different color from the window.

Ideas?

Re: Function to set colours of OptionGadget() & CheckBoxGadg

Posted: Sat Jan 06, 2018 10:46 pm
by charvista
Hi TI-994A

Thank you for this fantastic code for OptionGadget() and CheckBoxGadget().
Is there also a way to colorize the PanelGadget(), especially the zone around, which should be dark gray instead of light gray ?
I tried your code, but seems not to work, and looks a bit... ugly :mrgreen:

Code: Select all

;========================================================
;  CheckOptionColor() sets the foreground & background
;  colours of the OptionGadget() and CheckBoxGadget()
;
;  requires a window-level callback - for WINDOWS ONLY
;
;  tested with PureBasic v5.31 on Windows 8.1 Pro (x64)
;
;  by TI-994A - free to use, improve, share...
;
;  30th November 2014
;========================================================

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_CTLCOLORSTATIC
    gadgetNo = GetProp_(lParam, "PB_ID")
    fgColor = GetProp_(GadgetID(gadgetNo), "fgc")
    bgColor = GetProp_(GadgetID(gadgetNo), "bgc")
    If fgColor Or bgColor
      SetTextColor_(wParam, fgColor)
      SetBkMode_(wParam, #TRANSPARENT)
      ProcedureReturn bgColor
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

Procedure CheckOptionColor(gadgetNo, fgColor = #Black, bgColor = #White)
  SetProp_(GadgetID(gadgetNo), "fgc", fgColor)
  SetProp_(GadgetID(gadgetNo), "bgc", CreateSolidBrush_(bgColor))
 
  OpenLibrary(0, "uxtheme.dll")
  CallFunction(0, "SetWindowTheme", GadgetID(gadgetNo), @none, @none)
  CloseLibrary(0) 
EndProcedure

;demo code
Enumeration
  #MainWindow
  #option1
  #option2
  #option3
  #check1
  #check2
  #check3
  #panel1
  #panel2
  #but1
  #but2
EndEnumeration

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 350, 400, "Options & Checks", wFlags)

SetWindowColor(#MainWindow, #Gray)
SetWindowCallback(@WndProc())

OptionGadget(#option1, 20, 15, 120, 30, "Red/Yellow")
CheckOptionColor(#option1, #Red, #Yellow)

OptionGadget(#option2, 160, 15, 120, 30, "Default/Cyan")
CheckOptionColor(#option2, #Null, #Cyan) ;default fore colour

CheckBoxGadget(#check1, 20, 55, 120, 30, "Blue/Default")
CheckOptionColor(#check1, #Blue) ;default back colour

CheckBoxGadget(#check2, 160, 55, 120, 30, "White/Red")
CheckOptionColor(#check2, #White, #Red)

OptionGadget(#option3, 160, 95, 120, 30, "Standard Option")
CheckBoxGadget(#check3, 20, 95, 120, 30, "Standard Check")


    PanelGadget     (#panel1, 8, 148, 306, 203)
      AddGadgetItem (#panel1, -1, "Panel 1")
        PanelGadget (#panel2, 5, 5, 290, 166)
          AddGadgetItem(#panel2, -1, "Sub-Panel 1")
          AddGadgetItem(#panel2, -1, "Sub-Panel 2")
          AddGadgetItem(#panel2, -1, "Sub-Panel 3")
        CloseGadgetList()
      AddGadgetItem (#panel1, -1,"Panel 2")
        ButtonGadget(#but1, 10, 15, 80, 24,"Button 1")
        ButtonGadget(#but2, 95, 15, 80, 24,"Button 2")
    CloseGadgetList()
    CheckOptionColor(#panel1, #Blue, #Red)
    CheckOptionColor(#panel2, #Green, #Red)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend