//ListCtrlWithCustomDraw.h #pragma once class CListCtrlWithCustomDraw : public CListCtrl { // Construction public: CListCtrlWithCustomDraw();
// Attributes public:
// Operations public:
// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CListCtrlWithCustomDraw) //}}AFX_VIRTUAL
// Implementation public: virtual ~CListCtrlWithCustomDraw();protected: CFont* m_pOldItemFont; CFont* m_pOldSubItemFont; // // Callbacks for whole control //
// do we want to do the drawing ourselves? virtual bool IsDraw() { return false; } // if we are doing the drawing ourselves // override and put the code in here // and return TRUE if we did indeed do // all the drawing ourselves virtual bool OnDraw(CDC* /*pDC*/) { return false; } // do we want to handle custom draw for // individual items virtual bool IsNotifyItemDraw() { return false; } // do we want to be notified when the // painting has finished virtual bool IsNotifyPostPaint() { return false; } // do we want to do any drawing after // the list control is finished virtual bool IsPostDraw() { return false; } // if we are doing the drawing afterwards ourselves // override and put the code in here // the return value is not used here virtual bool OnPostDraw(CDC* /*pDC*/) { return false; }
// // Callbacks for each item //
// return a pointer to the font to use for this item. // return NULL to use default virtual CFont* FontForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; } // return the text color to use for this item // return CLR_DEFAULT to use default virtual COLORREF TextColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; } // return the background color to use for this item // return CLR_DEFAULT to use default virtual COLORREF BkColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; } // do we want to do the drawing for this item ourselves? virtual bool IsItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // if we are doing the drawing ourselves // override and put the code in here // and return TRUE if we did indeed do // all the drawing ourselves virtual bool OnItemDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // do we want to handle custom draw for // individual sub items virtual bool IsNotifySubItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // do we want to be notified when the // painting has finished virtual bool IsNotifyItemPostPaint(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // do we want to do any drawing after // the list control is finished virtual bool IsItemPostDraw() { return false; } // if we are doing the drawing afterwards ourselves // override and put the code in here // the return value is not used here virtual bool OnItemPostDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // // Callbacks for each sub item //
// return a pointer to the font to use for this sub item. // return NULL to use default virtual CFont* FontForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; } // return the text color to use for this sub item // return CLR_DEFAULT to use default virtual COLORREF TextColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; } // return the background color to use for this sub item // return CLR_DEFAULT to use default virtual COLORREF BkColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; } // do we want to do the drawing for this sub item ourselves? virtual bool IsSubItemDraw(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // if we are doing the drawing ourselves // override and put the code in here // and return TRUE if we did indeed do // all the drawing ourselves virtual bool OnSubItemDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // do we want to be notified when the // painting has finished virtual bool IsNotifySubItemPostPaint(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } // do we want to do any drawing after // the list control is finished virtual bool IsSubItemPostDraw() { return false; } // if we are doing the drawing afterwards ourselves // override and put the code in here // the return value is not used here virtual bool OnSubItemPostDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
http://www.vckbase.com/document/viewdoc/?id=891
发现都不能改变没有填充数据的行的背景颜色!
我把行号设成没有数据的行,都会报错!即我需要一上来还没有数据的时候就能改变某一行的背景颜色。
我是在OnNMCustomdraw里画的,但就是画不了没有数据的行啊!
救命啊!
//MyListCtrl.h
#pragma once
#include "ListCtrlWithCustomDraw.h"class CMyListCtrl : public CListCtrlWithCustomDraw
{
// Construction
public:
CMyListCtrl();// Attributes
public:
struct ItemColor
{
int item;
COLORREF color;
ItemColor(){};
ItemColor(int row, COLORREF rowcolor){item = row; color = rowcolor;}
};
typedef CArray<ItemColor, ItemColor&> ItemColorArray;
ItemColorArray m_ICArray;// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyListCtrl)
//}}AFX_VIRTUAL
virtual bool IsNotifyItemDraw();
virtual bool IsNotifySubItemDraw(int nItem, UINT nState, LPARAM lParam);
virtual COLORREF TextColorForSubItem(int nItem, int nSubItem, UINT nState, LPARAM lParam);// Implementation
public:
void ResetAllItemColor();
void ChangeItemColor(int row, COLORREF color=0);
void AddItem(int row, const CStringArray& textArray, COLORREF color=0);
BOOL DeleteItem(int nItem);
virtual ~CMyListCtrl(); // Generated message map functions
protected:
//{{AFX_MSG(CMyListCtrl)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
#pragma once
class CListCtrlWithCustomDraw : public CListCtrl
{
// Construction
public:
CListCtrlWithCustomDraw();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CListCtrlWithCustomDraw)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CListCtrlWithCustomDraw();protected:
CFont* m_pOldItemFont;
CFont* m_pOldSubItemFont; //
// Callbacks for whole control
//
// do we want to do the drawing ourselves?
virtual bool IsDraw() { return false; }
// if we are doing the drawing ourselves
// override and put the code in here
// and return TRUE if we did indeed do
// all the drawing ourselves
virtual bool OnDraw(CDC* /*pDC*/) { return false; }
// do we want to handle custom draw for
// individual items
virtual bool IsNotifyItemDraw() { return false; }
// do we want to be notified when the
// painting has finished
virtual bool IsNotifyPostPaint() { return false; }
// do we want to do any drawing after
// the list control is finished
virtual bool IsPostDraw() { return false; }
// if we are doing the drawing afterwards ourselves
// override and put the code in here
// the return value is not used here
virtual bool OnPostDraw(CDC* /*pDC*/) { return false; }
//
// Callbacks for each item
//
// return a pointer to the font to use for this item.
// return NULL to use default
virtual CFont* FontForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; }
// return the text color to use for this item
// return CLR_DEFAULT to use default
virtual COLORREF TextColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
// return the background color to use for this item
// return CLR_DEFAULT to use default
virtual COLORREF BkColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
// do we want to do the drawing for this item ourselves?
virtual bool IsItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// if we are doing the drawing ourselves
// override and put the code in here
// and return TRUE if we did indeed do
// all the drawing ourselves
virtual bool OnItemDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// do we want to handle custom draw for
// individual sub items
virtual bool IsNotifySubItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// do we want to be notified when the
// painting has finished
virtual bool IsNotifyItemPostPaint(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// do we want to do any drawing after
// the list control is finished
virtual bool IsItemPostDraw() { return false; }
// if we are doing the drawing afterwards ourselves
// override and put the code in here
// the return value is not used here
virtual bool OnItemPostDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } //
// Callbacks for each sub item
//
// return a pointer to the font to use for this sub item.
// return NULL to use default
virtual CFont* FontForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; }
// return the text color to use for this sub item
// return CLR_DEFAULT to use default
virtual COLORREF TextColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
// return the background color to use for this sub item
// return CLR_DEFAULT to use default
virtual COLORREF BkColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
// do we want to do the drawing for this sub item ourselves?
virtual bool IsSubItemDraw(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// if we are doing the drawing ourselves
// override and put the code in here
// and return TRUE if we did indeed do
// all the drawing ourselves
virtual bool OnSubItemDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// do we want to be notified when the
// painting has finished
virtual bool IsNotifySubItemPostPaint(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// do we want to do any drawing after
// the list control is finished
virtual bool IsSubItemPostDraw() { return false; }
// if we are doing the drawing afterwards ourselves
// override and put the code in here
// the return value is not used here
virtual bool OnSubItemPostDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
// Generated message map functions
protected:
//{{AFX_MSG(CListCtrlWithCustomDraw)
afx_msg void OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
http://www.iuishop.com,在“文档”中有个“自绘ListCtrl”里面的源代码。
OwnerDraw实现起来好像很麻烦。慢慢学!我现在是把背景形成一幅背景图片实现的。