Solved

Colouring the CListView

Posted on 2001-08-07
6
428 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 52
maven archtype selection in eclipse 1 58
java ^ examples 8 69
"Black Box" Testing of Control System Software 2 89
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 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