ListView 是一个用途广泛的 control, 在MFC 里的 listView 很平淡,而C#.net 中则增强了很多,可以在设计时设定各个 cell字体,前景/背景颜色。好在 mfc 下control 的自控制性更强,可以做到 C# listView 能做到的一切,甚至更多。下面一个 listView class 是很早以前从 codeproject.com 下载(不好意思,忘了作者是谁),稍稍作了一点修改,只要在派生class 中重载那些 virtual functions 就可以控制任何一个 cell 的 Paint 行为,做出各种花哨的 listView.
#if !defined(AFX_LISTCTRLWITHCUSTOMDRAW_H__AA70C396_EC02_4103_BD7D_3CCEDE0E1C65__INCLUDED_)
#define AFX_LISTCTRLWITHCUSTOMDRAW_H__AA70C396_EC02_4103_BD7D_3CCEDE0E1C65__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CListCtrlWithCustomDraw : public CListCtrl
{
public:
CListCtrlWithCustomDraw();
void SetGridLines();
void SetFullRowSelect();
virtual ~CListCtrlWithCustomDraw();
protected:
CFont* m_pOldItemFont;
CFont* m_pOldSubItemFont;
virtual bool IsDraw() { return false; }
virtual bool OnDraw(CDC* /*pDC*/) { return false; }
virtual bool IsNotifyItemDraw() { return false; }
virtual bool IsNotifyPostPaint() { return false; }
virtual bool IsPostDraw() { return false; }
virtual bool OnPostDraw(CDC* /*pDC*/) { return false; }
virtual CFont* FontForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; }
virtual COLORREF TextColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
virtual COLORREF BkColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
virtual bool IsItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool OnItemDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool IsNotifySubItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool IsNotifyItemPostPaint(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool OnItemPostDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual COLORREF TextColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
virtual COLORREF BkColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
virtual bool IsSubItemDraw(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool OnSubItemDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool IsNotifySubItemPostPaint(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
virtual bool IsSubItemPostDraw() { return false; }
virtual bool OnSubItemPostDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
protected:
//{ {AFX_MSG(CListCtrlWithCustomDraw)
afx_msg void OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_LISTCTRLWITHCUSTOMDRAW_H__AA70C396_EC02_4103_BD7D_3CCEDE0E1C65__INCLUDED_)