简体   繁体   中英

Change Background color of full column of CListCtrl in MFC

Ive made a CListCtrl in Report View in MFC. I want to color the first column (the full column, not only those cells where an Item is) with a grey background.

How can I do this? Thank

The way you will implement this is color each cell of the first row individually. The code will look something like below, which basically a blue print but It should work (Note:I haven't test this for this post). You will have to use lplvcd->iSubItem and paint the first column of each row.

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;
        case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
            {

                // something like if(lplvcd->iSubItem == 0 ) to paint first column
                lplvcd->clrText = RGB(0,0,255);


                *pResult = CDRF_NEWFONT;
                return;
            }

        default:
            break;
    }
}

Here two perfect articles that describe custom draw in detail.

Part I & Part II

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM