1. 程式人生 > >讓CListCtrl選中行恆保持其藍色高亮狀態

讓CListCtrl選中行恆保持其藍色高亮狀態

為CListCtrl控制元件新增NM_CUSTOMDRAW事件響應函式,這樣當我們點選介面的其他地方地方時,列表中的選中項仍處於選中狀態。

afx_msg void OnNMCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult);

ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_CALL_DATA, OnNMCustomDrawList)

void CDataCallDlg::OnNMCustomDrawList(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
 
    *pResult = CDRF_DODEFAULT;
 
    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        *pResult = CDRF_NOTIFYSUBITEMDRAW;
    }
    else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
    {
  
        COLORREF clrNewTextColor, clrNewBkColor;
  
        int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
       
        POSITION pos = m_ctrlList.GetFirstSelectedItemPosition();
        int index = m_ctrlList.GetNextSelectedItem(pos);
       
        if (index == nItem)//如果要重新整理的項為當前選擇的項,則將文字設為白色,背景色設為藍色
        {
            clrNewTextColor = RGB(255,255,255);        //Set the text to white
            clrNewBkColor = RGB(49,106,197);        //Set the background color to blue
        }
        else
        {
            clrNewTextColor = RGB(0,0,0);        //set the text black
            clrNewBkColor = RGB(255,255,255);    //leave the background color white
        }
  
        pLVCD->clrText = clrNewTextColor;
        pLVCD->clrTextBk = clrNewBkColor;
  
        *pResult = CDRF_DODEFAULT;
    }
}

CListCtrl控制元件的“Show Always Selection”屬性不要選中