改变WTL中CListViewCtrl中行颜色及背景色的方法(一)

CListViewCtrl继承于CCustomDraw的方法:

 

class CColorListCtrl : public CWindowImpl
	, public CCustomDraw
{
public:
	BEGIN_MSG_MAP(CMovieListCtrl)
	//	CHAIN_MSG_MAP(CCustomDraw)
		CHAIN_MSG_MAP_ALT(CCustomDraw, 1)
		DEFAULT_REFLECTION_HANDLER()
	END_MSG_MAP()

	// Overrideables
	DWORD OnPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW /*lpNMCustomDraw*/)
	{
		return CDRF_NOTIFYITEMDRAW;
	}

	DWORD OnItemPrePaint(int idCtrl, LPNMCUSTOMDRAW lpNMCustomDraw)
	{
		NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( lpNMCustomDraw );

		int nItem = pLVCD->nmcd.dwItemSpec;

		return CDRF_NOTIFYSUBITEMDRAW;
	}

	DWORD OnSubItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw)
	{
		NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( lpNMCustomDraw );

		int nItem = pLVCD->nmcd.dwItemSpec;
		int nSubItem = pLVCD->iSubItem;

		return CDRF_DODEFAULT;
	}
};

红色的是要注意的地方不能使用CHAIN_MSG_MAP宏,而要使用CHAIN_MSG_MAP_ALT宏,因为其CCustomDraw需要的是ALT_MSG_MAP链,而不是其默认的消息映射。

 

同时对话框使用时需要注意:

(1) 对话框消息链

BEGIN_MSG_MAP(CMainDlg)
  MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
  COMMAND_ID_HANDLER(IDOK, OnOK)
  COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
  REFLECT_NOTIFICATIONS()
 END_MSG_MAP()


(2)CListViewCtrl控件需要子类化,而不是简单的Attatch();

m_colorListCtrl.SubclassWindow(GetDlgItem(IDC_LIST_COLOR_CTRL));


 

(3)参考的链接:

http://microsoft.public.vc.atl.narkive.com/jcRucwT1/wtl-list-view-custom-draw-problem
 

你可能感兴趣的:(WTL)