CListCtrl sets the font style to bold

I want to change the font of any cell of the CListCtrl control to bold. Can anyone tell me how to do this for CList Ctrl.

I already did it for CTreeCtrl like this

pTC->SetItemState(hItemCur, TVIS_BOLD, TVIS_BOLD);

      

Do we have something similar for CListCtrl?

Thanks in advance.

+3


source to share


2 answers


If you can use CMFCListCtrl (VS2008 SP1 and above), you can extract the class from it and override OnGetCellFont . From there, you return your bold font (you can create your own, or return AFX_GLOBAL_DATA :: fontBold):

HFONT CMyListCtrl::OnGetCellFont( int nRow, int nColumn, DWORD dwData /*= 0*/ )
{
    if (UseBoldFont(/* params */))
    {
        return GetGlobalData()->fontBold;
    }
    return NULL;
}

      



If you need to stick with the plain old CListCtrl, the easiest way would be to use Custom Draw, where you can customize the drawing process to your own needs. Don't confuse it with Owner Draw, where you have to make all the drawings yourself.

Here's an article explaining the basics of using custom Draw with CListCtrl.

+2


source


Add to

ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdraw)

      

to the message card.



Now you can just change the elements as you like. Here you can change alignment, font, background color, text color, [...] and you can set the elements to bold -> example . The best way IMO is to either store a pointer to a struct, a class, or just a flag in the LPARAM of the control's element (s). This function works for both CListCtrl and CTreeCtrl.
Here's an example with flags:

enum ColorFlags
{
    F_COLOR_BLACK = 0x1,
    F_COLOR_WHITE = 0x2
    //and more...
};

enum CustomColors
{
    COLOR_BLACK = RGB(0, 0, 0),
    COLOR_WHITE = RGB(255, 255, 255)
};

afx_msg
void CMyListCtrl::OnCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVCUSTOMDRAW *pDraw = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
    switch (pDraw->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT:
            *pResult = CDRF_NOTIFYITEMDRAW; //Do not forget this...
            break;
        case CDDS_ITEMPREPAINT:
        {
            switch (pDraw->nmcd.lItemlParam) //Extract color from flags
            {
                case F_COLOR_BLACK:
                {
                    pDraw->clrText = COLOR_BLACK;
                } break;
                case F_COLOR_WHITE:
                {
                    pDraw->clrText = COLOR_WHITE;
                } break;
                default:
                    break;
            } //switch
        } break;
    } //switch
}

      

+1


source







All Articles