Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

How to specify individual List item's Font color in VC++ WTL

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.

Upvotes: 1

Views: 1701

Answers (2)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

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.

Upvotes: 0

jiten
jiten

Reputation: 5264

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) 
{

}

Upvotes: 2

Related Questions