从CListBox 继承一个新类

从CListBox 继承一个新类
在其中的DrawItem和MeasureItem函数里面设置修改颜色
void   CNewListBox::MeasureItem(LPMEASUREITEMSTRUCT   lpMeasureItemStruct)   

//   TODO:   Add   your   code   to   determine   the   size   of   specified   item 
int   nItem   =   lpMeasureItemStruct-> itemID; 
CPaintDC   dc(this); 
CString   sLabel; 
CRect   rcLabel; 


GetText(   nItem,   sLabel   ); 
GetItemRect(nItem,   rcLabel); 




int   itemHeight   =   dc.DrawText(   sLabel,   rcLabel,   DT_WORDBREAK   |   DT_CALCRECT   ); 
lpMeasureItemStruct-> itemHeight   =   itemHeight; 





void   CNewListBox::DrawItem(LPDRAWITEMSTRUCT   lpDrawItemStruct)   

//   TODO:   Add   your   code   to   draw   the   specified   item 
CDC*   pDC   =   CDC::FromHandle(lpDrawItemStruct-> hDC); 
//从选项数据获得背景颜色值 
COLORREF   rColor   =   (COLORREF)lpDrawItemStruct-> itemData;   //   RGB   in   item   data 


CString   sLabel; 
GetText(lpDrawItemStruct-> itemID,   sLabel); 


//   些选项被选择 
if   ((lpDrawItemStruct-> itemState   &   ODS_SELECTED)) 

// 
CBrush   colorBrush(rColor); 
CRect   colorRect   =   lpDrawItemStruct-> rcItem; 


//   绘制背景 
CBrush   labelBrush(::GetSysColor(COLOR_HIGHLIGHT)); 
CRect   labelRect   =   lpDrawItemStruct-> rcItem; 
pDC-> FillRect(&labelRect,&labelBrush); 


//   绘制文本 
COLORREF   colorTextSave; 
COLORREF   colorBkSave; 


colorTextSave   =   pDC-> SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT)); 
colorBkSave   =   pDC-> SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); 
pDC-> DrawText(   sLabel,   &lpDrawItemStruct-> rcItem,   DT_WORDBREAK   ); 


pDC-> SetTextColor(colorTextSave); 
pDC-> SetBkColor(colorBkSave); 


return; 



//   些选项未被选中 
if   (!(lpDrawItemStruct-> itemState   &   ODS_SELECTED)) 

CRect   rect   =   lpDrawItemStruct-> rcItem; 
CBrush   brush(rColor); 
pDC-> SetBkColor(rColor); 
pDC-> FillRect(&rect,&brush); 
pDC-> DrawText(   sLabel,   -1,   &lpDrawItemStruct-> rcItem,   DT_WORDBREAK   ); 


return; 




void   CNewListBox::AddItem(LPCTSTR   lpszItem,   COLORREF   color,   int   nIndex) 





int   index=InsertString(nIndex,lpszItem); 
SetItemData(index,color); 





void   CNewListBox::AddItem(CString&   sitem) 
{ CString   str=_T( " "); 
CTime   time=CTime::GetCurrentTime(); 
str=time.Format( "[   %Y-%m-%d   %I:%M   %p   ] "); 
str+= "\r\n       "+sitem; 


COLORREF   color; 
color=GetCount()%2?RGB(222,227,247):RGB(250,250,250); 
AddItem(str,color,0); 
str+= "\r\n "; 


}

你可能感兴趣的:(从CListBox 继承一个新类)