Solved

Colouring the CListView

Posted on 2001-08-07
6
427 Views
Last Modified: 2013-11-20
Hi,
I?ve been trying to make the code below to work out.
What I was trying to do is to colour each column of my ListView. I had extract the OnCustomDraw from codeGuru, but it doesn?t work properly. I?d already checked the version of my comctl32.dll which is 5.28. When I was debbugging the code below, I realized that it passed by the CDDS_PREPAINT case and CDDS_ITEMPREPAINT case, but never by the last case. But even when it passed by CDDS_ITEMPREPAINT it didn?t change the colour of the text. Is there something missed?

Thanks.



BEGIN_MESSAGE_MAP(CMyView, CListView)
      //{{AFX_MSG_MAP(CMyView)
//      ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

void CMyView::OnDraw(CDC* pDC)
{
      CDocument* pDoc = GetDocument();
      // TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::inic ()
{
    ASSERT (pMovel != NULL);

    CListCtrl& rList = GetListCtrl();

      
    for( UINT i = 0; i < 10; i++ )
    {
        setListCtrlText( rList, i, COL_1, _T( "info 1" ) );
        setListCtrlText( rList, i, COL_2, _T( "info 2" ) );
        setListCtrlText( rList, i, COL_3, _T( "info 3" ) );
        setListCtrlText( rList, i, COL_4, _T( "info 4" ) );
        setListCtrlText( rList, i, COL_5, _T( "info 5" ) );
        }
    }
}


void CMyView::OnInitialUpdate()
{
      CListViewEx::OnInitialUpdate();
      
      /*------------------------------
        Cria as colunas da CListView
      ------------------------------*/
      CListCtrl& rList = GetListCtrl();

      createColumn( rList, "1", COL_1,  80, LVCFMT_LEFT );
      createColumn( rList, "2", COL_2,  80, LVCFMT_RIGHT );
      createColumn( rList, "3", COL_3,  80, LVCFMT_RIGHT );
      createColumn( rList, "4", COL_4,  80, LVCFMT_RIGHT );
      createColumn( rList, "5", COL_5,  80, LVCFMT_RIGHT );
}

 
void CMyView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
   *pResult = 0;

   COLORREF dwColourTxt = 0;
   COLORREF dwColourBk = RGB(255, 255, 255);

   LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

   switch(lplvcd->nmcd.dwDrawStage)
   {
      case CDDS_PREPAINT :
      {
         *pResult = CDRF_NOTIFYITEMDRAW;
         return;
      }
            
      // Modify item text and or background
      case CDDS_ITEMPREPAINT:
      {
         if(lplvcd->nmcd.dwItemSpec == 0)
         {
            dwColourTxt = RGB(255, 0, 0); // Red
               dwColourBk = RGB(255, 255, 0);
         }
         else if(lplvcd->nmcd.dwItemSpec == 1)
         {
            dwColourTxt = RGB(0, 255, 0); // Green
         }
         else
         {
            dwColourTxt = RGB(0, 0, 255); // Blue
         }
         lplvcd->clrText = dwColourTxt;
         // If you want the sub items the same as the item set *pResult
         // to CDRF_NEWFONT
         *pResult = CDRF_NOTIFYSUBITEMDRAW;
         return;
      }

      // Modify sub item  text and or background
      case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
      {
         // Ignore the first sub item it is the item handled above
         if(lplvcd->iSubItem != 0)
         {
            // First sub item
            if(lplvcd->iSubItem == 1)
            {
               dwColourTxt = RGB(0, 127, 127);  // Pale blue
               dwColourBk = RGB(255, 255, 255); // White
            }
            // All sub items after the first
            else
            {
               dwColourTxt = RGB(255, 255, 255);   // White
               if(lplvcd->nmcd.dwItemSpec == 0)
               {
                  dwColourBk = RGB(255, 0, 0); // Red
               }
               else if(lplvcd->nmcd.dwItemSpec == 1)
               {
                  dwColourBk = RGB(0, 255, 0);  // Green
               }
               else
               {
                  dwColourBk = RGB(0, 0, 255);  // Blue
               }
            }
            lplvcd->clrText = dwColourTxt;
            lplvcd->clrTextBk = dwColourBk;
         }
         *pResult = CDRF_NEWFONT;
         return;
      }
   }
}


void CMyView::setListCtrlText(
      CListCtrl&      rLista,
      int                  iIndice,
      int                  iColuna,
      CString            sTexto)
{
      TCHAR      rgtszDesc[255];
      _tcscpy( rgtszDesc, sTexto );

      LV_ITEM      lvitem;
      lvitem.mask            = LVIF_TEXT;
      lvitem.iItem      = iIndice;
      lvitem.iSubItem      = iColuna;
      lvitem.pszText      = rgtszDesc;

      if ( ( iColuna == 0 ) && rLista.GetItemCount() <= iIndice )
            rLista.InsertItem( &lvitem );
      else
            rLista.SetItem( &lvitem );

}

void CMyView::createColumn(
      CListCtrl&      rLista,
      CString            sLabel,
      int                  iColuna,
      int                  iWidth,
      int                  iAlign  )
{
      TCHAR      rgtszLabel[255];
      _tcscpy( rgtszLabel, sLabel );

      LV_COLUMN lvcolumn;

      lvcolumn.mask     = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
      lvcolumn.fmt      = iAlign;
      lvcolumn.pszText  = rgtszLabel;
      lvcolumn.iSubItem = iColuna;
      lvcolumn.cx       = iWidth;
      rLista.InsertColumn( iColuna, &lvcolumn );

}
0
Comment
Question by:mauling
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 6362717
this code colors the text in every second row in red:

void CMyView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
     NMCUSTOMDRAW *pNM = (NMCUSTOMDRAW*)pNMHDR;
     LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNM;
     switch( pNM->dwDrawStage )
          {
          case CDDS_PREPAINT:
               *pResult = CDRF_NOTIFYITEMDRAW;
              return;
          case CDDS_ITEMPREPAINT:
              if(lplvcd->nmcd.dwItemSpec % 2)
                    lplvcd->clrText = RGB(255,0,0);
               *pResult = CDRF_NOTIFYITEMDRAW;
               return;
          }

     *pResult = CDRF_DODEFAULT;
}

good luck
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 100 total points
ID: 6362770
if u need some clarifications or any other code would be helpful let me know
0
 

Author Comment

by:mauling
ID: 6363589
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mauling
ID: 6363594
0
 

Author Comment

by:mauling
ID: 6364383
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9521145
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: sedgwick

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question