CListCtrl非激活状态高亮显示

CListCtrl非激活状态高亮显示之前遇到过,搜了半天解决了但没记录,今天又遇到了,又搜了半天,所以这次得做个记录。

三个要点:
一、Always Show Selection:False

总是显示选定内容,即使控件没有焦点。
二、View:Report

三、响应NM_CUSTOMDRAW


void CmfcDlg::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 );

       if (lstTest.GetItemState(nItem,LVIS_SELECTED))//如果要刷新的项为当前选择的项,则将文字设为白色,背景色设为蓝色
        {
            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;
    }
}


你可能感兴趣的:(mfc,CListCtrl,NM_CUSTOMDRAW)