I have list box CListBox
in which i need to specify the color of individual list item according to some condition. How can i achieve this.
Im running VS2005.
The application is a WTL Dialog based app.
Asked
Active
Viewed 1,600 times
1

Rohit Vipin Mathews
- 11,629
- 15
- 57
- 112
-
1You'll need to make it owner drawn I think. – Mark Ransom Oct 09 '12 at 12:06
-
Could you explain with an example or a link? Im new to WTL. – Rohit Vipin Mathews Oct 09 '12 at 12:07
2 Answers
2
You can create your own listbox(Ex:CColorListBox)
ColorListBox.h
class CColorListBox : public CListBox
{
// Construction
public:
CColorListBox();
// Attributes
public:
// Operations
public:
int AddString( LPCTSTR lpszItem, COLORREF rgb);
int InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CColorListBox)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CColorListBox();
// Generated message map functions
protected:
//{{AFX_MSG(CColorListBox)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
ColorListBox.cpp
Here is an idea not exact code............
int CColorListBox::AddString( LPCTSTR lpszItem,COLORREF rgb )
{
int item = AddString(lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;
}
int CColorListBox::InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
int item = ((CListBox*)this)->InsertString(nIndex,lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;
}
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
{
}

jiten
- 5,128
- 4
- 44
- 73
-
I got the idea. But what i need is the implementation. Nothing seems to be working with WTL. **So i need an exact code to set list item color in ListBox in WTL.** – Rohit Vipin Mathews Oct 10 '12 at 04:18
0
This is what i did to implement the same functionality in ListViewCtrl
.
I wrote a class to extend the CListViewCtrl
.
class CListViewCtrlEx: public CWindowImpl<CListViewCtrlEx, CListViewCtrl>, public CCustomDraw<CListViewCtrlEx>
{
public:
BEGIN_MSG_MAP(CListViewCtrlEx)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
CHAIN_MSG_MAP_ALT(CCustomDraw<CListViewCtrlEx>, 1)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL bHandled);
DWORD OnPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw);
DWORD OnItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw);
void ForceMeasureItemMessage();
void DeleteItem(LPDELETEITEMSTRUCT /*lpDeleteItemStruct*/);
BOOL DeleteItem(int nItem);
void GetCellRect(int header_column, const CRect& item_rect, CRect& cell_rect);
};
The complete code is HERE.

Rohit Vipin Mathews
- 11,629
- 15
- 57
- 112