Solved

Colouring the CListView

Posted on 2001-08-07
6
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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