OwnerDraw combobox

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

OwnerDraw combobox

Post 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
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post 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
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Re: OwnerDraw combobox

Post 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:
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Tsss :wink:
CNESM
User
User
Posts: 54
Joined: Sat Jun 21, 2003 11:15 pm
Contact:

Post by CNESM »

Hi,

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

[Delete]
Last edited by CNESM on Mon Jan 29, 2007 1:47 pm, edited 1 time in total.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post 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...?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Crashes here too.

You're finding some kind of infection :D

cheers
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Thanks, I'll post it in bugs.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post 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....
Apart from that Mrs Lincoln, how was the show?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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)
I may look like a mule, but I'm not a complete ass.
ehowington
Enthusiast
Enthusiast
Posts: 115
Joined: Sat Sep 12, 2009 3:06 pm

Re: OwnerDraw combobox

Post by ehowington »

Ok can anyone answer why my text comes up like this in the combobox

caR
caR


?????????????
ehowington
Enthusiast
Enthusiast
Posts: 115
Joined: Sat Sep 12, 2009 3:06 pm

Re: OwnerDraw combobox

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