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

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

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

Post 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. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post by IdeasVacuum »

Excellent TI-994A, very handy indeed 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

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

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

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

Post by TI-994A »

Thanks guys, for your great feedback. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

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

Post 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)
ImageThe happiness is a road...
Not a destination
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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).
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
es_91
Enthusiast
Enthusiast
Posts: 242
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

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

Post 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?
User avatar
charvista
Addict
Addict
Posts: 902
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

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

Post 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
- Windows 11 Home 64-bit
- PureBasic 6.04 LTS (x64)
- 64 Gb RAM
Post Reply