CListBox Changing Font and Colors

CoderSource.net
CListBox Changing Font and Colors
Rating:

Date: 5/1/2004 12:00:00 AM

   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.
 

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us