Page 1 of 1

Colouring the static-field in an un-editable ComboBox

Posted: Sun Oct 30, 2005 8:57 pm
by srod
I've seen this request before, but as no solution was forthcoming and I'm now up against the problem myself, I though I'd ask anew.

The problem is that I need to colour the 'static' field at the top of an un-editable ComboBox control. I've tried intercepting #WM_CTLCOLORSTATIC events, but no joy.

The following code shows my progress so far in that I can colour the listbox easily enough:

Code: Select all

global OldCombProc.l, BackBrush.l
BackBrush = CreateSolidBrush_(#blue)

Procedure.l ComboCallBack( hWnd.l, uMsg.l, wParam.l, lParam.l ) 
  protected result
  if uMsg = #WM_CTLCOLORLISTBOX
      SetBkMode_(wParam,#TRANSPARENT)
      SetTextColor_(wParam,#red)
      SetBkColor_(wParam,#blue)
      result = BackBrush
  Else
    result = CallWindowProc_(OldCombProc, hWnd, uMsg, wParam, lParam) 
  
  EndIf
  
  procedurereturn result
endprocedure


If OpenWindow(0,0,0,270,440,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"ComboBoxGadget") And CreateGadgetList(WindowID(0))
    ComboBoxGadget(1,10,40,250,50)


    For a=0 To 15 : AddGadgetItem(1,-1,"ComboBox item "+Str(a)) : Next
    SetGadgetState(1,0)   

    OldCombProc=SetWindowLong_(GadgetID(1), #GWL_WNDPROC, @ComboCallBack()) 


    Repeat
    EventID = WaitWindowEvent()
        
    Select EventID
     
      Case #PB_Event_Gadget
        select EventGadgetID() 
      EndSelect
    EndSelect
  Until EventID = #PB_Event_CloseWindow
  EndIf

Anyone have a solution?

Thanks.

Posted: Sun Oct 30, 2005 9:31 pm
by srod
Right, I have a work around.

I use an editable combobox in which I subclass the edit-field (after a bit of fiddling) and then trap all messages involving user input (thereby turning it into a read-only edit field, but without the sunken shaded effect). I can then colour the edit-field to match the rest of the combobox.

However, this leaves the flashing caret in the edit-field, which looks a little 'odd'!

Not a brilliant solution, so I am still open to any suggestions!

Before anyone asks, however, there are good reasons why I need to avoid an owner-drawn control in this case. This would, in all likelihood, get around the problem.

Thanks.

Posted: Sun Oct 30, 2005 9:45 pm
by Sparkie
Hi srod :) I fudged with your code a little and I'm not sure if it's what you're looking for, but try this.

Code: Select all

Global BackBrush.l 
BackBrush = CreateSolidBrush_(#blue) 

Procedure.l MainCallback( hwnd.l, uMsg.l, wparam.l, lparam.l ) 
  Protected result 
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_CTLCOLOREDIT And lparam = GadgetID(1)
    SetBkMode_(wparam,#TRANSPARENT) 
    SetTextColor_(wparam,#red) 
    SetBkColor_(wparam,#blue) 
    result = BackBrush 
  EndIf 
  ProcedureReturn result 
EndProcedure 

If OpenWindow(0,0,0,270,440,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"ComboBoxGadget") And CreateGadgetList(WindowID(0)) 
  SetWindowCallback(@MainCallback())
  ComboBoxGadget(1,10,40,250,150) 
   
  For a=0 To 15 : AddGadgetItem(1,-1,"ComboBox item "+Str(a)) : Next 
  
  SetGadgetState(1,0)    
  
  Repeat 
    EventID = WaitWindowEvent() 
    
    Select EventID 
      
      Case #PB_Event_Gadget 
        Select EventGadgetID() 
        EndSelect 
    EndSelect 
  Until EventID = #PB_Event_CloseWindow 
  DeleteObject_(BackBrush)
EndIf 

Posted: Sun Oct 30, 2005 11:01 pm
by srod
Sparkie you flaming genius - I didn't think of trying the parent of the combobox! That is actually better than I was originally aiming for, the way that only the static control is coloured and not the list box. Brilliant.

Your knack of finding solutions is amazing. :lol:

Thanks again.

Posted: Mon Oct 31, 2005 4:57 am
by Sparkie
You're very welcome srod, and thank you for the kind words. :)
Fred and the rest of the PureBasic team make finding the solutions that much easier for us all! 8)

Posted: Mon Nov 07, 2005 11:49 am
by PB
Anyone know a way of coloring the drop-down list of the ComboBox? :)

Posted: Mon Nov 07, 2005 12:13 pm
by srod
The following will colour the edit field and drop down list of an editable combo. If you switch to an uneditable combo, then the following still works on the drop down part, but not the static field. For that you will need Sparkie's code above.

Code: Select all

Global oldcombproc, BackBrush
BackBrush = CreateSolidBrush_(#blue) 

Procedure ComboCallBack( hWnd.l, Message.l, wParam.l, lParam.l ) 

  
  If (Message = #WM_CTLCOLOREDIT) Or (Message = #WM_CTLCOLORLISTBOX) 
    
      SetBkMode_(wParam,#TRANSPARENT)
      SetTextColor_(wParam,#red)
      SetBkColor_(wParam,#blue)
      Result = BackBrush
  Else
    Result.l = CallWindowProc_(oldcombproc, hWnd, Message, wParam, lParam ) 
  EndIf 
  
  ProcedureReturn Result 
  
EndProcedure 


OpenWindow( 0, 200,400,200,100, #PB_Window_SystemMenu, "Colorisation et Callback" ) 

CreateGadgetList( WindowID() ) 
ComboBoxGadget( 1, 10, 10, 180, 120, #PB_ComboBox_Editable ) 

  AddGadgetItem(1, -1, "123" ) 
  AddGadgetItem(1, -1, "456" ) 
  AddGadgetItem(1, -1, "789" ) 
  setgadgetstate(1,0)
  
oldcombproc = SetWindowLong_(GadgetID(1), #GWL_WNDPROC, @ComboCallBack())

Repeat 
Until WaitWindowEvent() = #PB_EventCloseWindow 
End 
Hope it helps.

Posted: Mon Nov 07, 2005 12:53 pm
by PB
Thanks, now I can't go to bed. ;)

Posted: Sat Jan 27, 2007 12:50 pm
by PB
Request for Fred/Freak: Can coloring be added for ComboBoxGadgets, please?

Posted: Sat Mar 01, 2008 6:49 am
by PB
Anyone know how to do this with two or more ComboBoxGadgets?
I just tried with two but it makes the second gadget invisible! :shock:

What I mean is: I'm currently doing it via a callback for both gadgets,
but is there a way to combine both (and even more) into just a single
callback, to reduce bloat?

Posted: Sat Mar 01, 2008 9:41 am
by gnozal
PB wrote:Anyone know how to do this with two or more ComboBoxGadgets?
Here is some example (just added some props) :

Code: Select all

Procedure ComboCallBack( hwnd.l, message.l, wParam.l, lParam.l ) 
  
  oldcombproc = GetProp_(hwnd.l, "OldProc")
  Result.l = CallWindowProc_(oldcombproc, hwnd, message, wParam, lParam ) 
  If Result
    If (message = #WM_CTLCOLOREDIT) Or (message = #WM_CTLCOLORLISTBOX) 
      SetBkMode_(wParam,#TRANSPARENT) 
      SetTextColor_(wParam,GetProp_(hwnd.l, "TextColor")) 
      SetBkColor_(wParam,GetProp_(hwnd.l, "BackColor")) 
      Result = GetProp_(hwnd.l, "BackBrush")
    ElseIf message = #WM_DESTROY 
      RemoveProp_(hwnd.l, "OldProc")
      RemoveProp_(hwnd.l, "TextColor")
      RemoveProp_(hwnd.l, "BackColor")
      DeleteObject_(GetProp_(hwnd.l, "BackBrush"))
      RemoveProp_(hwnd.l, "BackBrush")
    EndIf 
  EndIf
  
  ProcedureReturn Result 
  
EndProcedure 


OpenWindow( 0, 200,400,200,100, "Colorisation et Callback", #PB_Window_SystemMenu) 

CreateGadgetList( WindowID(0) ) 

ComboBoxGadget( 1, 10, 10, 180, 120, #PB_ComboBox_Editable ) 
AddGadgetItem(1, -1, "123" ) 
AddGadgetItem(1, -1, "456" ) 
AddGadgetItem(1, -1, "789" ) 
SetGadgetState(1,0) 


ComboBoxGadget( 2, 10, 60, 180, 120, #PB_ComboBox_Editable ) 
AddGadgetItem(2, -1, "123" ) 
AddGadgetItem(2, -1, "456" ) 
AddGadgetItem(2, -1, "789" ) 
SetGadgetState(2,0) 
  
SetProp_(GadgetID(1), "OldProc", SetWindowLong_(GadgetID(1), #GWL_WNDPROC, @ComboCallBack()) )
SetProp_(GadgetID(1), "TextColor", #White)
SetProp_(GadgetID(1), "BackColor", #Blue)
SetProp_(GadgetID(1), "BackBrush", CreateSolidBrush_(#Blue))

SetProp_(GadgetID(2), "OldProc", SetWindowLong_(GadgetID(2), #GWL_WNDPROC, @ComboCallBack()) )
SetProp_(GadgetID(2), "TextColor", #Red)
SetProp_(GadgetID(2), "BackColor", #White)
SetProp_(GadgetID(2), "BackBrush", CreateSolidBrush_(#White))

Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow 
End

Posted: Sat Mar 01, 2008 11:42 am
by PB
Thanks gnozal, good example with the props. :)

Posted: Mon Mar 03, 2008 3:16 pm
by kinglestat
srod asking a question?
now, THAT took my by surprise
(of course I have no clue what the question is all about)

Re: Colouring the static-field in an un-editable ComboBox

Posted: Sun Feb 05, 2023 7:13 pm
by HarrysLad
An old thread but ...
Is it possible to set the foreground and/or background colours of a ComboBoxGadget?
This is not usually a problem but on one of my apps (and in Dark-Mode) I get a white foreground on a white background!

{edit:] this is on macOS. I should have mentioned that. :oops:

Dave