MFC: How to change color/boldness of inidividual rows of ListCtrl?
Asked Answered
A

3

6

Using MFC and Visual Studio 2010 C++. I need a way to make certain individual rows of a CListCtrl stand out (however I do not want to use the built-in selection capability to highlight the rows). It could be the color of the row background, or font weight, or possibly even an image (if that is performant).

Ideally I want to know how to do this using the stock list control. However, if this is not possible then let me know of a way using 3rd party code.

UPDATE

Here's the code I ended up using:

void MyList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVCUSTOMDRAW* cd = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

    *pResult = CDRF_DODEFAULT;

    switch( cd->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT:
            *pResult = CDRF_NOTIFYITEMDRAW;
            break;

        case CDDS_ITEMPREPAINT:
            {
                int rowNumber = cd->nmcd.dwItemSpec;
                bool highlightRow = (bool)GetItemData(rowNumber);
                if (highlightRow)
                {
                    COLORREF backgroundColor;
                    backgroundColor = RGB(255, 0, 0);
                    cd->clrTextBk = backgroundColor;
                }
            }
            break;

        default:
            break;
    }
}

In my case, I wasn't using the ItemData for anything, so I called SetItemData elsewhere with a boolean value to indicate whether the row should be highlighted.

Arevalo answered 11/1, 2012 at 16:14 Comment(2)
That's indeed exactly what I head in mind ;-) Regarding GetItemData(rowNumber), don't you have it available in cd->nmcd.lItemlParamKristakristal
@Serge-appTranslator: ah good point, would save a call. I'll have to try that.Arevalo
K
7

The key message here is the NM_CUSTOMDRAW message sent to your CListCtrl (and some other controls). It allows you to tell Windows that you want to custom draw some part of the CListCtrl. The idea is that the message allows you tell which part of the control should be custom drawn. Because custom drawing the whole CListCtrl only to change the text color of a cell would be totally overkill.

Don't worry, you don't have to handle custom draw yourself: The message allows to set the font and/or text/back color for one specific row or cell of the control.

This codeproject article is probably a good starting point.

Here is a shorter code example[archived] to set the color of a specific line in your CListCtrl.

Kristakristal answered 12/1, 2012 at 12:31 Comment(0)
R
-2

You can use the following code to alter the whole list backgroud colour but I am not sure there is supported functionality to change the colour per row. Following is the code:

YourControl.SetBkColor(RGB(212,208,200));

I hope it helps.

Rodney answered 12/1, 2012 at 12:4 Comment(2)
Everything is Possible.Onomastic
Yes, I believe now.Ass
A
-3

Here is what i did to my program, if the row i want highlighted is a cin or a cout or any other just put this code above the that row

SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);

the last part i.e, 0x0F lets u change the colour code of the background and text

after changing that to your desired colour, just insert another below the row you want highlighted, i.e;

 SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x0C);

here is the table of colours and their codes

    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White

EXAMPLE for a black background and blue text;

SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x01);
Allanson answered 26/4, 2013 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.