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

来源:互联网 发布:网络歌曲大全100首一 编辑:程序博客网 时间:2024/05/18 23:15
WTL CListViewCtrl 实现列表元素字体着色


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

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

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

#pragma once#define WINVER0x0500#define _WIN32_WINNT0x0501#define _WIN32_IE0x0501#define _RICHEDIT_VER0x0500#define _WTL_NO_CSTRING  #define _WTL_NO_WTYPES  #include <atlstr.h>  #include <atltypes.h>  #include <atlbase.h>#include <atlapp.h>#include <atlframe.h>#include <atlctrls.h>#include <atldlgs.h>#include <atlddx.h>#include <atlcrack.h>#include <atlwin.h>#include <atluser.h>


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

class CMyDlg :   public CDialogImpl<CMyDlg>,public CWinDataExchange<CMyDlg>,public CCustomDraw<CMyDlg>

增加消息:

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<LPNMLVCUSTOMDRAW>(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;}




0 0