参考文章:https://blog.youkuaiyun.com/weixin_43913330/article/details/90287250
CListCtrlCl.h
#pragma once
#include "afxwin.h"
class CListCtrlCl : public CListCtrl
{
DECLARE_DYNAMIC(CListCtrlCl )
public:
CListCtrlCl ();
virtual ~CListCtrlCl ();
protected:
DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
public:
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
public:
// 设置背景颜色
void SetBkColor(int nRow, int nCol,COLORREF crColor);
//设置字体颜色
void SetFontColor(int nRow, int nCol, COLORREF crColor);
public:
// 列数
int m_nCol;
CMap<DWORD, DWORD&, COLORREF, COLORREF&> MapBkColor;
CMap<DWORD, DWORD&, COLORREF, COLORREF&> MapFontColor;
};
CListCtrlCl.cpp
#include "stdafx.h"
#include "ListCtrlCl.h"
// CListCtrlCl
IMPLEMENT_DYNAMIC(CListCtrlCl, CListCtrl)
CListCtrlCl::CListCtrlCl()
{
m_nCol = -1;
}
CListCtrlCl::~CListCtrlCl()
{
}
BEGIN_MESSAGE_MAP(CListCtrlCl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW,OnNMCustomdraw)
END_MESSAGE_MAP()
// CListCtrlCl 消息处理程序
void CListCtrlCl::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
CListCtrl::PreSubclassWindow();
}
void CListCtrlCl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
//首先声明一个NMLVCUSTOMDRAW结构体的指针pLVCD,关联pNMHDR。
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage.
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
//仅当pLVCD结构体中nmcd成员的dwDrawStage状态为CDDS_ITEMPREPAINT | CDDS_SUBITEM时
//我们就可以判断“行”和“列”,从而来设置文字颜色和文字背景颜色了。
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
// This is the prepaint stage for a subitem. Here's where we set the
// item's text and background colors. Our return value will tell
// Windows to draw the subitem itself, but it will use the new colors
// we set here.
COLORREF ItemColor;
DWORD dw = (DWORD)(pLVCD->iSubItem + m_nCol * pLVCD->nmcd.dwItemSpec);
if (MapBkColor.Lookup(dw, ItemColor))
{
//背景色
pLVCD->clrTextBk = ItemColor;
// SetFont(m_Font, false);
}
else
{
//如果不是选择的“行”和“列”就设置成系统默认的那种颜色。
pLVCD->clrTextBk = RGB(255, 255, 255);
}
if (MapFontColor.Lookup(dw, ItemColor))
{
//前景色
pLVCD->clrText = ItemColor;
//SetFont(m_Font, false);
}
else
{
pLVCD->clrText = RGB(0, 0, 0);
}
/*
if (pLVCD->iSubItem == m_iRow)
{
if (m_FontFlag)
{
m_FontFlag = FALSE;
SetFont(m_Font, false);
}
}
*/
// Store the colors back in the NMLVCUSTOMDRAW struct.
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}
void CListCtrlCl::SetBkColor(int nRow, int nCol, COLORREF crColor)
{
CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
if (pHeaderCtrl)
{
m_nCol = pHeaderCtrl->GetItemCount();
}
DWORD iItem = nRow * m_nCol + nCol;
MapBkColor.SetAt(iItem, crColor);//设置某行的颜色。
}
void CListCtrlCl::SetFontColor(int nRow, int nCol, COLORREF crColor)
{
CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl();
if (pHeaderCtrl)
{
m_nCol = pHeaderCtrl->GetItemCount();
}
DWORD iItem = nRow * m_nCol + nCol;
MapFontColor.SetAt(iItem, crColor);//设置单元格字体的颜色
}
CListCtrlCl m_lstResult;
/*******……*******/
m_lstResult.SetFontColor(行, 列, RGB(255,255,255));
m_lstResult.SetBkColor(行, 列, RGB(255, 0, 0));

本文介绍了如何在MFC程序中自定义CListCtrl控件的背景颜色和字体颜色,通过创建一个继承自CListCtrl的类实现对控件样式和显示效果的修改。

被折叠的 条评论
为什么被折叠?



