65.9K
CodeProject is changing. Read more.
Home

Owner Draw List Box with Icons and Text

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (8 votes)

Sep 11, 2002

viewsIcon

140943

downloadIcon

5433

Easy to implementation Owner Draw List Box

Sample Image - _ListBoxCH.jpg

Introduction

The article presents an implementation of an owner drawn ListBox with the icon and text.

Before programming, you can use resource editor to create a list box template which properties must be set. Select Variable for owner draw and check Want key to input, then uncheck Sort and Has strings check box. Import your favorite icon to your project if you want to display in list box (the icon only support 32x32 dimension size image), then add ListBoxCH.h and ListBoxCH.cpp, two files to your project and add following line to the top where you want to use CListBoxCH:

#include "ListBoxCH.h"

Then add following statements to implement owner draw list box:

// attach dialog template to the CWnd object
m_ListBox.SubclassDlgItem(IDC_ICON_LIST,this);
// add item 
m_ListBox.AddItem("MFC",AfxGetApp()->LoadIcon(IDR_MAINFRAME));

Initial owner draw list box:

m_ListBox.SetCurSel(0);
// set default selected index.

The CListBoxCH has some operations. Member functions can set or change owner draw list box surface:

class CListBoxCH: public CListBox
{
    ...
// Operations
public:
    // set active selected color
    void SetSelColor(COLORREF clr);
    // set inactive background (unselected) color 
    void SetBgColor(COLORREF clr);
    // change or set current select index
    void SetCurSel(int curSel);
    // set text color
    void SetTextColor(COLORREF clr);
    //  display separator edge ?  
    void EnableEdge(BOOL bEnable); 
    // get current selected index
    int  GetCurSel(); 
    ...
}

Conclusion

This owner draw list box is very simple. You can add your code to this class or add other message handler to this class.

Happy programming!