1、声明部分(TFColorListCtrl.h) #if !defined(AFX_TFCOLORLISTCTRL_H__BF926AD6_0143_4E1A_8D27_33D7FEA11D51__INCLUDED_) #define AFX_TFCOLORLISTCTRL_H__BF926AD6_0143_4E1A_8D27_33D7FEA11D51__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // TFColorListCtrl.h : header file // // //* //*功能:可修改指定行列的文本色及背景色的列表控件 //*作者:童方 //*QQ:58408454 //*时间:2010.3.24 //* // / // TFColorListCtrl window class TFColorListCtrl : public CListCtrl { // Construction public: TFColorListCtrl(); // Attributes public: // Operations public: //*重载Item相关的方法 int InsertItem(const LVITEM* pItem); int InsertItem(int nItem, LPCTSTR lpszItem); int InsertItem(int nItem, LPCTSTR lpszItem, int nImage); int InsertItem(UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam); BOOL DeleteItem(int nItem); BOOL DeleteAllItems(); //*重载ItemData相关方法 BOOL SetItemData(int nItem, DWORD dwData); DWORD GetItemData(int nItem) const; //*新加入的方法,设置颜色以及获取颜色 void SetCellBackColor(int iRow, int iCol, COLORREF Color); void SetCellForeColor(int iRow, int iCol, COLORREF Color); COLORREF GetCellBackColor(int iRow, int iCol); COLORREF GetCellForeColor(int iRow, int iCol); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(TFColorListCtrl) protected: virtual void PreSubclassWindow(); //}}AFX_VIRTUAL // Implementation public: virtual ~TFColorListCtrl(); // Generated message map functions protected: //{{AFX_MSG(TFColorListCtrl) afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult); afx_msg void OnDestroy(); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: CPtrArray m_ptrListItemData; }; / //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_TFCOLORLISTCTRL_H__BF926AD6_0143_4E1A_8D27_33D7FEA11D51__INCLUDED_) 2、实现文件(TFColorListCtrl.cpp) // TFColorListCtrl.cpp : implementation file // #include "stdafx.h" #include "TFColorListCtrl.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // //* //*功能:可修改指定行列的文本色及背景色的列表控件 //*作者:童方 //*QQ:58408454 //*时间:2010.3.24 //* // // //*思路:将背景色信息绑定到对应的ListItemData //*自订义列表项数据结构 struct TFColorListItemData { CPtrArray BackColor; //*背景色列表 CPtrArray TextColor; //*前景色列表 DWORD ItemData; //*列表项数据(由于当前列表项占用了默认的ItemData,因此留出此空间保留原控件功能) }; //*释放列表项结构 void ReleaseTFColorListItemData(TFColorListItemData* p) { COLORREF *pColor; int i; //*释放背景色列表 for(i=0; i<p->BackColor.GetSize(); i++) { pColor = (COLORREF*)p->BackColor.GetAt(i); if(pColor) delete pColor; } //*释放文本色列表 for(i=0; i<p->TextColor.GetSize(); i++) { pColor = (COLORREF*)p->TextColor.GetAt(i); if(pColor) delete pColor; } } //*设置列表项文本颜色 void SetMyTextColor(TFColorListItemData* p, int nIndex, COLORREF Color) { if(nIndex < 0) return; //*如果需要设置的坐标无对应的内存,则为其分配内存 while(p->TextColor.GetSize() <= nIndex) { COLORREF* pColor = new COLORREF; *pColor = RGB(0,0,0); p->TextColor.Add(pColor); } //*得到颜色指针 COLORREF* pClr = (COLORREF*)p->TextColor.GetAt(nIndex); //*设置颜色 *pClr = Color; } //*设置列表项背景颜色 void SetMyBackColor(TFColorListItemData* p, int nIndex, COLORREF Color) { if(nIndex < 0) return; //*如果需要设置的坐标无对应的内存,则为其分配内存 while(p->BackColor.GetSize() <= nIndex) { COLORREF* pColor = new COLORREF; *pColor = RGB(255,255,255); p->BackColor.Add(pColor); } //*得到颜色指针 COLORREF* pClr = (COLORREF*)p->BackColor.GetAt(nIndex); //*设置颜色 *pClr = Color; } //*获取列表项文本颜色 COLORREF GetMyTextColor(TFColorListItemData* p, int nIndex) { if(nIndex < 0) return RGB(0,0,0); if(nIndex >= p->TextColor.GetSize()) return RGB(0,0,0); COLORREF *pClr = (COLORREF*)p->TextColor.GetAt(nIndex); return (*pClr); } //*获取列表项背景颜色 COLORREF GetMyBackColor(TFColorListItemData* p, int nIndex) { if(nIndex < 0) return RGB(255,255,255); if(nIndex >= p->BackColor.GetSize()) return RGB(255,255,255); COLORREF *pClr = (COLORREF*)p->BackColor.GetAt(nIndex); return (*pClr); } / // TFColorListCtrl TFColorListCtrl::TFColorListCtrl() { } TFColorListCtrl::~TFColorListCtrl() { } void TFColorListCtrl::PreSubclassWindow() { // TODO: Add your specialized code here and/or call the base class CListCtrl::PreSubclassWindow(); } BEGIN_MESSAGE_MAP(TFColorListCtrl, CListCtrl) //{{AFX_MSG_MAP(TFColorListCtrl) ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() / // TFColorListCtrl message handlers int TFColorListCtrl::InsertItem(const LVITEM* pItem) { //*插入行,并得到行索引 int nRet = CListCtrl::InsertItem(pItem); //*分配自订义数据 TFColorListItemData* p = new TFColorListItemData(); //*设置ItemData p->ItemData = NULL; //*将自定义数据绑定到行 CListCtrl::SetItemData(nRet, (DWORD)p); return nRet; } int TFColorListCtrl::InsertItem(int nItem, LPCTSTR lpszItem) { //*插入行,并得到行索引 int nRet = CListCtrl::InsertItem(nItem, lpszItem); //*分配自订义数据 TFColorListItemData* p = new TFColorListItemData(); //*设置ItemData p->ItemData = NULL; //*将自定义数据绑定到行 CListCtrl::SetItemData(nRet, (DWORD)p); return nRet; } int TFColorListCtrl::InsertItem(int nItem, LPCTSTR lpszItem, int nImage) { //*插入行,并得到行索引 int nRet = CListCtrl::InsertItem(nItem, lpszItem, nImage); //*分配自订义数据 TFColorListItemData* p = new TFColorListItemData(); //*设置ItemData p->ItemData = NULL; //*将自定义数据绑定到行 CListCtrl::SetItemData(nRet, (DWORD)p); return nRet; } int TFColorListCtrl::InsertItem(UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam) { //*插入行,并得到行索引 int nRet = CListCtrl::InsertItem(nMask, nItem, lpszItem, nState, nStateMask, nImage, lParam); //*分配自订义数据 TFColorListItemData* p = new TFColorListItemData(); //*设置ItemData p->ItemData = NULL; //*将自定义数据绑定到行 CListCtrl::SetItemData(nRet, (DWORD)p); return nRet; } BOOL TFColorListCtrl::DeleteItem(int nItem) { //*获取该行自定义颜色信息 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(nItem); if(p) { //*释放空间 ReleaseTFColorListItemData(p); delete p; } //*调用基类的对应函数 return CListCtrl::DeleteItem(nItem); } BOOL TFColorListCtrl::DeleteAllItems() { //*释放空间 for(int i=0; i<GetItemCount(); i++) { TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(i); if(p) { ReleaseTFColorListItemData(p); delete p; } } //*调用基类的对应函数 return CListCtrl::DeleteAllItems(); } BOOL TFColorListCtrl::SetItemData(int nItem, DWORD dwData) { //*得到自订义结构 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(nItem); if(p) { //*将用户数据绑定到自定义结构的ItemData p->ItemData = dwData; return TRUE; } return FALSE; } DWORD TFColorListCtrl::GetItemData(int nItem) const { //*得到自订义结构 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(nItem); if(p) { //*返回自定义结构中的ItemData return p->ItemData; } return 0; } void TFColorListCtrl::SetCellBackColor(int iRow, int iCol, COLORREF Color) { //*如果索引越界,则直接返回 if(iRow < 0) return; if(iRow >= GetItemCount()) return; if(iCol < 0) return; if(iCol >= GetHeaderCtrl()->GetItemCount()) return; //*得到自定义颜色结构 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(iRow); if(p) { //*设置背景色 SetMyBackColor(p, iCol, Color); Invalidate(); } } void TFColorListCtrl::SetCellForeColor(int iRow, int iCol, COLORREF Color) { //*如果索引越界,则直接返回 if(iRow < 0) return; if(iRow >= GetItemCount()) return; if(iCol < 0) return; if(iCol >= GetHeaderCtrl()->GetItemCount()) return; //*得到自定义颜色结构 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(iRow); if(p) { //*设置文本色 SetMyTextColor(p, iCol, Color); Invalidate(); } } COLORREF TFColorListCtrl::GetCellBackColor(int iRow, int iCol) { //*如果索引越界,则直接返回 if(iRow < 0) return RGB(255,255,255); if(iRow >= GetItemCount()) return RGB(255,255,255); if(iCol < 0) return RGB(255,255,255); if(iCol >= GetHeaderCtrl()->GetItemCount()) return RGB(255,255,255); //*得到自定义颜色结构 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(iRow); if(p) { //*返回背景色 return GetMyBackColor(p, iCol); } //*如果无法得到自定义结构,则返回默认值 return RGB(255,255,255); } COLORREF TFColorListCtrl::GetCellForeColor(int iRow, int iCol) { //*如果索引越界,则直接返回 if(iRow < 0) return RGB(0,0,0); if(iRow >= GetItemCount()) return RGB(0,0,0); if(iCol < 0) return RGB(0,0,0); if(iCol >= GetHeaderCtrl()->GetItemCount()) return RGB(0,0,0); //*得到自定义颜色结构 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(iRow); if(p) { //*返回文本色 return GetMyTextColor(p, iCol); } //*如果无法得到自定义结构,则返回默认值 return RGB(0,0,0); } void TFColorListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) { NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR); //*调用默认处理 *pResult = CDRF_DODEFAULT; //*检查控件重绘消息 if(pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT) { *pResult = CDRF_NOTIFYITEMDRAW; } //*检查子项重绘消息 else if(pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) { *pResult = CDRF_NOTIFYSUBITEMDRAW; } //*检查子项中的列重绘消息 else if(pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) { //*得到该单元格信息 TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(pLVCD->nmcd.dwItemSpec); if(p) { //*设置背景色 pLVCD->clrTextBk = GetMyBackColor(p, pLVCD->iSubItem); //*设置前景色 pLVCD->clrText = GetMyTextColor(p, pLVCD->iSubItem); } else { pLVCD->clrTextBk = RGB(255,255,255); pLVCD->clrText = RGB(0,0,0); } *pResult = CDRF_DODEFAULT; } } void TFColorListCtrl::OnDestroy() { //*控件释放时,释放自定义结构资源 for(int i=0; i<GetItemCount(); i++) { TFColorListItemData* p = (TFColorListItemData*)CListCtrl::GetItemData(i); if(p) { ReleaseTFColorListItemData(p); delete p; } } CListCtrl::OnDestroy(); } 3、使用方法 m_list.SetExtendedStyle(m_list.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_REPORT); m_list.InsertColumn(0, "aaa", LVCFMT_LEFT, 50); m_list.InsertColumn(1, "bbb", LVCFMT_LEFT, 50); m_list.InsertColumn(2, "ccc", LVCFMT_LEFT, 50); m_list.InsertColumn(3, "ddd", LVCFMT_LEFT, 50); m_list.InsertColumn(4, "eee", LVCFMT_LEFT, 50); m_list.InsertItem(0, "a1"); m_list.InsertItem(1, "a2"); m_list.InsertItem(2, "a3"); m_list.InsertItem(3, "a4"); m_list.SetItemText(0,1,"b1"); m_list.SetItemText(1,1,"b2"); m_list.SetItemText(2,1,"b3"); m_list.SetItemText(3,1,"b4"); m_list.SetItemText(0,2,"c1"); m_list.SetItemText(1,2,"c2"); m_list.SetItemText(2,2,"c3"); m_list.SetItemText(3,2,"c4"); m_list.SetItemText(0,3,"d1"); m_list.SetItemText(1,3,"d2"); m_list.SetItemText(2,3,"d3"); m_list.SetItemText(3,3,"d4"); m_list.SetItemText(0,4,"e1"); m_list.SetItemText(1,4,"e2"); m_list.SetItemText(2,4,"e3"); m_list.SetItemText(3,4,"e4"); m_list.SetCellBackColor(0, 3, RGB(240,200,80)); m_list.SetCellBackColor(1, 2, RGB(40,200,80)); m_list.SetCellBackColor(3, 3, RGB(240,20,80)); m_list.SetCellBackColor(2, 2, RGB(50,200,180)); m_list.SetCellForeColor(0, 1, RGB(0,0,255)); 感谢C/C++/VC/MFC/ASM(74315870)群主Koma:)