CListBox::DrawItem(), CListBox::AddItem(), CListBox::SetItemData()
Env: WindowsXP, VC++6.00
To have individual lines inside a CListBox to be different colors, I used
Dr. GUI's solution in [MSDN] which can be refered at "Ask Dr. GUI #43".
In the solution, the color of a line is specified by SetItemData() as
follows,
int CMyListBox::AddItem(const CString& str, COLORREF rgbText)
{
int nIndex;
nIndex = AddString(str);
if( CB_ERR != nIndex )
SetItemData(nIndex, rgbText);
return nIndex;
}
And the line is drawed by the color at the following function.
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC dc;
CRect rcItem(lpDIS->rcItem);
UINT nIndex = lpDIS->itemID;
COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?
COLOR_HIGHLIGHT : COLOR_WINDOW);
dc.Attach(lpDIS->hDC);
CBrush br(rgbBkgnd);
dc.FillRect(rcItem, &br);
if( lpDIS->itemState & ODS_FOCUS )
dc.DrawFocusRect(rcItem);
if( nIndex != (UINT)-1 )
{
// The text color is stored as the item data.
COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?
::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
CString str;
GetText(nIndex, str);
dc.SetBkColor(rgbBkgnd);
dc.SetTextColor(rgbText);
dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
}
dc.Detach();
}
Obviously, the line's ItemData is used. I am tring to use line's ItemData
for other purpose. Then, how should I fix the above functions so that
ItemData is not occupied.
TIA
William