WTL CListViewCtrl 实现列表元素字体着色

WTL CListViewCtrl 实现列表元素字体着色


CListViewCtrl 实现列表元素字体着色的过程:

假设是基于对话框开发界面上有一个列表控件, 数据在该列表控件中以 “Report” 形式展现。

包含必要的头文件如下: (一般放在stdafx.h)

#pragma once

#define WINVER		0x0500
#define _WIN32_WINNT	0x0501
#define _WIN32_IE	0x0501
#define _RICHEDIT_VER	0x0500

#define _WTL_NO_CSTRING  
#define _WTL_NO_WTYPES  
#include   
#include   
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


子类继承于CCustomDraw:(在 .H 文件中)

class CMyDlg :   public CDialogImpl,
	public CWinDataExchange,
	public CCustomDraw

增加消息:

BEGIN_MSG_MAP_EX(CPNXClientFixToolMd5CheckDlg)
		MSG_WM_INITDIALOG(OnInitDialog)
		COMMAND_ID_HANDLER(IDC_BTN_CHK, OnBnClickedBtnChk)
		COMMAND_ID_HANDLER(IDC_BTN_EXPORT_TO_TXT, OnBnClickedBtnExportToTxt)
		NOTIFY_HANDLER(IDC_LIST_RESULT, NM_CUSTOMDRAW, OnNMCustomdrawListResult)
	END_MSG_MAP()


消息响应函数的声明:
LRESULT OnNMCustomdrawListResult(int/*idCtrl*/, LPNMHDR pNMHDR, BOOL&/*bHandled*/);

类成员中,列表控件的定义:

CListViewCtrl m_list;

消息响应函数的定义(在 .CPP 文件中):

LRESULT CMyDlg::OnNMCustomdrawListResult(int/*idCtrl*/, LPNMHDR pNMHDR, BOOL&/*bHandled*/)
{
	LPNMLVCUSTOMDRAW pLVNMCD = reinterpret_cast(pNMHDR);
	int nResult = CDRF_DODEFAULT;
	if(CDDS_PREPAINT== pLVNMCD->nmcd.dwDrawStage)
	{
		nResult = CDRF_NOTIFYITEMDRAW;
	}
	else if (CDDS_ITEMPREPAINT == pLVNMCD->nmcd.dwDrawStage)
	{
		nResult = CDRF_NOTIFYSUBITEMDRAW;
		DWORD dwItem = (DWORD)pLVNMCD->nmcd.dwItemSpec;
		if (m_list.GetItemData(dwItem) == 0)
		{
			pLVNMCD->clrText = RGB(0,0,0);
			pLVNMCD->clrTextBk = RGB(255,255,255);
		}
		else if (m_list.GetItemData(dwItem) == 1)
		{
			pLVNMCD->clrText = RGB(0,255,0);
			pLVNMCD->clrTextBk = RGB(255,255,255);
		}
		else
		{
			pLVNMCD->clrText = RGB(255,0,0);
			pLVNMCD->clrTextBk = RGB(255,255,255);
		}


	}
	else if (pLVNMCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT|CDDS_SUBITEM))
	{
	}
	return nResult;
}




你可能感兴趣的:(C++,WTL)