CListBox Changing Font and Colors

   CListBox class as such does not give support for different fonts or colors. So whenever a programmer needs to use different fonts or colors, he should derive a class from CListBox and override the DrawItem & MeasureItem functions. The property ListBox Properties –> Styles tab –> OwnerDraw should be set as Fixed. The ListBox Properties –> Styles tab –> HasStrings property should be set to true. This article explains how to modify the MeasureItem and DrawItem functions to support different fonts and colors.

    The only important thing to be changed is the “modified font and color should be supplied to the device context before the text is being added/drawn to the CListBox class”. The following DrawItem function supplies the Font changed for color and type into the device context. 

Important Steps – CListBox Changing Font and Colors :

    The sample code Highlighted in red color is the important place to be looked at. The following are the steps to be done for customizing list box to support different fonts and colors.

  • In the newly created MFC Application (either Dialog application or MFC SDI/MDI app), open the classwizard by clicking Menu –> View –>ClassWizard.
  • Click on the Command Button Add Class–>New and give a new name for the CListBox derived class. This sample uses the name as MyListBox.
  • After adding it, add the function DrawItem to the MyListBox class, through the classwizard’s MessageMap tab. If there is any need to change the size of the rows in List Box, then MeasureItem also should be added.
  • The following sample code can be copied and used. The following two lines can be modified to change the font or color for the programmer’s application.

                    font.CreatePointFont(100,”Times New Roman”);
crText = RGB(80,80,200);

DrawItem Sample – CListBox Changing Font and Colors :


void MyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//CRect members to store the position of the items
CRect rItem;
CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);

if ((int)lpDrawItemStruct->itemID < 0)
{
// If there are no elements in the CListBox
// based on whether the list box has Focus  or not
// draw the Focus Rect or Erase it,
if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && (lpDrawItemStruct->itemState & ODS_FOCUS))
{
dc->DrawFocusRect(&lpDrawItemStruct->rcItem);
}
else if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && !(lpDrawItemStruct->itemState & ODS_FOCUS))
{
dc->DrawFocusRect(&lpDrawItemStruct->rcItem);
}
return;
}

// String to store the text
CString strText;
// Get the item text.
GetText(lpDrawItemStruct->itemID, strText);

//Initialize the CListBox Item's row size
rItem = lpDrawItemStruct->rcItem;

UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
if (GetStyle() & LBS_USETABSTOPS)
nFormat |= DT_EXPANDTABS;

// If CListBox item selected, draw the highlight rectangle.
// Or if CListBox item deselected, draw the rectangle using the window color.
if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush br(::GetSysColor(COLOR_HIGHLIGHT));
dc->FillRect(&rItem, &br);
}
else if (!(lpDrawItemStruct->itemState & ODS_SELECTED) && (lpDrawItemStruct->itemAction & ODA_SELECT))
{
CBrush br(::GetSysColor(COLOR_WINDOW));
dc->FillRect(&rItem, &br);
}

// If the CListBox item has focus, draw the focus rect.
// If the item does not have focus, erase the focus rect.
if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && (lpDrawItemStruct->itemState & ODS_FOCUS))
{
dc->DrawFocusRect(&rItem);
}
else if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && !(lpDrawItemStruct->itemState & ODS_FOCUS))
{
dc->DrawFocusRect(&rItem);
}

// To draw the Text in the CListBox set the background mode to Transparent.
int iBkMode = dc->SetBkMode(TRANSPARENT);

COLORREF crText;
CFont font;
font.CreatePointFont(100,"Times New Roman");

crText = RGB(80,80,200);
dc->SetTextColor(crText);
dc->SelectObject(&font);

//Draw the Text
dc->TextOut(rItem.left,rItem.top,strText);
}

The above code can be modified to accommodate many other List box customizations like displaying Displaying Icons and such things.