~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Timothy O'Connor, Multimedia programmer for the Faculty of Nursing, ~
~ RMIT. All opinions expressed are my own, and do not represent RMIT. ~
~ Who's the bad guy? ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You might not find exactly what you're looking for because what you do
inside DrawItem is the same thing you do inside OnPaint, OnDraw, etc.
So read about drawing using device contexts if that part is confusing to
you. That said, here's a tiny example for owner-drawing a list box with
a custom color.
// MFC Owner draw call to measure line height
void CRedListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
DWORD dwUnits = ::GetDialogBaseUnits();
lpMIS->itemHeight = HIWORD(dwUnits); // Use dialog font height
}
// MFC Owner draw call to draw one line of the list box
void CRedListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
COLORREF crOldBk, crOldTx;
char czbuf[MAX_REDLISTTEXT];
int txtlen;
// Draw only if the line scrolled or select state changed
if ( (lpDIS->itemAction & (ODA_DRAWENTIRE | ODA_SELECT))
&& (lpDIS->itemID >= 0) )
{
// A device context for drawing is provided in lpDIS
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
txtlen = GetText(lpDIS->itemID, czbuf);
// If this line is selected, use custom highlight color
if (lpDIS->itemState & ODS_SELECTED )
{ crOldBk = pDC->SetBkColor(RGB(255,0,0));
crOldTx = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
}
// Paint the text and background
pDC->ExtTextOut( 0, lpDIS->rcItem.top, // text x, y
ETO_OPAQUE,
&(lpDIS->rcItem), // rect
czbuf,
txtlen,
NULL );
// If we changed colors, restore DC
if (lpDIS->itemState & ODS_SELECTED )
{ pDC->SetBkColor(crOldBk);
pDC->SetTextColor(crOldTx);
}
}
}