How to change color of CListCtrl column(如何更改 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 列的颜色
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- 近似搜索的工作原理 2021-01-01