Click here to Skip to main content
Click here to Skip to main content
Go to top

CListBoxColorPickerST v1.1

, 6 Oct 2001
Rate this:
Please Sign up or sign in to vote.
A CListBox derived class to pick-up colors from a pre-defined list

Sample Image

Abstract

CListBoxColorPickerST is a CListBox derived class that shows a colored bar on the left of each item. This control can be useful in such applications like supervisors of an industrial process where each displayed object can have a different color based on its current state (Run, stop, failure...); a color legend can be developed using CListBoxColorPickerST to visually associate each possible color with a text description.

How to integrate CListBoxColorPickerST in your application

In your project include the following files:

  • ListBoxColorPickerST.h
  • ListBoxColorPickerST.cpp
With dialog editor create a standard list box called, for example, IDC_LBXCOLORS. 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:

CListBoxColorPickerST m_lbxColors;
Now attach the list box to CListBoxColorPickerST. For dialog-based applications, in your OnInitDialog:
// Call the base-class method
CDialog::OnInitDialog();

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

// Create the IDC_LBXCOLORS list box
DDX_Control(pDX, IDC_LBXCOLORS, m_lbxColors);

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]   crColor
//            Color to be associated with the string.
//
// 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, COLORREF crColor)
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]   crColor
//            Color to be associated with the string.
//
// 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, COLORREF crColor)
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]   crColor
//            Color to be associated with the string.
//
// 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, COLORREF crColor)
ResetContent
Clears all the entries from the list box.
void ResetContent()
SetColor
Associates a color with a string at a specific location in the list box.
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the string.
//     [IN]   crColor
//            Color to be associated with the string.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     LB_ERR if an error occurs.
//
int SetColor(int nIndex, COLORREF crColor, BOOL bRepaint = TRUE)
GetColor
Returns the color associated with a string at a specific location in the list box.
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the color to be retrieved.
//
// Return value:
//     The color associated with the specified string.
//     The return value is -1 if an error occurs.
//
COLORREF GetColor(int nIndex)
GetSelectedColor
Returns the color associated with the currently selected string in the list box.
// Return value:
//     The color associated with the currently selected string.
//     The return value is -1 if an error occurs or no string is selected.
//
COLORREF GetSelectedColor()
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)
SelectColor
Searches for a particular color in the list box. If found the associated
string is then selected. The search ends at the first occurence of the color.
// Parameters:
//     [IN]   crColor
//            Color to search for.
//
// Return value:
//     The zero-based index of the position at which the color was found.
//     The return value is LB_ERR if an error occurs or the spcified color
//     is not found.
//
int SelectColor(COLORREF crColor)
GetTextAndColor
Returns a list box item and the associated color.
// Parameters:
//     [IN]   nIndex
//            Specifies the zero-based index of the item.
//     [OUT]  lpszBuffer
//            Pointer to the buffer that will receive the string.
//            The buffer must have sufficient space for the string and a 
//            terminating null character. The size of the string can be 
//            determined ahead of time by calling the GetTextLen member function.
//     [OUT]  rString
//            A reference to a CString object.
//     [OUT]  lpcrColor
//            Pointer to a COLORREF variable that will receive the color associated with
//            the specified item.
//
// Return value:
//     The length (in bytes) of the string, excluding the terminating null character. 
//     If nIndex does not specify a valid index, the return value is LB_ERR.
//
int GetTextAndColor(int nIndex, LPTSTR lpszBuffer, COLORREF* lpcrColor)
int GetTextAndColor(int nIndex, CString& rString, COLORREF* lpcrColor)

Credits

CListBoxColorPickerST is based on the control by James R. Twine and Mark Jackson (mark@mjsoft.co.uk).

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

SoftechSoftware homepage
SoftechSoftware Email

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

Share

About the Author

Davide Calabro
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
Generallist items Not visible Pinsusslaiju8-Jul-05 20:49 
GeneralColour List Box in Embedded VC++ Pinmemberfyp20030418-Jan-04 22:00 
GeneralRe: Colour List Box in Embedded VC++ PinmemberJohann Gerell18-Jan-04 22:28 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 7 Oct 2001
Article Copyright 2001 by Davide Calabro
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid