ListBox重绘



void CUIList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
BITMAP bmp;
GetObject(m_hBitmap, sizeof(bmp), &bmp);

HDC hDC = lpDrawItemStruct->hDC;
CRect rc = lpDrawItemStruct->rcItem;
HDC hMemDC = ::CreateCompatibleDC(hDC);

if( lpDrawItemStruct->itemState & ODS_SELECTED )

{

//注意rect.left, rect.top

m_hOldBmp = (HBITMAP)::SelectObject(hMemDC, m_hBitmap);
::TransparentBlt(hDC, rc.left, rc.top, rc.Width(), rc.Height(), hMemDC, 0, 0, bmp.bmWidth, bmp.bmHeight, RGB(255,0,255));
SelectObject(hMemDC, m_hOldBmp);
}
else
{ //如果放在OnEraseBKgnd 里面,会非常闪烁
CRect clientRC;
GetClientRect(&clientRC);

HBRUSH hBrush = CreateSolidBrush(RGB(255, 200, 200));
::FillRect(hDC, &clientRC, hBrush);
DeleteObject(hBrush);
}


//这个必须重绘字符内容,但有个问题:第N>1次重绘时字体加粗

int height = rc.Height();
CRect rect = rc;
rect.top = -height;
SetBkMode(hDC, TRANSPARENT);
CString text;
for(int i = 0; i < GetCount(); i++)
{
GetText(i, text);
rect.top += height;
rect.bottom = rect.top + height;
DrawText(hDC, text, text.GetLength(), &rect, DT_LEFT | DT_SINGLELINE);

}

DeleteObject(hMemDC);

}


void CUIList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
}


int CUIList::CompareItem(LPCOMPAREITEMSTRUCT lpMeasureItemStruct)
{
return 0;
}

你可能感兴趣的:(ListBox重绘)