如何更改 CListCtrl 列的颜色

How to change color of CListCtrl column(如何更改 CListCtrl 列的颜色)

本文介绍了如何更改 CListCtrl 列的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将特定列的背景颜色更改为对话框的颜色(灰色).我怎样才能实现它?

I want to change the background color of a specific column to a color of the dialog (grey). How can I achive it?

void CUcsOpTerminalDlg::OnCustomdrawFeatureList(NMHDR *pNMHDR, LRESULT *pResult)
{
  LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);

  // TODO: change color 

  *pResult = 0;
}

谢谢

推荐答案

如果您使用新的"MFC Feature Pack 类(VS 2008 SP1 及更高版本),您可以使用 CMFCListCtrl 代替 CListCtrl 并使用 CMFCListCtrl::OnGetCellBkColor.

If you are using the "new" MFC Feature Pack classes (VS 2008 SP1 and up), you can use CMFCListCtrl instead of CListCtrl and use CMFCListCtrl::OnGetCellBkColor.

您必须从中派生自己的类并覆盖 CMFCListCtrl::OnGetCellBkColor.在那里,只需检查列索引并返回您需要的背景颜色:

You would have to derive your own class from it and override CMFCListCtrl::OnGetCellBkColor. There, just check the column index and return the background color you need:

COLORREF CMyColorfulListCtrl::OnGetCellBkColor(int nRow,int nColumn)
{
    if (nColumn == THE_COLUMN_IM_INTERESTED_IN)
    {
        return WHATEVER_COLOR_I_NEED;
    }
    return CMFCListCtrl::OnGetCellBkColor(nRow, nColumn);
}

或者,如果您需要对话框来做出决定,您可以从该函数中查询对话框:

Or, if you need the dialog to make the decission, you can query the dialog from that function:

COLORREF CMyColorfulListCtrl::OnGetCellBkColor(int nRow,int nColumn)
{
    COLORREF color = GetParent()->SendMessage(UWM_QUERY_ITEM_COLOR, nRow, nColumn);

    if ( color == ((COLORREF)-1) ) 
    { // If the parent doesn't set the color, let the base class decide
        color = CMFCListCtrl::OnGetCellBkColor(nRow, nColumn);
    }    
    return color;
}

请注意,UWM_QUERY_ITEM_COLOR 是自定义消息.我通常使用注册的 Windows 消息这里解释了.

Note that UWM_QUERY_ITEM_COLOR is a custom message. I usually use Registered Windows Messages as explained here.

这篇关于如何更改 CListCtrl 列的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何更改 CListCtrl 列的颜色

基础教程推荐