Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / MFC
Article

CListBoxST, a CListBox derived control

Rate me:
Please Sign up or sign in to vote.
4.81/5 (24 votes)
21 Mar 20022 min read 241.7K   8.6K   93   35
A CListBox derived class that handles icons and disabled items

Sample Image

Abstract

CListBoxST is a CListBox derived class that tries to reproduce the look and feel of the same control
that appears under Windows XP when a CD without autorun.inf is inserted into the CD-Rom drive.
Each list box item can be enabled or disabled, can have a icon on the left and its text can be multi-line.
Even if almost the same visual effects can be obtained using a standard CListCtrl control, there are
some differences that make CListBoxST better:

  • Easy to use
  • Standard CListBox methods
  • Disabled items
  • Multi-line text
  • Methods to move items up, down, at top, at bottom
  • Different hilight styles

How to integrate CListBoxST in your application

In your project include the following files:

  • ListBoxST.h
  • ListBoxST.cpp

With the dialog editor create a standard list box called, for example, IDC_LBXITEMS. You need to set the following properties:


The Notify property is not mandatory but it will be required to catch messages from the list box, like for example, the Double-click event.

Then create a member variable for this list box:

CListBoxST m_lbxItems;

// Create a image list to hold icons
CImageList m_ImageList;

Now attach the list box to CListBoxST. For dialog-based applications, in your OnInitDialog:

// Call the base-class method
CDialog::OnInitDialog();

// Create the IDC_LBXITEMS list box
m_lbxItems.SubclassDlgItem(IDC_LBXITEMS, this);
Or in your DoDataExchange:
// Call the base method
CDialog::DoDataExchange(pDX);

// Create the IDC_LBXITEMS list box
DDX_Control(pDX, IDC_LBXITEMS, m_lbxItems);

To support icons, a image list must be associated to the control:

BOOL   bRetValue = FALSE;
HICON  hIcon = NULL;

// Create image list
bRetValue = m_ImageList.Create(32, 32, ILC_COLOR32 | ILC_MASK, 5, 1);
ASSERT(bRetValue == TRUE);

// Add some icons
hIcon = AfxGetApp()->LoadIcon(IDI_SHELL32_203);
m_ImageList.Add(hIcon);
hIcon = AfxGetApp()->LoadIcon(IDI_SHELL32_141);
m_ImageList.Add(hIcon);

// Associate image list to list box
m_lbxItems.SetImageList(&m_ImageList);

// At this point the list box is ready to be used. Add some items
m_lbxItems.AddString(_T("First item"), 0);
m_lbxItems.AddString(_T("Second item\nThis is multi-line"), 1);

Class methods

AddString

Adds a string to the list box.

// Parameters:
//     [IN]   lpszItem
//            Points to the null-terminated string that is to be added.
//     [IN]   nImage
//            Image to be associated with the string.
//            Pass -1L to associate no image.
//
// Return value:
//     The zero-based index of the string in the list box.
//     The return value is LB_ERR if an error occurs; the return value 
//     is LB_ERRSPACE if insufficient space is available to store the new string.
//
int AddString(LPCTSTR lpszItem, int nImage = -1L)

InsertString

Inserts a string at a specific location in the list box.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the position to insert the string.
//            If this parameter is -1, the string is added to the end of the list.
//     [IN]   lpszItem
//            Pointer to the null-terminated string that is to be inserted.
//     [IN]   nImage
//            Image to be associated with the string.
//            Pass -1L to associate no image.
//
// Return value:
//     The zero-based index of the position at which the string was inserted.
//     The return value is LB_ERR if an error occurs; the return value 
//     is LB_ERRSPACE if insufficient space is available to store the new string.
//
int InsertString(int nIndex, LPCTSTR lpszString, int nImage = -1L)

DeleteString

Deletes a string from the list box.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the string to be deleted.
//
// Return value:
//     A count of the strings remaining in the list box.
//     The return value is LB_ERR if nIndex specifies an index greater than 
//     the number of items in the list.
//
int DeleteString(int nIndex)

ReplaceString

Replaces a string at a specific location in the list box.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the position to replace the string.
//     [IN]   lpszItem
//            Pointer to the null-terminated string that is to be replaced.
//     [IN]   nImage
//            Image to be associated with the string.
//            Pass -1L to associate no image.
//
// Return value:
//     The zero-based index of the position at which the string was replaced.
//     The return value is LB_ERR if an error occurs; the return value 
//     is LB_ERRSPACE if insufficient space is available to store the new string.
//
int ReplaceString(int nIndex, LPCTSTR lpszString, int nImage = -1L)

