Solved

Colouring the CListView

Posted on 2001-08-07
6
425 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now