将下面这三行代码加入到mylistbox.h中
int AddString( LPCTSTR lpszItem);
int AddString( LPCTSTR lpszItem, COLORREF rgb);
int InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb);
这是重载三个函数的声明,重要的是后面的二个,因为我们必须能够使用我们想要的功能。在mylistbox.cpp中加入这三个函数的函数体:
int CMyListBox::AddString( LPCTSTR lpszItem)
{
return (((CListBox*)this)->AddString(lpszItem);
}
int CMyListBox::AddString( LPCTSTR lpszItem,COLORREF rgb )
{
int item = AddString(lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;}
int CMyListBox::InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
int item = ((CListBox*)this)->InsertString(nIndex,lpszItem);
if(item >=0) SetItemData(item,rgb);
return item;
}
if (lpdis->itemID < 0)
return;
COLORREF cvText;
COLORREF cvBack;
CString itemString;
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);
以上转自: http://blog.sina.com.cn/s/blog_513abb890100bjax.html
将DrawItem中的LPDRAWITEMSTRUCT lpDrawItemStruct 改为LPDRAWITEMSTRUCT lpdis编译能通过,但运行失败;
在testdlg.cpp的oninitdialog()中的m_list1.SubclassDlgItem(IDC_LIST1, this)之后加上m_list1.AddString(("test",RGB(255,0,0))后能运行起来,也能达到预期效果,原因待查,得解决才能用。。
之前程序崩溃以解决 将drawitem函数中的代码改为如下:
if (lpdis->itemID < 0)
return;
COLORREF cvText;
COLORREF cvBack;
CString itemString;
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
if(lpdis->itemID != (UINT) -1)
{
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);
}
效果如下图:
可见效果不太好,需要修改DrawItem函数,具体见下一篇吧。