CListBox owner draw control for Displaying different colors

   CListBox in MFC provides functions and data members to manipulate a list box control. This class can be over-ridden/manipulated in order to change the behavior of the list box to our needs.

The project in this article illustrates an owner-draw list box that is used to list the colors and drawing text on the text based on the selected color. The class uses an abstract object to draw the items. In this case a class derived from the abstract object is created to make the control list colors, but making you own classes that implement you ideas for owner-draw lists, derived from it should be easy.

   The abstract class in this article contains methods that are called from the CListBox derived class. They are 

virtual void DrawItem(LPDRAWITEMSTRUCT lpdis, bool bHasFocus) = 0;

   Actually draws the item using the Win32 DRAWITEMSTRUCT. 

virtual void MeasureItem(LPMEASUREITEMSTRUCT lpmis) = 0;

  Sets the item size once or every time depending of the owner-draw style: fixed ot variable.

virtual void InitLBox(CListBox* pLBox) = 0;

   Initializes the control holding the class if it is a CListBox (or derived from it) 

virtual int AddItemLst(CListBox* pLBox, UINT nItem) = 0;

   Adds an item to the control holding the class if it is a CListBox (or derived from it)

 Using the CListBox derived Color Owner Draw Class:

  • This class should be added to the list box via it?s method SetDrawC. It must be added prior to the creation of the control, so better call it in the parent dialog class constructor.

  • The Init() method of the control should be called immediately after it?s creation.

  • Items are added with the AddItem function of the control. This AddItem function of the CListBox class is over-ridden to call the AddItemLst function written inside our class. This automatically calls the SetItemData for setting the values.

  •    List boxes using from this type can be created either dynamically or using the dialog editor and after adding a variable of type CListBox just rename it with CODrawLBox. Don?t forget to call SetDrawC and Init. List boxes must be created with the following styles: LBS_HASSTRINGS, LBS_NOTIFY, LBS_OWNERDRAWFIXED or LBS_OWNERDRAWVARIABLE.

    In this particular example a list box listing colors is implemented. The init method is obsolete for there is no initialization needed for this case. The AddItem implementation inserts a not used string in the control and sets the item data to the color needed: 

virtual int AddItemLst(CListBox* pList, UINT nItem) 
{
           
int n;
           
n = pList ->AddString("un");
           
pList ->SetItemData(n, nItem);
           
return n;
}

   After that in the drawing function the color is received as item data and drawn in a black rectangle like this: 

CPen pen(PS_SOLID, 1, RGB(0,0,0));
CBrush brush;
?.
dc.SelectObject(pen);
dc.SelectObject(brush);
dc.Rectangle(ColRect);
?.
//lpszText is the number
dc.DrawText(lpszText, strlen(lpszText), TextRect, DT_SINGLELINE|DT_VCENTER);

   When the selection in the list box is changed an event is raised. We catch this event to update the sample text with Invalidate. But the function that actually changes the color of the sample text is OnCtlColor. There you can see how to get the currently selected color from the control ie:

m_colorList.GetItemData(m_colorList.GetCurSel())

   Read here to know the MFC General Owner draw issues & how to solve them.

   Download the source files and Project Files here.

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > c > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction