重CListCtrl派生新类,并重载CListCtrl::DrawItem.

重CListCtrl派生新类,并重载CListCtrl::DrawItem.

void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);

LVITEM lvi = {0};
lvi.mask = LVIF_STATE;//|LVIF_IMAGE;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.iItem = lpDIS->itemID;
BOOL bGet = GetItem(&lvi);

BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED) &&
((GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS))));

// 画文本背景
CRect rcBack = lpDIS->rcItem;
pDC->SetBkMode(TRANSPARENT);
if( bHighlight )
{
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));

pDC->FillRect(rcBack, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
}
else
{
if (lpDIS->itemID%2 ==0)
{
pDC->SetTextColor(0x0C0F80);
pDC->FillRect(rcBack, &CBrush(0xEEF1E3));
}
else
{
pDC->SetTextColor(RGB(0,128,255));
pDC->FillRect(rcBack, &CBrush(0xE3e3e3));
}
}

//设置字体颜色
CString str;

//得到焦点大小
CRect rcFocus = lpDIS->rcItem;
rcFocus.DeflateRect(1,1,1,1);

if (lpDIS->itemAction & ODA_DRAWENTIRE)
{
//写文本
CString szText;
for (int i = 0; i < /*GetColumns()*/10; i++)
{ //循环得到文本
CRect rcItem;
if ( !GetSubItemRect(lpDIS->itemID, i, LVIR_LABEL, rcItem ))
continue;
szText = GetItemText( lpDIS->itemID, i );

rcItem.left += 5; rcItem.right -= 1;
pDC->DrawText(szText/*szText*/, lstrlen(szText), &rcItem, DT_CENTER|DT_VCENTER|DT_NOPREFIX|DT_SINGLELINE);
}
}

}

你可能感兴趣的:(重CListCtrl派生新类,并重载CListCtrl::DrawItem.)