Colouring the static-field in an un-editable ComboBox

Just starting out? Need help? Post your questions and find answers here.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Colouring the static-field in an un-editable ComboBox

Post 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.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Anyone know a way of coloring the drop-down list of the ComboBox? :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Thanks, now I can't go to bed. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Request for Fred/Freak: Can coloring be added for ComboBoxGadgets, please?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Thanks gnozal, good example with the props. :)
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

srod asking a question?
now, THAT took my by surprise
(of course I have no clue what the question is all about)
I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
HarrysLad
User
User
Posts: 57
Joined: Fri Jun 04, 2010 9:29 pm
Location: Sunny Spain

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

Post 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
Post Reply