Solved

Colouring the CListView

Posted on 2001-08-07
6
426 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 130
Named range not carried over 10 63
sumDigits challenge 9 147
method notes when mouse over in eclipse 5 86
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

775 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