Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Listbox item color

131 views
Skip to first unread message

Tim O'Connor

unread,
May 26, 1998, 3:00:00 AM5/26/98
to

Can someone guide me on the following. I want to create a version of
CListbox that has SetColor and GetColor functions. Im getting
there, but Im stuck. I KNOW that I have to make sure my listbox is
ownerdraw, and 'do the drawing myself' but HOW do I do the drawing myself?
None of the code at www.codeguru.com has really helped me here. I realise
I have to overide the DrawItem and MeasureItem functions, but I really
dont have much of a clue as to why or whats happening with much of the
source code on ownerdraw. Are there any sites that EXPLAIN whats
happening, rather than just providing source code?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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? ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


ScottMcP

unread,
May 26, 1998, 3:00:00 AM5/26/98
to

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);
}
}
}

will

unread,
Jun 1, 1998, 3:00:00 AM6/1/98
to
When you write DrawItem, just remember to make all your coordinates
relative to the rect passed in the LPDRAWITEMSTRUCT. With a little
care, you can optimize the painting. BTW: Owner drawn menus are quite
similar. Just remember to set the width in MeasureItem and avoid
painting outside the clip rect (which, for menus, does not clip).
vcard.vcf
0 new messages