Page 1 of 1

OwnerDraw combobox

Posted: Tue Dec 09, 2003 6:05 pm
by Fred
Code updated for 5.20+. Not fully working anymore as PureBasic internals have changed.

Here is (another) little ownerdraw example which allow to customize a combobox as you want:

Code: Select all

; ComboBox OwnerDraw in PureBasic
;

Enumeration
  #Window_0
EndEnumeration

Enumeration
  #Gadget_0
EndEnumeration


Procedure Open_Window_0()
  If OpenWindow(#Window_0, 0, 0, 400, 100, "New window ( 0 )", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_TitleBar)
    ComboBoxGadget(#Gadget_0, 60, 40, 330, 25, #CBS_OWNERDRAWFIXED)
  EndIf
EndProcedure

Open_Window_0()

Procedure WindowCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents

  Select Message

    Case #WM_DRAWITEM
      *DrawItem.DRAWITEMSTRUCT = lParam
     
      If *DrawItem\CtlType = #ODT_COMBOBOX
        SetBkMode_(*DrawItem\hDC, #TRANSPARENT) ; Text is rendered transparent
       
        If *DrawItem\ItemState & #ODS_FOCUS
          Brush = CreateSolidBrush_($FFEEFF)
          FillRect_(*DrawItem\hDC, *DrawItem\rcItem, Brush)
          DeleteObject_(Brush)
         
          SetTextColor_(*DrawItem\hDC, $FF)
        Else
          FillRect_(*DrawItem\hDC, *DrawItem\rcItem, GetStockObject_(#WHITE_BRUSH))
        EndIf

        If *DrawItem\itemID <> -1
          Text$ = Space(512)
          SendMessage_(*DrawItem\hwndItem, #CB_GETLBTEXT, *DrawItem\itemID, @Text$)
          
          DrawIconEx_(*DrawItem\hDC, *DrawItem\rcItem\left+2   , *DrawItem\rcItem\top+1, LoadIcon_(0, #IDI_ASTERISK), 16, 16, 0, 0, #DI_NORMAL)
          TextOut_   (*DrawItem\hDC, *DrawItem\rcItem\left+2+20, *DrawItem\rcItem\top+1, Text$, Len(Text$))
        EndIf
      EndIf
 
  EndSelect
 
  ProcedureReturn Result
EndProcedure


SetWindowCallback(@WindowCallback())

  AddGadgetItem(#Gadget_0, -1, "Test1")
  AddGadgetItem(#Gadget_0, -1, "Test2")
  AddGadgetItem(#Gadget_0, -1, "Test3")
 
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

End

Posted: Tue Dec 09, 2003 6:11 pm
by Codemonger
Awesome Fred, I was using the address of lParam for my userdrawnbuttons ... that's why I couldn't retrieve the info from the lparam correctly :roll:

Great example :D

Re: OwnerDraw combobox

Posted: Tue Dec 09, 2003 7:29 pm
by Berikco
Fred wrote:Here is (another) little ownerdraw example which allow to customize a combobox as you want:...
Hey, nice, and it even works :twisted:

Posted: Tue Dec 09, 2003 11:01 pm
by Fred
Tsss :wink:

Posted: Wed Dec 10, 2003 10:06 am
by CNESM
Hi,

for my actuell project i build my own comboboxgadget with own style. Take a look on it:

[Delete]

Posted: Tue May 30, 2006 5:22 pm
by DoubleDutch
Looks like it isn't just me...

This code by Fred also crashes like http://www.purebasic.fr/english/viewtopic.php?t=18746 on V4...

Code: Select all

; ComboBox OwnerDraw in PureBasic 
; 

Enumeration 
  #Window_0 
EndEnumeration 

Enumeration 
  #Gadget_0 
EndEnumeration 


Procedure Open_Window_0() 
  If OpenWindow(#Window_0, 0, 0, 400, 100 , "New window ( 0 )", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(0)) 
      ComboBoxGadget(#Gadget_0, 60, 40, 330, 200,  #CBS_OWNERDRAWFIXED) 
      
    EndIf 
  EndIf 
EndProcedure 

Open_Window_0() 

#DI_NORMAL = $0003 

Procedure WindowCallback(WindowID, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 

  Select Message 

    Case #WM_DRAWITEM 
      *DrawItem.DRAWITEMSTRUCT = lParam 
      
      If *DrawItem\CtlType = #ODT_COMBOBOX 
        SetBkMode_(*DrawItem\hDC, #TRANSPARENT) ; Text is rendered transparent 
        
        If *DrawItem\ItemState & #ODS_FOCUS 
          Brush = CreateSolidBrush_($FFEEFF) 
          FillRect_(*DrawItem\hDC, *DrawItem\rcItem, Brush) 
          DeleteObject_(Brush) 
          
          SetTextColor_(*DrawItem\hDC, $FF) 
        Else 
          FillRect_(*DrawItem\hDC, *DrawItem\rcItem, GetStockObject_(#WHITE_BRUSH)) 
        EndIf 

        If *DrawItem\itemID <> -1 
          Text$ = Space(512) 
          SendMessage_(*DrawItem\hwndItem, #CB_GETLBTEXT, *DrawItem\itemID, @Text$) 
          
          DrawIconEx_(*DrawItem\hDC, *DrawItem\rcItem\left+2   , *DrawItem\rcItem\top+1, LoadIcon_(0, #IDI_ASTERISK), 16, 16, 0, 0, #DI_NORMAL) 
          TextOut_   (*DrawItem\hDC, *DrawItem\rcItem\left+2+20, *DrawItem\rcItem\top+1, Text$, Len(Text$)) 
        EndIf 
      EndIf 
  
  EndSelect 
  
  ProcedureReturn Result 
EndProcedure 


SetWindowCallback(@WindowCallback()) 

  AddGadgetItem(#Gadget_0, -1, "Test1") 
  AddGadgetItem(#Gadget_0, -1, "Test2") 
  AddGadgetItem(#Gadget_0, -1, "Test3") 
  
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow 

End 
Does anyone else have it crash on exit, just making sure its not something local to my machine...?

Posted: Tue May 30, 2006 5:53 pm
by rsts
Crashes here too.

You're finding some kind of infection :D

cheers

Posted: Tue May 30, 2006 6:33 pm
by DoubleDutch
Thanks, I'll post it in bugs.

Posted: Wed May 31, 2006 9:17 pm
by Konne
Is there a way to change the height of the Gadget, like 21, because the height parameter is just for the dropbox height....

Posted: Wed May 31, 2006 9:52 pm
by srod
Konne wrote:Is there a way to change the height of the Gadget, like 21, because the height parameter is just for the dropbox height....

Code: Select all

SendMessage_(GadgetID(#combo), #CB_SETITEMHEIGHT, -1,30)

Re: OwnerDraw combobox

Posted: Mon Nov 04, 2013 7:31 pm
by ehowington
Ok can anyone answer why my text comes up like this in the combobox

caR
caR


?????????????

Re: OwnerDraw combobox

Posted: Mon Nov 04, 2013 7:38 pm
by ehowington

Code: Select all

; ComboBox OwnerDraw in PureBasic
;

Enumeration
  #Window_0
EndEnumeration

Enumeration
  #Gadget_0
EndEnumeration


Procedure Open_Window_0()
  If OpenWindow(#Window_0, 0, 0, 400, 100, "New window ( 0 )", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_TitleBar)
    ComboBoxGadget(#Gadget_0, 60, 40, 330, 25, #CBS_OWNERDRAWFIXED | #CBS_HASSTRINGS)
  EndIf
EndProcedure

Open_Window_0()

Procedure WindowCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents

  Select Message

    Case #WM_DRAWITEM
      *DrawItem.DRAWITEMSTRUCT = lParam
     
      If *DrawItem\CtlType = #ODT_COMBOBOX
        SetBkMode_(*DrawItem\hDC, #TRANSPARENT) ; Text is rendered transparent
       
        If *DrawItem\ItemState & #ODS_FOCUS
          Brush = CreateSolidBrush_($FFEEFF)
          FillRect_(*DrawItem\hDC, *DrawItem\rcItem, Brush)
          DeleteObject_(Brush)
         
          SetTextColor_(*DrawItem\hDC, $FF)
        Else
          FillRect_(*DrawItem\hDC, *DrawItem\rcItem, GetStockObject_(#WHITE_BRUSH))
        EndIf

        If *DrawItem\itemID <> -1
          Text$ = Space(512)
          SendMessage_(*DrawItem\hwndItem, #CB_GETLBTEXT, *DrawItem\itemID, @Text$)
         
          DrawIconEx_(*DrawItem\hDC, *DrawItem\rcItem\left+2   , *DrawItem\rcItem\top+1, LoadIcon_(0, #IDI_ASTERISK), 16, 16, 0, 0, #DI_NORMAL)
          TextOut_   (*DrawItem\hDC, *DrawItem\rcItem\left+2+20, *DrawItem\rcItem\top+1, Text$, Len(Text$))
        EndIf
      EndIf
 
  EndSelect
 
  ProcedureReturn Result
EndProcedure


SetWindowCallback(@WindowCallback())

  AddGadgetItem(#Gadget_0, -1, "Test1")
  AddGadgetItem(#Gadget_0, -1, "Test2")
  AddGadgetItem(#Gadget_0, -1, "Test3")
 
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

End

Never mind found the issue NOTE THE #CBS_HASSTRINGS