讓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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章