Ownerdraw Listicon: Multicolored items

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Ownerdraw Listicon: Multicolored items

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Justin.

This code shows how to handle an ownerdraw listicon (list mode), to have different colors with each item.

My initial goal was to have a listicon in report mode with different colors for each subitem, but i'm having problems with it, any help would be appreciated:

http://msdn.microsoft.com/library/en-us ... stdraw.asp

Code: Select all

;Custom draw ListView

#LVS_OWNERDRAWFIXED=1024
#NM_CUSTOMDRAW=#NM_FIRST-12

#CDDS_ITEM=65536
#CDDS_PREPAINT=1
;#CDDS_ITEMPREPAINT=#CDDS_ITEM|#CDDS_PREPAINT
#CDRF_NOTIFYITEMDRAW=32
#CDRF_NEWFONT=2

Procedure listproc(hwnd,msg,wparam,lparam)
  ret=#PB_ProcessPureBasicEvents
  Select msg
      
    Case #WM_NOTIFY
      *ptr.NMLVCUSTOMDRAW=lparam
      
      Select *ptr\nmcd\hdr\code
        Case #NM_CUSTOMDRAW
          
          Select *ptr\nmcd\dwDrawStage
            Case #CDDS_PREPAINT
              ret=#CDRF_NOTIFYITEMDRAW
              
            Case #CDDS_ITEM | #CDDS_PREPAINT
              ;set text color
              *ptr\clrText=RGB(Random(255),Random(255),Random(255))
              ret=#CDRF_NEWFONT
          EndSelect
          
      EndSelect
      
  EndSelect
  ProcedureReturn ret
EndProcedure


hWnd=OpenWindow(0,10,10,500,500,"Custom Draw List View",#PB_Window_SystemMenu)

hl=ListIconGadget(1,10,10,400,400,"",100,#PB_ListIcon_GridLines|#LVS_OWNERDRAWFIXED)
SetGadgetAttribute(1, #PB_ListIcon_DisplayMode, 2)


ButtonGadget(2, 20, 450, 50, 20,"Add")

SetWindowCallback(@listproc())

Repeat
  eid=WaitWindowEvent()
  If eid=#PB_Event_Gadget
    Select EventGadget()
        
      Case 2
        AddGadgetItem(1,-1,"Customdrawing!")
    EndSelect
  EndIf
Until eid=#PB_Event_CloseWindow
Last edited by fsw on Fri Aug 23, 2013 1:10 am, edited 1 time in total.
Reason: Code updated for 5.20+
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hi Justin!

Very very nice example!! It should also be possible to add bitmaps to columns with custumdrawing but I dont figured it out in the last days. possible it will comes to me in the next days with a closer look on your source. :)

Thanks a lot!

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

There is an easy way to add a bitmap to the entire list(reading from disk), but to have a bitmap for each column has to be tricky.

It seems that the LVS_OWNERDRAWFIXED style is not necessary, that is why the report mode did not work (are PB gadgets already ownerdrawn?)

This is what i do with the report mode, colorizes only the second column, although some flickering appears.

;Custom draw ListView

;#LVS_OWNERDRAWFIXED=1024
#NM_CUSTOMDRAW=#NM_FIRST-12

#CDDS_ITEM=65536
#CDDS_PREPAINT=1
#CDDS_SUBITEM=131072
;CDDS_ITEMPREPAINT=#CDDS_ITEM|#CDDS_PREPAINT
;#CDRF_DODEFAULT=0
#CDRF_NOTIFYITEMDRAW=32
#CDRF_NOTIFYSUBITEMDRAW=32
#CDRF_NEWFONT=2
;#CDRF_SKIPDEFAULT=4

structure NMCUSTOMDRAW
hdr.NMHDR
dwDrawStage.l
hdc.l
rc.RECT
dwItemSpec.l
uItemState.l
lItemlParam.l
endstructure

structure NMLVCUSTOMDRAW
nmcd.NMCUSTOMDRAW
clrText.l
clrTextBk.l
iSubItem.l
endstructure

Procedure listproc(hwnd,msg,wparam,lparam)
ret=#PB_ProcessPureBasicEvents
Select msg

Case #WM_NOTIFY
*ptr.NMLVCUSTOMDRAW=lparam

select *ptr\nmcd\hdr\code
case #NM_CUSTOMDRAW

select *ptr\nmcd\dwDrawStage
case #CDDS_PREPAINT
ret=#CDRF_NOTIFYITEMDRAW

case #CDDS_ITEM | #CDDS_PREPAINT ;CDDS_ITEMPREPAINT
ret=#CDRF_NOTIFYSUBITEMDRAW

case #CDDS_SUBITEM | #CDDS_ITEM | #CDDS_PREPAINT ;CDDS_SUBITEM | CDDS_ITEMPREPAINT
;set text color
if *ptr\iSubItem=1
*ptr\clrText=rgb(random(255),random(255),random(255))
else
*ptr\clrText=rgb(0,0,0) ;assuming black is the default
endif
ret=#CDRF_NEWFONT

endselect

endselect

EndSelect
ProcedureReturn ret
EndProcedure


hWnd=OpenWindow(0,10,10,500,500,#PB_Window_SystemMenu,"Custom Draw List View")
CreateGadgetList(hWnd)

hl=ListIconGadget(1,10,10,400,400,"Name",100,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)


AddGadgetColumn(1,1,"Address",120)
AddGadgetColumn(1,2,"Phone",80)

buttongadget(2,20,450,50,20,"Add")

SetWindowCallback(@listproc())

Repeat
eid=WaitWindowEvent()
if eid=#PB_EventGadget
select EventGadgetID()

case 2
AddGadgetItem(1,-1,"Jim" + Chr(10) + "xxxxx" + Chr(10) + "555")
endselect
endif
Until eid=#PB_EventCloseWindow
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi Justin
Very nice examples.... The first example have a litte small bug. When adding a lot of NewText, so the next text will outside of the right side (you will only see the first two chars of the text)... Just scroll then to right to see the complete text and as you may notice, the text is in two colors... (first two chars have another color as the following chars)... Only as Info... anyway nice work!


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

every day i learn more about the listicongadget.
very good code Justin,thx to share your code with us.

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

easy :)
Post Reply