ResetContent

Clears all the entries from the list box.

void ResetContent()

SetImageList

Sets the image list to use in the list box.

// Parameters:
//     [IN]   pImageList
//            Pointer to an CImageList object containing the image list
//            to use in the list box. Pass NULL to remove any previous
//            associated image list.
//
void SetImageList(CImageList* pImageList)

SetImage

Sets the image to use in a list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the string.
//     [IN]   nImage
//            Specifies the zero-based index of the image
//            inside the imagelist to use.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetImage(int nIndex, int nImage, BOOL bRepaint = TRUE)

GetImage

Returns the image index associated to a list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the string.
//     [OUT]  lpnImage
//            Pointer to a int variable that will receive the index
//            of the image inside the imagelist.
//            This variable will be set to -1L if no image is associated.
//
void GetImage(int nIndex, LPINT lpnImage)

SetItemData

Sets the 32-bit value associated with the list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   dwItemData
//            Specifies the value to be associated with the item.
//
// Return value:
//     LB_ERR if an error occurs.
//
int SetItemData(int nIndex, DWORD dwItemData)

GetItemData

Returns the 32-bit value associated with the list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//
// Return value:
//     The 32-bit value associated with the item, or LB_ERR if an error occurs.
//
DWORD GetItemData(int nIndex)

SetItemDataPtr

Sets a pointer to a list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   pData
//            Specifies the pointer to be associated with the item.
//
// Return value:
//     LB_ERR if an error occurs.
//
int SetItemDataPtr(int nIndex, void* pData)

GetItemDataPtr

Returns a pointer of a list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//
// Return value:
//     Pointer associated with the item, or -1 if an error occurs.
//
void* GetItemDataPtr(int nIndex)

MoveUp

Moves a list box item up by one position.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   bSetCurSel
//            If TRUE the item will be highlighted
//
// Return value:
//      The zero-based index of the position at which the string was moved.
//      The return value is LB_ERR if an error occurs; the return value 
//      is LB_ERRSPACE if insufficient space is available to store the string.
//
int MoveUp(int nIndex, BOOL bSetCurSel = TRUE)

MoveDown

Moves a list box item down by one position.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   bSetCurSel
//            If TRUE the item will be highlighted
//
// Return value:
//      The zero-based index of the position at which the string was moved.
//      The return value is LB_ERR if an error occurs; the return value 
//      is LB_ERRSPACE if insufficient space is available to store the string.
//
int MoveDown(int nIndex, BOOL bSetCurSel = TRUE)

MoveTop

Moves a list box item to the top most position.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   bSetCurSel
//            If TRUE the item will be highlighted
//
// Return value:
//      The zero-based index of the position at which the string was moved.
//      The return value is LB_ERR if an error occurs; the return value 
//      is LB_ERRSPACE if insufficient space is available to store the string.
//
int MoveTop(int nIndex, BOOL bSetCurSel = TRUE)

MoveBottom

Moves a list box item to the bottom most position.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   bSetCurSel
//            If TRUE the item will be highlighted
//
// Return value:
//      The zero-based index of the position at which the string was moved.
//      The return value is LB_ERR if an error occurs; the return value 
//      is LB_ERRSPACE if insufficient space is available to store the string.
//
int MoveBottom(int nIndex, BOOL bSetCurSel = TRUE)

EnableItem

Enables or disables a list box item.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [IN]   bEnable
//            Specifies whether the given item is to be enabled or disabled.
//            If this parameter is TRUE, the item will be enabled.
//            If this parameter is FALSE, the item will be disabled.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void EnableItem(int nIndex, BOOL bEnable = TRUE, BOOL bRepaint = TRUE)

IsItemEnabled

Specifies whether a list box item is enabled.

// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//
// Return value:
//      TRUE if the item is enabled; otherwise FALSE.
//
BOOL IsItemEnabled(int nIndex)

SetRowSelect

Sets how a selected list box item will be highlighted.

