2012-10-09 28 views
1

我有列表框CListBox其中我需要根據某些條件指定單個列表項的顏色。我怎樣才能做到這一點。我運行VS2005。 該應用程序是一個基於WTL對話框的應用程序。如何在VC++中指定單個列表項的字體顏色WTL

+1

你需要讓它自己畫出我認爲。 –

+0

你能解釋一個例子或鏈接嗎? Im新到WTL。 –

回答

2

您可以創建自己的列表框(例如: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

這裏是一個想法沒有確切的代碼... .........

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

} 
+0

我明白了。但我需要的是實施。似乎沒有什麼與WTL合作。 **所以我需要一個精確的代碼來設置WTL列表框中的列表項顏色。** –

0

這是我在ListViewCtrl中實現相同的功能。

我寫了一個類來擴展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); 
}; 

完整的代碼是HERE

相關問題