MFC程序 —— 改变ListCtrl的背景颜色和字体颜色

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

参考文章: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));

 

 

 

 

 

 

 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值