// Parameters:
//     [IN]   byRowSelect
//            Selection type. Can be one of the following values:
//            ST_FULLROWSELECT   Hilight full list box item (Default)
//            ST_FULLTEXTSELECT  Hilight half list box item (Part containing text)
//            ST_TEXTSELECT      Hilight only list box text
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
void SetRowSelect(BYTE byRowSelect = ST_FULLROWSELECT, BOOL bRepaint = TRUE)

GetVersionI

Returns the class version as a short value.

// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()

GetVersionC

Returns the class version as a string value.

// Return value:
//     Pointer to a null-terminated string containig the class version.
//
static LPCTSTR GetVersionC()

History

  • v1.0 (13/March/2002) First release

Disclaimer

The software and the accompanying files are distributed "as is" and without any warranties whether expressed or implied. No responsibilities for possible damages or even functionality can be taken. The user must assume the entire risk of using this software.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSmall bug when trying to get item on mouse point Pin
mrsilver28-Nov-15 12:26
mrsilver28-Nov-15 12:26 
GeneralMy vote of 5 Pin
Franc Morales30-Apr-13 20:28
Franc Morales30-Apr-13 20:28 
GeneralLicenses Pin
clintM22-Apr-09 7:10
clintM22-Apr-09 7:10 
AnswerChanges needed for VS2k5 Pin
Sam_Gong4-Jul-08 17:57
Sam_Gong4-Jul-08 17:57 
NewsBetter rendering for 32 bit Disabled/Focused images Pin
Eli Burke14-Sep-06 10:19
Eli Burke14-Sep-06 10:19 
GeneralImpossible to add strings to a CListBox item in a child dialog Pin
Morten Dramstad19-Feb-06 5:58
Morten Dramstad19-Feb-06 5:58 
GeneralDoesn't work on VC 7.0 Pin
mohan_v989-Feb-05 11:30
sussmohan_v989-Feb-05 11:30 
GeneralMore MeasureItem fixes Pin
pixelgrease31-Aug-04 16:35
pixelgrease31-Aug-04 16:35 
GeneralRe: More MeasureItem fixes Pin
Hesham Desouky21-Nov-06 2:28
Hesham Desouky21-Nov-06 2:28 
QuestionHow to change background color? Pin
Oitz6-Aug-04 14:00
Oitz6-Aug-04 14:00 
AnswerRe: How to change background color? Pin
Cintia29-Jan-07 4:40
Cintia29-Jan-07 4:40 
GeneralHeight Problem Pin
itaifrenkel25-May-04 1:09
itaifrenkel25-May-04 1:09 
GeneralRe: Height Problem Pin
H.Ouf22-Oct-08 0:28
H.Ouf22-Oct-08 0:28 
QuestionHow do I change the font? Pin
hoobyjuice17-May-04 0:46
hoobyjuice17-May-04 0:46 
GeneralMulticolumn Pin
Angelo Santangelo28-Oct-03 23:19
Angelo Santangelo28-Oct-03 23:19 
GeneralResource leak! Pin
jack tian7-Aug-03 21:53
jack tian7-Aug-03 21:53 
GeneralRe: Resource leak! Pin
QuiJohn7-Dec-05 10:13
QuiJohn7-Dec-05 10:13 
GeneralList Control with Cust and Paste Pin
tools200027-May-03 6:32
tools200027-May-03 6:32 
GeneralIncorrect LBS_NOSEL style handling Pin
Ryszard Krakowiak12-Apr-03 22:05
Ryszard Krakowiak12-Apr-03 22:05 
QuestionCListBoxST under PocketPC? Pin
Daniel Strigl5-Nov-02 9:34
Daniel Strigl5-Nov-02 9:34 
GeneralI wonder if someone could help with a problem I'm having with this Pin
Jason Troitsky (was Hattingh)29-Oct-02 6:03
Jason Troitsky (was Hattingh)29-Oct-02 6:03 
GeneralBug in common controls6 which affects your control Pin
Christian Skovdal Andersen28-Aug-02 5:44
Christian Skovdal Andersen28-Aug-02 5:44 
GeneralRe: Bug in common controls6 which affects your control Pin
Davide Calabro28-Aug-02 6:19
Davide Calabro28-Aug-02 6:19 
GeneralMousewheel scrolling goes opposite direction! Pin
Anonymous24-Aug-02 16:15
Anonymous24-Aug-02 16:15 
GeneralRe: Mousewheel scrolling goes opposite direction! Pin
dw44182-Sep-09 17:40
dw44182-Sep-09 17:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.