让CtrlList的某一行自定义颜色
参考文章:https://blog.csdn.net/weixin_43913330/article/details/90287250
定义类继承CtrlList
头文件中添加:
CMap<DWORD, DWORD&, COLORREF, COLORREF&> MapItemColor;
CMap<DWORD, DWORD&, COLORREF, COLORREF&> MapFontColor;
用于存储自定义设置的列表项颜色、字体等
需要添加专门用于自绘控件的事件响应:
//sonne 2020-08-18 void 类名::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) { int col_num = get_col_num(); 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; } 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 = (pLVCD->iSubItem + col_num * pLVCD->nmcd.dwItemSpec); if (MapItemColor.Lookup(dw, ItemColor)) { pLVCD->clrTextBk = ItemColor; //SetFont(m_Font, false); } else { //如果不是选择的“行”和“列”就设置成系统默认的那种颜色 pLVCD->clrTextBk = 16777215; } if (MapFontColor.Lookup(dw, ItemColor)) { pLVCD->clrText = ItemColor; //SetFont(m_Font, false); } else { pLVCD->clrText = 0; } /* if (pLVCD->iSubItem == m_iRow) { if (m_FontFlag) { m_FontFlag = FALSE; SetFont(m_Font, false); } } */ *pResult = CDRF_DODEFAULT; } }
需要将之注册到消息映射里:
BEGIN_MESSAGE_MAP(COnlineDeviceListCtrl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
END_MESSAGE_MAP()
对表格项进行自定义的一些函数,调用相关函数,自动会响应OnNMCustomdraw从而达到自绘的效果:
//sonne 2020-08-18 void 类名::SetCellColor(int iRow, int iCol, COLORREF color) { int col_num = get_col_num(); DWORD iItem = iRow * col_num + iCol; //设置某行的颜色 MapItemColor.SetAt(iItem, color); //重新染色 this->RedrawItems(iRow, iRow); //设置焦点 this->SetFocus(); UpdateWindow(); } /* * sonne * 2020-08-18 * 为列表某一行自定义颜色 */ void 类名::set_ctrllist_row_color(int row, COLORREF color) { int col_num = get_col_num(); for (int i=0; i<col_num; i++) { SetCellColor(row, i, color); } } /* * sonne * 2020-08-18 * 获取表格的列数 */ int 类名::get_col_num() { CHeaderCtrl* pHeaderCtrl = GetHeaderCtrl(); int col_num = 0; if (pHeaderCtrl) { col_num = pHeaderCtrl->GetItemCount(); } return col_num; } //sonne 2020-08-18 void 类名::SetFontColor(int iRow, int iCol, COLORREF color) { int col_num = get_col_num(); DWORD iItem = iRow * col_num + iCol; //设置单元格字体的颜色 MapFontColor.SetAt(iItem, color); //重新染色 this->RedrawItems(iRow, iRow); //设置焦点 this->SetFocus(); UpdateWindow(); }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步