示例#1
1
void CMLListView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC dcItem;
	dcItem.Attach(lpDrawItemStruct->hDC);
	lpDrawItemStruct->itemState;

	CHeaderCtrl* pHdr = GetListCtrl().GetHeaderCtrl();
	int nColCount = pHdr->GetItemCount();

	CRect rcCell = lpDrawItemStruct->rcItem;
	rcCell.right = 0;

	CRect rcCellText;
	for(int iCol = 0; iCol < nColCount; iCol ++)
	{
		int nWidthCol = GetListCtrl().GetColumnWidth(iCol);
		rcCell.right = rcCell.left + nWidthCol;

		CString strItem;
		GetItemText(lpDrawItemStruct->itemID, iCol, strItem);

		dcItem.FillSolidRect(rcCell, OnGetCellBkColor(lpDrawItemStruct->itemID, iCol));
		rcCellText = rcCell;

		rcCellText.DeflateRect(2, 1, 0, 0);
		dcItem.SelectObject(OnGetCellFont(lpDrawItemStruct->itemID, iCol));
		dcItem.SetTextColor(OnGetCellTextColor(lpDrawItemStruct->itemID, iCol));
		dcItem.DrawText(strItem, rcCellText, DT_EXTERNALLEADING|DT_LEFT);
		rcCell.left = rcCell.right;
	}

	if((lpDrawItemStruct->itemState & ODS_FOCUS) == ODS_FOCUS)
	{
		CRect rcFocus = lpDrawItemStruct->rcItem;
		rcFocus.DeflateRect(0, 0, 0, 1);
		dcItem.Draw3dRect(rcFocus, OnGetItemFocusColor(), OnGetItemFocusColor());
	}

	dcItem.SelectStockObject(SYSTEM_FONT) ;
	dcItem.Detach();
}
//****************************************************************************************
void CBCGPListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

    switch(lplvcd->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
	    *pResult = CDRF_NOTIFYITEMDRAW;
	    break;

	case CDDS_ITEMPREPAINT:
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		break;

	case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
		{
			int iColumn = lplvcd->iSubItem;
			int iRow = (int) lplvcd->nmcd.dwItemSpec;

			lplvcd->clrTextBk = OnGetCellBkColor (iRow, iColumn);
			lplvcd->clrText = OnGetCellTextColor (iRow, iColumn);

			if (iColumn == m_iSortedColumn && m_bMarkSortedColumn &&
				lplvcd->clrTextBk == GetBkColor ())
			{
				lplvcd->clrTextBk = m_clrSortedColumn;
			}

			HFONT hFont = OnGetCellFont (	iRow, iColumn, 
											(DWORD) lplvcd->nmcd.lItemlParam);
				
			if (hFont != NULL)
			{
				m_hOldFont = (HFONT) SelectObject (lplvcd->nmcd.hdc, hFont);
				ASSERT (m_hOldFont != NULL);

				*pResult = CDRF_NEWFONT | CDRF_NOTIFYPOSTPAINT;
			}
			else
			{
				*pResult = CDRF_DODEFAULT;
			}
		}
	    break;

	case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
		if (m_hOldFont != NULL)
		{
			SelectObject (lplvcd->nmcd.hdc, m_hOldFont);
			m_hOldFont = NULL;
		}

		*pResult = CDRF_DODEFAULT;
		break;
	}
}