// sublclass listbox so that our ccolorlistbox receives windows events
m_clistbox.subclassdlgitem(idc_clistbox, this); // idc_clistbox is the listbox's resource id
if ((lpdis->itemState & ODS_SELECTED) && // if item has been selected
(lpdis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
DrawFocusRect(lpdis->hDC, &lpdis->rcItem);
if (!(lpdis->itemState & ODS_SELECTED) && // if item has been deselected
(lpdis->itemAction & ODA_SELECT))
DrawFocusRect(lpdis->hDC, &lpdis->rcItem);
if(lpdis->itemData) // if color information is present
cvText = SetTextColor(lpdis->hDC, lpdis->itemData);
else // if no color information, use default system colors
cvText = SetTextColor(lpdis->hDC, GetSysColor((lpdis->itemState & ODS_SELECTED)
? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT));
// always use system colors for background
cvBack = SetBkColor(lpdis->hDC, GetSysColor((lpdis->itemState & ODS_SELECTED)
? COLOR_HIGHLIGHT : COLOR_WINDOW));
// get and display item text
GetText(lpdis->itemID, itemString );
DrawText(lpdis->hDC, itemString, -1, &lpdis->rcItem, DT_LEFT | DT_SINGLELINE);
// restore DC colors
SetTextColor(lpdis->hDC, cvText);
SetBkColor(lpdis->hDC, cvBack);
}
//***********************************************
// original AddString() method
//
// purpose: Add a string to the listbox
//
// parameters:
// lpszItem: pointer to item text
//
// remarks:
// provided because CListBox::AddString is
// NOT virtual
//
// return: item index
//***********************************************
int CColorListBox::AddString( LPCTSTR lpszItem)
{
return ((CListBox*)this)->AddString(lpszItem);
}
//***********************************************
// new AddString() method
//
// purpose: Add a string to the listbox
//
// parameters:
// lpszItem: pointer to item text
// rgb: text color as a COLORREF
//
// return: item index
//***********************************************
int CColorListBox::AddString( LPCTSTR lpszItem,COLORREF rgb )
{
int item = AddString(lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;
}
//***********************************************
// new InsertString() method
//
// purpose: Insert a string to the listbox
//
// parameters:
// nIndex: index of inserted item
// lpszItem: pointer to item text
// rgb: text color as a COLORREF
//
// return: item index
//***********************************************
int CColorListBox::InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
int item = ((CListBox*)this)->InsertString(nIndex,lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;