//添加消息
afx_msg void OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult);
//捆绑
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CInvoicedistribution::OnNMCustomdrawList1)
//实现方法
void CMainFrame::OnNMCustomdrawRecQueryList(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_lstRecQueryRes.GetFirstSelectedItemPosition();
int index = m_lstRecQueryRes.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;
}
}
调用方法
m_list.SetItemState(5, LVIS_FOCUSED | LVIS_SELECTED,LVIS_FOCUSED | LVIS_SELECTED);
m_list.RedrawItems(5,5);
我们知道对CListBox来说,选中一行非常简单,只要SetCurSel()函数就可以了。
对CListCtrl来说可以这样
m_list.SetItemState(nItem, LVIS_FOCUSED | LVIS_SELECTED,LVIS_FOCUSED | LVIS_SELECTED);
取消选中可以这样
m_list.SetItemState(nItem, 0, LVIS_FOCUSED | LVIS_SELECTED);
还有非常重要的一点是
将ListCtrl控件的Tab键顺序设置为1,否则不容易出现.
另外一种方法
void CListCtrlEx::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVCUSTOMDRAW* lplvcd = (NMLVCUSTOMDRAW*)(pNMHDR);
int nRow = (int)lplvcd->nmcd.dwItemSpec;
switch (lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult |= CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
//------------------------------------------------------
// 单行与双行的颜色 [Juncox 6月15日 11:57:29]
if(nRow & 1){ // 单行
lplvcd->clrTextBk = RGB(0xFF,0xFF,0xFF);
lplvcd->clrText = RGB(0x00,0x00,0x00);
*pResult = CDRF_NEWFONT;
}else{ // 双行
lplvcd->clrTextBk = RGB(0xBB,0xCC,0xFF);
lplvcd->clrText = RGB(0x44,0x33,0x00);
*pResult = CDRF_NEWFONT;
}
//------------------------------------------------------
// 打勾项 [Juncox 6月15日 11:57:38]
if(GetCheck(nRow) && (GetExtendedStyle() & LVS_EX_CHECKBOXES))
{
lplvcd->clrTextBk = RGB(0x00,0x00,0x80);
lplvcd->clrText = RGB(0xFF,0xFF,0x00);
//--------------------------------------------
// 设置字体
HFONT hFont = (HFONT)this->SendMessage(WM_GETFONT);
LOGFONT lf; GetObject(hFont, sizeof(lf), &lf);
lf.lfStrikeOut = TRUE; // 删除线
lf.lfItalic = TRUE; // 斜体
hFont = CreateFontIndirect(&lf);
SelectObject(lplvcd->nmcd.hdc, hFont);
DeleteObject(hFont);
*pResult = CDRF_NEWFONT;
}
//------------------------------------------------------
// 选中行 [Juncox 6月15日 11:57:42]
if( GetItemState(nRow, CDIS_SELECTED) )
{ // 预留BUG,看有多少人是摘自这里, CDIS_SELECTED前少取反号,且把异或为与
// 使用此代码段,请留下Juncox@163.com说明
lplvcd->nmcd.uItemState ^= CDIS_SELECTED;// 预留BUG
lplvcd->clrTextBk =RGB(255, 0, 0);
lplvcd->clrText = RGB(0, 0, 255);
*pResult = CDRF_NEWFONT;
}
break;
default:
*pResult = CDRF_DODEFAULT;
break;
}
}