需求場景:
單擊进统、按住ctrl\shfit鍵多選選中表控件后悦污,需要至其他區(qū)域進行后續(xù)操作代芜,失焦會使選中項高亮取消策州,在某些業(yè)務場景下需要保留選中項的高亮狀態(tài)
示例代碼:
添加響應事件 NM_CUSTOMDRAW
//實現(xiàn)代碼
void TestDemoDlg::OnNMCustomdrawTestList(NMHDR *pNMHDR, LRESULT *pResult)
{
? NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( pNMHDR );
switch(pLVCD->nmcd.dwDrawStage)
{
?case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;?
?case CDDS_ITEMPREPAINT:
*pResult = CDRF_NOTIFYSUBITEMDRAW;
break;?
?case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
COLORREF clrNewTextColor, clrNewBkColor;
?int nItem = static_cast( pLVCD->nmcd.dwItemSpec );
BOOL bSelect = FALSE;
POSITION pos =m_TestList.GetFirstSelectedItemPosition();
while(pos)
{
int index = m_TestList.GetNextSelectedItem(pos);
if(index==nItem)
{
bSelect = TRUE;
break;
}
}
if(bSelect)
{
clrNewTextColor =RGB(255,0,0);//設置選中行文本顏色 紅色
clrNewBkColor? = RGB(51,102,205);//設置選中行文本背景色 藍色
}
else
{
clrNewTextColor = RGB(0,0,0);//保持文本顏色 黑色
clrNewBkColor = RGB(204,232,207); //保持文本背景色 白色
}
pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
break;
//Default:
//*pResult = CDRF_DODEFAULT;
break;
}
}