; if using a resource script define the listbox like this
; else if using createwindow be sure to use LBS_OWNERDRAWFIXED

;    LISTBOX	    500, 4, 50, 234, 60, LBS_DISABLENOSCROLL | LBS_HASSTRINGS | LBS_OWNERDRAWFIXED |
;    			WS_VSCROLL | 0x2000, WS_EX_CLIENTEDGE

;########################################################

; protos
        additem PROTO :DWORD, :DWORD, 

;########################################################


.data?
        Tm              TEXTMETRIC <?>
        ItemBuff        db MAX_PATH dup (?)         ; temporary buffer for items
        y               dd ?                        ; y axis position of items
        hBrush          dd ?                        ; hanle of brush in owner drawn listbox
        wRect           RECT <?>
        DrawItem        DRAWITEMSTRUCT <?>          ; owner drawn listbox items
	listhWnd		dd	?		    ; listbox handle
	

;########################################################
; add this to your WNDPROC
;########################################################



  .elseif uMsg == WM_DRAWITEM

        .if wParam == 500				; id of listbox if using resource script
        						; not needed if using createwindow and
        						; the listbox is subclassed

                mov esi, lParam
                mov edi, offset DrawItem
                mov ecx, sizeof DRAWITEMSTRUCT
                rep movsb
                
                .if DrawItem.itemID != -1
                   
                        invoke SendMessage,listhWnd,LB_GETTEXT,DrawItem.itemID,addr ItemBuff
                        push eax  ; pushing the length
                        invoke GetTextMetrics,DrawItem.hdc,addr Tm
        
                        mov eax,DrawItem.rcItem.bottom
                        add eax,DrawItem.rcItem.top
                        sub eax, Tm.tmHeight
                        xor edx,edx
                        mov ecx,2
                        div ecx
                        mov y, eax

                        .if (DrawItem.itemState & ODS_FOCUS)
                                invoke CreateSolidBrush,DrawItem.itemData ;000000ffh
                                mov hBrush,eax
                                invoke FillRect,DrawItem.hdc,addr DrawItem.rcItem,eax
                                invoke SetTextColor,DrawItem.hdc,00FFFFFFh
                                invoke SetBkColor,DrawItem.hdc,DrawItem.itemData ;000000FFh
                                invoke DeleteObject,hBrush
                        .else
                                invoke CreateSolidBrush,00FFFFFFh
                                mov hBrush,eax
                                invoke FillRect,DrawItem.hdc,addr DrawItem.rcItem,eax
                                invoke SetTextColor,DrawItem.hdc,DrawItem.itemData
                                invoke SetBkColor,DrawItem.hdc,00FFFFFFh
                                invoke DeleteObject,hBrush
                        .endif        

                        pop edx
                        mov ecx, DrawItem.rcItem.left
                        add ecx,4
                        invoke TextOut,DrawItem.hdc,ecx,y,addr ItemBuff,edx
                        xor eax,eax
                        ret
                .endif
                
        .endif				; see comment for the corresponding .if statement


;########################################################
; function for adding items to the listbox using colors
;########################################################

;listhwnd == handle of listbox
;color == RGB color number  eg 00FFEEDDh  DD being red EE being green FF being blue
;string == pointer to a string to add


additem proc string:DWORD,color:DWORD, 
	invoke SendMessage,listhWnd,LB_ADDSTRING,0, string
	push eax
	invoke SendMessage,listhWnd,LB_SETITEMDATA,eax,color
	pop eax
	invoke SendMessage,listhWnd,LB_SETCURSEL,eax,0
	invoke SetFocus,listhWnd
	ret
additem endp


