Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a CListCtrl and I need to change the color of A SPECIFIC character/set of characters (which I choose by comparison) from the text of every cell in the list.

I know how to change the color of the entire text of the cell when I find the character/set of characters (by using 'strstr' command), but I can't find an example which shows how to change ONLY the character/set of characters.

Here is a sample of my code:

C++
void Agenda::OnCustomdrawMyList( NMHDR* pNMHDR, LRESULT* pResult )
    {
        NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)pNMHDR;

        *pResult = CDRF_DODEFAULT;

        if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
        {
            *pResult = CDRF_NOTIFYITEMDRAW;
            return;
        }else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
        {
            *pResult = CDRF_NOTIFYSUBITEMDRAW;
            return;
        }else if ( (CDDS_SUBITEM | CDDS_ITEMPREPAINT) == pLVCD->nmcd.dwDrawStage )
        {

            // So right now I am in the stage where a SUBITEM is PREPAINTED

            int nItem = pLVCD->nmcd.dwItemSpec;
            int nSubItem = pLVCD->iSubItem;

            char a[100];
            listControl.GetItemText(nItem,nSubItem,a,100);

            COLORREF textColorFound, textColorDefault;
            textColorDefault = RGB(0,0,0);
            pLVCD->clrText = textColorDefault;

            char* startingFrom;

            if( (startingFrom = strstr(a,filterText)) != NULL ) {
                    // Could I set a pointer here or something like that so
                    //   the coloring could start only from 'startingFrom'
                    //   and stop at 'strlen(filterText)' characters?

                textColorFound = RGB(205,92,92);
                pLVCD->clrText = textColorFound;
            }
            *pResult = CDRF_DODEFAULT;
        }
    }


* listControl is the variable for my CListCtrl
* the other things are pretty self-explanatory
Posted

The controls capable of such capability are controls like rich edit control and browser control.
So you need to figure out how to place such controls in a cell of a list control.

Here are some such articles on code project -
Embedding Controls in a ListView[^]
Simple CListCtrl with HyperLink Function[^]
TreeListViewEx Tree, List, and Drag and Drop[^]

Another option would be to create the required text as a bitmap and then show the bitmap in the list control.
Here is an article on using bitmaps with list controls -
Using a Virtual CListCtrl to Display Text and Bitmaps[^]
 
Share this answer
 
Besides the options from Solution 1, you can do it inside your custom draw handler by drawing the text of cells that match the criteria there and returning CDRF_SKIPDEFAULT or use owner drawing to draw all cells yourself (implementing DrawItem()).

But both methods require code to draw the cell content in a similar way like Windows does. The resulting code can become quite large when all possible options should be supported: Coloring (selected, hot, inactive, background), text alignment, images (check boxes, icons) and style (classic or themed).

For a simple implementation perform these steps:

  • Get the cell rect
  • Clear cell by calling FillSolidRect() with background color
  • Subtract cell text spacing of 6 pixels from left and right
  • Draw the text using format flags DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER

Because you want text with multiple colors, you need to call DrawText() multiple times and can't use the alignment and end ellipsis flags but must simulate these formats too.
 
Share this answer
 
Comments
nv3 31-May-13 3:07am    
Good answer, Jochen. +5
Member 10010073 1-Jun-13 9:47am    
Please share the sample source for solution2
Jochen Arndt 3-Jun-13 2:54am    
I'm sorry, I have no public working sample. I have written code for custom drawn list cells once but are not allowed to share this code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900