Color Individual ListIconGadget Rows
Posted: Tue May 06, 2003 11:12 pm
Code updated for 5.20+
Thanks to Denis for the pointer (pun intended
) to the C code that I converted to PureBasic. The code below lets you set the text and background colors for each row individually. I haven't been able to make it work for each "cell" yet, but I'm going to keep trying!
Thanks to Denis for the pointer (pun intended

Code: Select all
Global ListGadget
; window callback routine to color listview rows
Declare NotifyCallback(WindowID, Message, wParam, lParam)
hWnd = OpenWindow(0, 0, 0, 356, 197, "Color List View Rows", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
; create list with seven columns
ListGadget = ListIconGadget(1, 10, 10, 336, 177,"", 70, #PB_ListIcon_GridLines)
AddGadgetColumn(1, 1, "Sun", 35)
AddGadgetColumn(1, 2, "Mon", 35)
AddGadgetColumn(1, 3, "Tue", 35)
AddGadgetColumn(1, 4, "Wed", 35)
AddGadgetColumn(1, 5, "Thu", 35)
AddGadgetColumn(1, 6, " Fri", 35)
AddGadgetColumn(1, 7, "Sat", 35)
; add some rows
AddGadgetItem(1, -1, " 9:00 am")
AddGadgetItem(1, -1, " 9:30 am")
AddGadgetItem(1, -1, "10:00 am")
AddGadgetItem(1, -1, "10:30 am")
AddGadgetItem(1, -1, "11:00 am")
AddGadgetItem(1, -1, "11:30 am")
AddGadgetItem(1, -1, "12:00 pm")
AddGadgetItem(1, -1, "12:30 pm")
AddGadgetItem(1, -1, " 1:00 pm")
AddGadgetItem(1, -1, " 1:30 pm")
AddGadgetItem(1, -1, " 2:00 pm")
AddGadgetItem(1, -1, " 2:30 pm")
AddGadgetItem(1, -1, " 3:00 pm")
AddGadgetItem(1, -1, " 3:30 pm")
AddGadgetItem(1, -1, " 4:00 pm")
AddGadgetItem(1, -1, " 4:30 pm")
AddGadgetItem(1, -1, " 5:00 pm")
; set callback routine
SetWindowCallback(@NotifyCallback())
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
End
; window callback routine to color listview rows
Procedure NotifyCallback(WindowID, Message, wParam, lParam)
; process NOTIFY message only
If Message = #WM_NOTIFY
; set stucture pointer
*LVCDHeader.NMLVCUSTOMDRAW = lParam
; CUSTOMDRAW message from desired gadget?
If *LVCDHeader\nmcd\hdr\hWndFrom = ListGadget And *LVCDHeader\nmcd\hdr\code = #NM_CUSTOMDRAW
Select *LVCDHeader\nmcd\dwDrawStage
Case #CDDS_PREPAINT
ProcedureReturn #CDRF_NOTIFYITEMDRAW
Case #CDDS_ITEMPREPAINT
; simple example - change text and background colors every other row
Row = *LVCDHeader\nmcd\dwItemSpec
If (Row/2) * 2 = Row
*LVCDHeader\clrText = RGB(255, 0, 0)
*LVCDHeader\clrTextBk = RGB(255, 255, 223)
Else
*LVCDHeader\clrText = RGB(0, 0, 255)
*LVCDHeader\clrTextBk = RGB(208, 208, 176)
EndIf
ProcedureReturn #CDRF_DODEFAULT
EndSelect
EndIf
Else
ProcedureReturn #PB_ProcessPureBasicEvents
EndIf
EndProcedure