本文主要是介绍ListCtrl失去焦点仍然高亮显示并改变单元格颜色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里使用VS2010的MFC进行讲解 :
1.找到需要添加的消息的ListControl控件的属性,选择事件里面,选择NM_CUSTOMDRAW事件,程序自动生成方法.
2.在.cpp文件中自动生成的 OnNMCustomdrawList1 方法里面添加代码.
(1).为List实现失去焦点仍然高亮显示.
void CDlgDacggl::OnNMCustomdrawList1(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 m_clrText2,m_clrBKcolor;int nItem = static_cast<int>(pLVCD->nmcd.dwItemSpec);POSITION pos = m_ctrDacgglList.GetFirstSelectedItemPosition();int index = m_ctrDacgglList.GetNextSelectedItem(pos); //获取鼠标点击的哪一项索引if (nItem == index) //某一项被选中则显示{m_clrText2 = RGB(255,255,255);m_clrBKcolor = RGB(49,106,197);}else //其余的默认如下{m_clrText2 = RGB(0,0,0);m_clrBKcolor = RGB(255,255,255);}pLVCD->clrText = m_clrText2;pLVCD->clrTextBk = m_clrBKcolor;}
}
(2).为List相应的单元格添加字体颜色.
void CDlgDacggl::OnNMCustomdrawList1(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 m_clrText,m_clrBKColor;int nItem = static_cast<int>(pLVCD->nmcd.dwItemSpec);// 判断使ListCtrl不同颜色现实的条件CString str = m_ctrDacgglList.GetItemText(nItem,2); //获取控件第二列的值,m_list为控件的变量if (str == "有" && 2 == pLVCD->iSubItem) //这里我的第三列单元格里面有一个汉字 "有" 字,若单元格里为 "有" 字,则显示下面的RGB,反之.{//设置该列的颜色m_clrText = RGB(255,127,0);}else if (str == "无" && 2 == pLVCD->iSubItem){m_clrText = RGB(0,139,139);}else //其他Text默认为RGB(0,0,0){m_clrText = RGB(0,0,0);}pLVCD->clrText = m_clrText;*pResult = CDRF_DODEFAULT;}
}
(3).在List失去焦点后仍然高亮显示,并且改变单元格的文字颜色,还可以在失去焦点后再改变文字颜色.
void CDlgDacggl::OnNMCustomdrawList1(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 m_clrText,m_clrBKColor;int nItem = static_cast<int>(pLVCD->nmcd.dwItemSpec);CString str = m_ctrDacgglList.GetItemText(nItem,2); //获取控件第二列的值,m_list为控件的变量BOOL bSelect = FALSE;POSITION pos = m_ctrDacgglList.GetFirstSelectedItemPosition();while(pos) //第二步当选中时,失去焦点高亮显示{int index = m_ctrDacgglList.GetNextSelectedItem(pos);if(index==nItem){bSelect = TRUE;break;}}if (bSelect) //当失去焦点后再回来改变单元格里字体的颜色{m_clrText = RGB(255,255,255);//"有"是单元格里的字符,2==pLVCD->iSubItem 是等于第三列的索引时if (str == "有" && 2 == pLVCD->iSubItem){//设置该列的颜色m_clrText = RGB(255,127,0);}m_clrBKColor = RGB(51,102,205);}else //第一步显示这里的代码块{m_clrText = RGB(0,0,0);if (str == "有" && 2 == pLVCD->iSubItem){//设置该列的颜色m_clrText = RGB(255,127,0);}else if (str == "无" && 2 == pLVCD->iSubItem){m_clrText = RGB(0,139,139);}m_clrBKColor = RGB(255,255,255);}pLVCD->clrText = m_clrText;pLVCD->clrTextBk = m_clrBKColor;*pResult = CDRF_DODEFAULT;}
}
部分参考 : MFC listCtrl选中某一项,LIST失去焦点时,该项仍然显示高亮_字子昂的博客-CSDN博客
这篇关于ListCtrl失去焦点仍然高亮显示并改变单元格颜色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!