Les images que j'utilise dans cette exemple peuvent être téléchargées ici : http://keonet.free.fr/Images_BMP32.zip
Code : Tout sélectionner
;ComboBox personnalisée, par Octavius
;Création d'une icône transparente pour chaque item à partir d'une image 32 bits (PNG ou BMP 32 bits)
;Possibilité de régler la hauteur du menu déroulant
EnableExplicit
#MyCombo=0
#White=$FFFFFF
#ItemHeight=16 ;Par défaut la hauteur PB est 14 mais ici j'utilise des images 16*16 pixels
#ComboHeight=#ItemHeight+6
Structure item
Text$
Icon.l
EndStructure
Define i.l
Global NewList Items.item()
Procedure SetDropDownHeight(Gadget.l,NumberOfItem.l)
MoveWindow_(GadgetID(Gadget),GadgetX(Gadget),GadgetY(Gadget),GadgetWidth(Gadget),2+GadgetHeight(Gadget)+NumberOfItem*(GadgetHeight(Gadget)-6),#True)
EndProcedure
Procedure CreateIcon(Image.l)
Protected NewIcon.ICONINFO,Mask.l,Icon.l
If IsImage(Image)
Mask=CreateImage(#PB_Any,ImageWidth(Image),ImageHeight(Image),32)
NewIcon\fIcon=#True
NewIcon\hbmMask=ImageID(Mask)
NewIcon\hbmColor=ImageID(Image)
Icon=CreateIconIndirect_(@NewIcon)
FreeImage(Mask)
EndIf
ProcedureReturn Icon
EndProcedure
Procedure WinProc(hwnd,msg,wparam,lparam)
Protected *dis.DRAWITEMSTRUCT,*mis.MEASUREITEMSTRUCT,Bmp.BITMAP,Text$
Protected Result.l,hBrush.l,OldBrush.l,hdcIn.l,hdcSrc.l
Result=#PB_ProcessPureBasicEvents
Select msg
Case #WM_DRAWITEM
*dis=lparam
If *dis\ctlid=#MyCombo
With *dis
Text$=GetGadgetItemText(\ctlid,\itemid)
If \itemstate & #ODS_SELECTED
hBrush=CreateSolidBrush_(GetSysColor_(#COLOR_HIGHLIGHT))
OldBrush=SelectObject_(\hdc,hBrush)
FillRect_(\hdc,\rcitem,hBrush)
DeleteObject_(hBrush)
SelectObject_(\hdc,OldBrush)
If \itemid>=0 And \itemid<=CountGadgetItems(\ctlid)-1
ChangeCurrentElement(Items(),GetGadgetItemData(\ctlid,\itemid))
If Items()\Icon
DrawIconEx_(\hdc,4,\rcitem\top,Items()\Icon,#ItemHeight,#ItemHeight,0,0,#DI_NORMAL|#DI_COMPAT)
EndIf
EndIf
SetBkColor_(\hdc,GetSysColor_(#COLOR_HIGHLIGHT))
TextOut_(\hdc,\rcitem\left+24,\rcitem\top+1,Text$,Len(Text$))
DrawFocusRect_(\hdc,\rcitem)
Else
hBrush=CreateSolidBrush_(#White)
OldBrush=SelectObject_(\hdc,hBrush)
FillRect_(\hdc,\rcitem,hBrush)
DeleteObject_(hBrush)
SelectObject_(\hdc,OldBrush)
If \itemid>=0 And \itemid<=CountGadgetItems(\ctlid)-1
ChangeCurrentElement(Items(),GetGadgetItemData(\ctlid,\itemid))
If Items()\Icon
DrawIconEx_(\hdc,4,\rcitem\top,Items()\Icon,#ItemHeight,#ItemHeight,0,0,#DI_NORMAL)
EndIf
EndIf
SetBkColor_(\hdc,#White)
TextOut_(\hdc,\rcitem\left+24,\rcitem\top+1,Text$,Len(Text$))
EndIf
EndWith
EndIf
Case #WM_MEASUREITEM
*mis=lparam
If *mis\ctlid=#MyCombo
*mis\itemheight=#ItemHeight
EndIf
EndSelect
ProcedureReturn Result
EndProcedure
OpenWindow(0,0,0,300,50,"ComboBox avec icônes, par Octavius",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowCallback(@WinProc())
ComboBoxGadget(#MyCombo,10,10,280,#ComboHeight,#CBS_OWNERDRAWVARIABLE)
For i=0 To 7
AddElement(Items())
Items()\Text$="Item n°"+Str(i+1)
Items()\Icon=CreateIcon(LoadImage(#PB_Any,"Image_"+Str(i+1)+".bmp"))
AddGadgetItem(#MyCombo,i,Items()\Text$)
SetGadgetItemData(#MyCombo,i,@Items())
Next i
SetDropDownHeight(#MyCombo,4)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow