如何更改CListCtrl列的颜色 [英] How to change color of CListCtrl column
问题描述
我想将特定列的背景颜色更改为对话框的颜色(灰色)。我如何实现它?
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功能包类(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列的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!