c++ - 如何更改 CListCtrl 列的颜色

标签 c++ mfc clistctrl

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

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

  // TODO: change color 

  *pResult = 0;
}

谢谢

最佳答案

如果您使用的是"new"MFC 功能包类(VS 2008 SP1 及更高版本),则可以使用 CMFCListCtrl 而不是 CListCtrl 并使用 CMFCListCtrl::OnGetCellBkColor .

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

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

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

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 消息 as explained here .

关于c++ - 如何更改 CListCtrl 列的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19682479/

相关文章:

c++ - 为什么 'declval' 指定为 'add_rvalue_reference<T>::type' 而不是 'T&&' ?

c++ - 在for循环中设置指向 double 组的指针

c++ - 删除线程时崩溃

c++ - 有什么办法可以使 SHFileOperation 成为模态吗?

c++ - 为什么我的 CUDA 应用程序没有启动?

c++ - 针对 MFC42 的 Visual Studio 2015 链接

c++ - 列 ClistCtrl 的排序不正确

c++ - 虚拟 CListCtrl 自动调整大小

c++ - 绘制错误的 CListCtrl 项

c++ - 我可以知道为什么这个函数的参数不是 int 类型吗?我不明白这里指针的整个概念