Hey, I have a ListView control in a dialog box that I want to have alternating row colours but obviously account for when a row is highlighted. This is my attempt so far but all I get is a blank looking listview, but I know the values are inside of it because the scrollbar appears.

Note: the listview control WAS created with the LVS_OWNERDRAWFIXED style.

case WM_DRAWITEM:
        {
           DRAWITEMSTRUCT* di;
           di = (DRAWITEMSTRUCT*)lParam;
           
           if ( di->CtlType == ODT_LISTVIEW )
           {          
                di->rcItem;                
                
                HWND hItem;
                hItem = (HWND) wParam;
                     
                RECT rcRow, rcClient;
                rcRow = di->rcItem;
                
                GetClientRect( hItem , &rcClient );
                
                rcRow.right = rcClient.right;
                     
                HDC hDC = di->hDC;
                     
                if ( di->itemState == ODS_SELECTED )
                {
                   SetTextColor( hDC , GetSysColor( COLOR_HIGHLIGHTTEXT ) );
                   SetBkColor  ( hDC , GetSysColor( COLOR_HIGHLIGHT ) );
                   FillRect    ( hDC , &rcRow , (HBRUSH) GetSysColor( COLOR_HIGHLIGHT ) );
                }
                else if ( di->itemState == ODS_DEFAULT )
                {                
                   SetTextColor( hDC , di->itemID % 2 == 1 ? GetSysColor( COLOR_WINDOWTEXT ) : GetSysColor( COLOR_MENUTEXT ) );
                   SetBkColor  ( hDC , di->itemID % 2 == 1 ? GetSysColor( COLOR_WINDOW ) : GetSysColor( COLOR_MENU ) );
                   FillRect    ( hDC , &rcRow , di->itemID % 2 == 1 ? (HBRUSH) GetSysColor( COLOR_WINDOW ) : (HBRUSH) GetSysColor( COLOR_MENU ) );
                }
           }

        }	
        break;

Can you tell me if I'm going about it the right way or help to point out my errors. If I'm going way off the mark could you please point me in the direction of a tutorial on how to do it. Thanks.

Recommended Answers

All 3 Replies

Hmm.. it doesn't seem to be what I'm looking for, I'm just using a standard ListView control but I want to colour the rows alternatively by processing the WM_DRAWITEM message.

After your edit:

The last one seems to be what I'm looking for, thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.