Code: Select all
;...Structure to hold character colors
Structure LVITEMCOLOR
iRow.l
iCol.l
iStartPos.l
iEndPos.l
iColor.l
EndStructure
;... 9 = rows 0 thru 9
;... 2 = columns 0 thru 2
;... 260 = characters 1 thru 260 (0 is a dummy)
Global Dim LVcolor.LVITEMCOLOR(9, 2, 260)
;... Create brushes for painting item background
Structure MYBRUSHES
brushDefault.l
brushSelected.l
EndStructure
Global brush.MYBRUSHES
brush\brushSelected = CreateSolidBrush_(RGB(255, 255, 155))
brush\brushDefault = GetStockObject_(#WHITE_BRUSH)
Procedure GetCharWidth(gad, c$)
ProcedureReturn SendMessage_(gad, #LVM_GETSTRINGWIDTH, 0, @c$)
EndProcedure
;Here we add some text to the underlying cell text to store the color info.
Procedure SetColor(gad, row, column, startp, endp, color)
LVcolor(row, column, 0)\iRow = row
LVcolor(row, column, 0)\iCol = column
LVcolor(row, column, 0)\iStartPos = startp
LVcolor(row, column, 0)\iEndPos = endp
LVcolor(row, column, 0)\iColor = color
For i = startp To endp
LVcolor(row, column, i)\iColor = color
Next
EndProcedure
Procedure myWindowCallback(hwnd, msg, wParam, lParam)
result = #PB_ProcessPureBasicEvents
Select msg
Case #WM_NOTIFY
*nmhdr.NMHDR = lParam
*lvCD.NMLVCUSTOMDRAW = lParam
If *lvCD\nmcd\hdr\hwndFrom=GadgetID(0) And *lvCD\nmcd\hdr\code = #NM_CUSTOMDRAW
Select *lvCD\nmcd\dwDrawStage
Case #CDDS_PREPAINT
result = #CDRF_NOTIFYITEMDRAW
Case #CDDS_ITEMPREPAINT
result = #CDRF_NOTIFYSUBITEMDRAW;
Case #CDDS_ITEMPREPAINT | #CDDS_SUBITEM
thisRow = *lvCD\nmcd\dwItemSpec
thisCol = *lvCD\iSubItem
;... Define rect for text
subItemRect.RECT\left = #LVIR_LABEL
subItemRect.RECT\top = *lvCD\iSubItem
;... Get the subitem rect
SendMessage_(GadgetID(0), #LVM_GETSUBITEMRECT, thisRow, @subItemRect)
subItemText$ = GetGadgetItemText(0, thisRow, thisCol)
;... Define text and background colors for column 0 and set Drawtext_() flags
lvFlags = #DT_END_ELLIPSIS | #DT_WORDBREAK;| #DT_MODIFYSTRING
;... Paint over unused icon rect
If *lvCD\iSubItem = 0
subItemRect\left = 0
EndIf
If GetGadgetState(0) = thisRow
;... If item is selected
FillRect_(*lvCD\nmcd\hdc, subItemRect, brush\brushSelected)
Else
;... If item is not selected
FillRect_(*lvCD\nmcd\hdc, subItemRect, brush\brushDefault)
EndIf
;... Here we will paste together the colored characters
;... to form a string. This should speed up the drawing
For c = 1 To Len(subItemText$)
c$ = Mid(subItemText$, c, 1)
For i = c + 1 To Len(subItemText$)
thisColor = LVcolor(thisRow, thisCol, c)\iColor
nextColor = LVcolor(thisRow, thisCol, i)\iColor
If thisColor = nextColor
c$ + Mid(subItemText$, i, 1)
c + 1
Else
Break
EndIf
Next i
SetTextColor_(*lvCD\nmcd\hdc, thisColor)
DrawText_(*lvCD\nmcd\hdc, c$, Len(c$), subItemRect, #DT_NOCLIP|#DT_END_ELLIPSIS)
subItemRect\left + GetCharWidth(*nmhdr\hwndFrom, c$)
If subItemRect\left >= subItemRect\right-4
Break
EndIf
Next c
result = #CDRF_SKIPDEFAULT
EndSelect
EndIf
EndSelect
ProcedureReturn result
EndProcedure
If OpenWindow(0, 0, 0, 480, 260, "Set Margins Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
SetWindowCallback(@myWindowCallback())
CreateStatusBar(0, WindowID(0))
ListIconGadget(0, 10, 10, 470, 225, "Column 0", 50, #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines | #PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(0, 1, "Column 1", 50)
AddGadgetColumn(0, 2, "Column 2", 150)
For a=0 To 9
addtext$ = "Column 0 item #" + Str(a) + Chr(10) + "Column 1 item #" + Str(a) + Chr(10) + "Column 2 item #" + Str(a)
atLen = Len(addtext$)
AddGadgetItem(0,-1, addtext$)
Next
SetColor(0, 0, 0, 0, 9, #Cyan)
SetColor(0, 0, 1, 0, 16, #Green)
SetColor(0, 0, 2, 0, 14, #Red)
SetColor(0, 5, 0, 0, 9, #Yellow)
SetColor(0, 6, 0, 10, 16, #Blue)
SetColor(0, 7, 1, 0, 8, #Red)
SetColor(0, 8, 2, 0, 14, #Magenta)
SetColor(0, 9, 2, 15, 16, #Blue)
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
DeleteObject_(brush\brushSelected)
EndIf
End
