June 17, 2024, 06:24:40 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Coloring a listview line

Started by JoaoAfonso, January 30, 2009, 01:57:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

JoaoAfonso

Good evening.
I pretend to color the entire line of a list view control. I've read the examples in projects directory, and also some other old IB examples, and the code is made to color single cells and uses too much codelines. I believe that there can be an easy way to do it, just don't know how.
What I need is to load data from an mdb file, and if a given number is less than 0, the entire line is coloured red. Any hints to do it easier than in the list view examples?
Thanks in advance
JoÃÆ'ƒÂÃ,£o Afonso
Viriato
-----------------
Iberia MUD
www.iberiamud.com
iberiamud.com:5900

JoaoAfonso

Doing some experiments with the listview demos I got eventually the minimum code to color lines in listviews:

Definitions:
DECLARE IMPORT,SetWindowLongA(hWnd:Int,nIndex:Int,dwNewLong:Int),Int

CONST DWL_MSGRESULT = 0
CONST CDDS_ITEM = 0x10000
CONST CDDS_SUBITEM = 0x20000
CONST CDDS_PREPAINT = 1
CONST NM_CUSTOMDRAW = (-12)
CONST CDDS_ITEMPREPAINT = (CDDS_ITEM | CDDS_PREPAINT)
CONST CDDS_SUBITEMPREPAINT = (CDDS_SUBITEM | CDDS_ITEMPREPAINT)
CONST CDRF_NOTIFYITEMDRAW = 0x20
CONST CDRF_NOTIFYSUBITEMDRAW = 0x20
CONST CDRF_DODEFAULT = 0
CONST CDRF_NEWFONT = 2

TYPE NMCUSTOMDRAWINFO
   NMHDR hdr
   UINT dwDrawStage
   UINT hdc
   WINRECT rc
   UINT dwItemSpec
   UINT uItemState
   UINT lItemlParam
ENDTYPE

TYPE NMLVCUSTOMDRAW
    NMCUSTOMDRAWINFO nmcd
    UINT clrText
    UINT clrTextBk
    INT iSubItem
ENDTYPE


Routine (still has some "extra code" because was got from the examples, and also some mine to experiment things myself. Anyway, is in this routine where we can define the colors and the items/subitems to color):
SUB ColorListView(hwnd AS UINT,lParam AS UINT,dial AS DIALOG,contr AS INT),UINT
UINT rv
POINTER colp
INT row,red,green,yellow,black,white
red=255:green=RGB(0,255,0):yellow=RGB(255,255,0):black=0:white=RGB(255,255,255)
                row=*<NMLVCUSTOMDRAW>lParam.nmcd.dwItemSpec
SELECT *<NMLVCUSTOMDRAW>lParam.nmcd.dwDrawStage
CASE CDDS_PREPAINT
rv = CDRF_NOTIFYITEMDRAW
CASE CDDS_ITEMPREPAINT
'*<NMLVCUSTOMDRAW>lParam.nmcd.dwItemSpec is the zero based item the Control is drawing
'check To see If the item number is odd or even and point the colp Pointer
'To the required variable color1 or color2
'but we want To Color each column item (subitem) individually
rv =CDRF_NOTIFYSUBITEMDRAW
CASE CDDS_SUBITEMPREPAINT
'*<NMLVCUSTOMDRAW>lParam.iSubItem contains the zero based Sub item number
'on System with IE 4.0 or greater installed.
INT item,tcolor
STRING str,div
item=*<NMLVCUSTOMDRAW>lParam.iSubItem
CONTROLCMD(dial,contr,@LVGETTEXT,row,item,str)
CONTROLCMD(dial,contr,@LVGETTEXT,row,3,div)
IF item=5
IF VAL(str)<0
colp=yellow
ENDIF
ENDIF
IF VAL(div)>0 THEN tcolor = green ELSE tcolor = black
colp=yellow
SELECT *<NMLVCUSTOMDRAW>lParam.iSubItem
' CASE 0:'  the ITEM Color
' *<NMLVCUSTOMDRAW>lParam.clrText = tcolor
' *<NMLVCUSTOMDRAW>lParam.clrTextBk = #colp :'set the text background Color For first item
' CASE 1:' the first Sub item
' *<NMLVCUSTOMDRAW>lParam.clrText = tcolor
' *<NMLVCUSTOMDRAW>lParam.clrTextBk = #colp :'set the text background Color For this subitem
DEFAULT: 'the Color of the rest of the line
*<NMLVCUSTOMDRAW>lParam.clrText = tcolor
*<NMLVCUSTOMDRAW>lParam.clrTextBk = #colp :'set the text background Color For this subitem
ENDSELECT
rv = CDRF_NEWFONT
DEFAULT
rv = CDRF_DODEFAULT
ENDSELECT
RETURN rv
ENDSUB


In the dialog handler (where 10 is the listview ID and d3 the dialog):
CASE @IDCONTROL
SELECT @CONTROLID
CASE 10
IF @NOTIFYCODE=NM_CUSTOMDRAW
SetWindowLongA(d3.hwnd,DWL_MSGRESULT,ColorListView(d3.hwnd,@LPARAM,d3,@CONTROLID))
RETURN TRUE
ENDIF


As I started the topic but figured it out, I felt I should "answer" myself :)
JoÃÆ'ƒÂÃ,£o Afonso
Viriato
-----------------
Iberia MUD
www.iberiamud.com
iberiamud.com:5900