Page 1 of 1

CheckBoxGadget BackColor

Posted: Wed Nov 29, 2006 8:24 am
by martin66119
Hi and good morning!

I am not sure if I am wrong or not. But I Think it is not possible to change
the Gadget_BackColor of a ChrckBoxGadget by using
SetGadgetColor(#Gadget, FarbTyp, Farbe).

Is there any possibility for changeing the "BackColor" by API.

Martin

Posted: Wed Nov 29, 2006 8:39 am
by netmaestro
Do you mean the actual box or the text that appears beside it?

Posted: Wed Nov 29, 2006 8:47 am
by martin66119
I mean the Box! not the text. But if it is possible to do both it is much more better.

Posted: Wed Nov 29, 2006 9:00 am
by netmaestro
The checkbox gadget is actually a button control that has the BS_CHECKBOX style, so SetGadgetColor isn't going to work on it.

For the text it can be done by reacting To the WM_CTLCOLORSTATIC message For the control by SetBkMode_(wParam,#TRANSPARENT), SetTextColor_(wParam,<desired text color>), And returning a SolidBrush your desired background color. For the box I've never tried to do that, so I'm not sure but it can probably be managed somehow. I'll study on it for a bit and let you know if something occurs.

Code: Select all

Global GadgetFrg=#Black, GadgetBkg = CreateSolidBrush_(#Green) 

Procedure WindowProc(hWnd, Msg, wParam, lParam) 
  result = #PB_ProcessPureBasicEvents 
  If Msg=#WM_CTLCOLORSTATIC 
    If GetDlgCtrlID_(lparam) = 1
      SetBkMode_(wParam,#TRANSPARENT) 
      SetTextColor_(wParam,GadgetFrg) 
      result = GadgetBkg 
    EndIf
  EndIf 
  ProcedureReturn result 
EndProcedure 

OpenWindow(0,0,0,260,160,"test",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered) 
SetWindowCallback(@WindowProc()) 
CreateGadgetList(WindowID(0)) 
CheckBoxGadget(1,10,20,245,20,"Checkbox gadget with a green background") 

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow 

DeleteObject_(GadgetBkg) 

Posted: Wed Nov 29, 2006 9:45 am
by martin66119
Thanks a lot for helping!!!!!!

Posted: Wed Nov 29, 2006 10:26 am
by netmaestro
OK, got something. Here's an example of coloring all elements of the checkbox including background, text, box background and checkmark:

Code: Select all

;====================================================
; Program:       CheckBox Colors Demo
; Author:        netmaestro
; Date:          November 29, 2006
;====================================================

Global GadgetBkg  = CreateSolidBrush_(#Green)
Global BoxBkg     = CreateSolidBrush_(#Yellow)
Global Checkcolor = #Blue
Global Textcolor  = #Red
Global NullPen    = CreatePen_(#PS_NULL, 0, 0)

Procedure GadgetProc(hwnd, msg, wparam, lparam)
  Shared oldproc
  result = CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
  Select msg 
    Case  #WM_PAINT, #WM_LBUTTONUP, #WM_LBUTTONDOWN
      dc = GetDC_(hwnd)
      SelectObject_(dc, BoxBkg)
      SelectObject_(dc, NullPen)
      Rectangle_(dc,2,5,12,15)
      If GetGadgetState(GetDlgCtrlID_(hwnd))
        Restore check
         For i = 1 To 21
           Read x
           Read y
           SetPixel_(dc,x+2,y+5, CheckColor)
         Next   
      EndIf
      ReleaseDC_(hwnd, dc);
  EndSelect
  ProcedureReturn result
EndProcedure

Procedure WindowProc(hWnd, Msg, wParam, lParam) 
  result = #PB_ProcessPureBasicEvents 
  If Msg=#WM_CTLCOLORSTATIC 
    If GetDlgCtrlID_(lparam) = 1 
      SetBkMode_(wParam,#TRANSPARENT) 
      SetTextColor_(wParam, Textcolor) 
      result = GadgetBkg 
    EndIf 
  EndIf 
  ProcedureReturn result 
EndProcedure 

OpenWindow(0,0,0,260,160,"test",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered) 
SetWindowCallback(@WindowProc()) 
CheckBoxGadget(1,10,20,245,20,"Checkbox gadget with a green background") 
oldproc = SetWindowLong_(GadgetID(1),#GWL_WNDPROC,@GadgetProc())

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow 

DeleteObject_(GadgetBkg) 
DeleteObject_(BoxBkg) 
DeleteObject_(NullPen) 

DataSection
  check:
  Data.i 7,1,6,2,7,2,1,3,5,3,6,3,7,3,1,4,2,4,4,4,5,4,6,4,1,5,2,5,3,5,4,5,5,5,2,6,3,6,4,6,3,7
EndDataSection

Posted: Wed Nov 29, 2006 10:43 am
by martin66119
Oh that is great: exactly what I am looking for.

Thank you very much

Posted: Wed Nov 29, 2006 10:56 am
by netmaestro
One caveat, if you compile with xp skin support enabled, the foreground text color will be always black, but the background, box background and checkmark colors will all work as selected. If it's a problem I can probably think up a solution.

Re: CheckBoxGadget BackColor

Posted: Wed Jan 20, 2010 11:55 pm
by TerryHough
Neat idea.

But has anyone figured how to change the background color of the check box only when it has the focus (or is the active gadget)?

Re: CheckBoxGadget BackColor

Posted: Sun Jul 21, 2024 3:17 am
by BarryG
Updated Netmaestro's code to not crash with PureBasic v6.10 now. (It needs ".i" for the Data line now, not ".l" anymore; otherwise the app crashes when the checkbox is clicked):

Code: Select all

; For PureBasic v6.10 and later.

Global GadgetBkg  = CreateSolidBrush_(#Green)
Global BoxBkg     = CreateSolidBrush_(#Yellow)
Global Checkcolor = #Blue
Global Textcolor  = #Red
Global NullPen    = CreatePen_(#PS_NULL, 0, 0)

Procedure GadgetProc(hwnd, msg, wparam, lparam)
  Shared oldproc
  result = CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
  Select msg
    Case  #WM_PAINT, #WM_LBUTTONUP, #WM_LBUTTONDOWN
      dc = GetDC_(hwnd)
      SelectObject_(dc, BoxBkg)
      SelectObject_(dc, NullPen)
      Rectangle_(dc,2,5,12,15)
      If GetGadgetState(GetDlgCtrlID_(hwnd))
        Restore check
         For i = 1 To 21
           Read x
           Read y
           SetPixel_(dc,x+2,y+5, CheckColor)
         Next
      EndIf
      ReleaseDC_(hwnd, dc)
  EndSelect
  ProcedureReturn result
EndProcedure

Procedure WindowProc(hWnd, Msg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If Msg=#WM_CTLCOLORSTATIC
    If GetDlgCtrlID_(lparam) = 1
      SetBkMode_(wParam,#TRANSPARENT)
      SetTextColor_(wParam, Textcolor)
      result = GadgetBkg
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

OpenWindow(0,0,0,260,70,"test",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SetWindowColor(0,#Red)
SetWindowCallback(@WindowProc())
CheckBoxGadget(1,10,20,245,20,"Yellow Checkbox gadget")
oldproc = SetWindowLongPtr_(GadgetID(1),#GWL_WNDPROC,@GadgetProc())

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

DeleteObject_(GadgetBkg)
DeleteObject_(BoxBkg)
DeleteObject_(NullPen)

DataSection
  check:
  Data.i 7,1,6,2,7,2,1,3,5,3,6,3,7,3,1,4,2,4,4,4,5,4,6,4,1,5,2,5,3,5,4,5,5,5,2,6,3,6,4,6,3,7
EndDataSection

Re: CheckBoxGadget BackColor

Posted: Sun Jul 21, 2024 3:52 pm
by Fred
Thanks, I edited as well the original post for consistancy.