mfcclistbox

Setting individual items in a CListBox as bold with MFC


I stumbled over this article:

http://asg.unige.ch/Past/fuentes/Mfc/HowTo_44.html

So, I reproduced the class in my project:

// FontStyleListBox.cpp : implementation file
//

#include "stdafx.h"
#include "Meeting Schedule Assistant.h"
#include "FontStyleListBox.h"


// CFontStyleListBox

IMPLEMENT_DYNAMIC(CFontStyleListBox, CListBox)

CFontStyleListBox::CFontStyleListBox()
{

}

CFontStyleListBox::~CFontStyleListBox()
{
}


BEGIN_MESSAGE_MAP(CFontStyleListBox, CListBox)
    ON_WM_DRAWITEM_REFLECT()
END_MESSAGE_MAP()



// CFontStyleListBox message handlers




void CFontStyleListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rect;

    // Draw the colored rectangle portion
    rect.CopyRect(&lpDrawItemStruct->rcItem);

    pDC->SetBkMode(TRANSPARENT);

    if (lpDrawItemStruct->itemState & ODS_SELECTED)
    {
        pDC->FillSolidRect(rect, GetSysColor(COLOR_HIGHLIGHT));
        pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
    }
    else
    {
        pDC->FillSolidRect(rect, GetSysColor(COLOR_WINDOW));
        pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
    }

    if ((int)(lpDrawItemStruct->itemID) >= 0) // Valid ID
    {
        CString sText;
        int     nIndex;

        CFont newFont;
        CFont *pOldFont;

        nIndex = lpDrawItemStruct->itemID;
        GetText(nIndex, sText);

        FONTSTYLE fontStyle = (FONTSTYLE)GetItemData(nIndex);

        // To avoid unnecessary processing
        if (fontStyle == NORMAL) {
            pDC->DrawText(sText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
            return;
        }

        LOGFONT logFont;
        CFont *pFont = GetFont();
        pFont->GetLogFont(&logFont);

        switch (fontStyle)
        {
            //case NORMAL: logFont.lfWeight = FW_NORMAL;
            //           break;
        case BOLD:   logFont.lfWeight = FW_BOLD;
            break;
        case ITALIC: logFont.lfItalic = TRUE;
            break;
        }

        newFont.CreatePointFontIndirect(&logFont);
        pOldFont = pDC->SelectObject(&newFont);
        pDC->DrawText(sText, rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
        pDC->SelectObject(pOldFont);
        newFont.DeleteObject();
    }
}


int CFontStyleListBox::GetFontStyle(int nIndex)
{
    return (FONTSTYLE)GetItemData(nIndex);
}


void CFontStyleListBox::SetFontStyle(int nIndex, FONTSTYLE fontStyle)
{
    SetItemData(nIndex, (DWORD)fontStyle);
    Invalidate();
}

I then used it in my application. I set the properties correctly for ownerdraw etc. but here is the results:

ListBox Output

The bold entry is the last one. Why is it not rendering correctly?

How do I fix this and / or are there newer ways to get this done?


Solution

  • You just need to CFont::CreateFontIndirect use. Using CFont::CreatePointFontIndirect causes a conversion from the font points into physical points. You don't need that.

    Also create the font only once. You may create it on demand in DrawItem. Just create a member in your subclassed CListBox...