需求场景:
单击、按住ctrl\shfit键多选选中表控件后,需要至其他区域进行后续操作,失焦会使选中项高亮取消,在某些业务场景下需要保留选中项的高亮状态
示例代码:
添加响应事件 NM_CUSTOMDRAW
//实现代码
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;
}
}
网友评论