CListBox的糊涂使用

   在用到CListBox的时候,总是觉得很郁闷,这次认真的研究了一下。有两种方法,

一种是相应WM_CTLCOLOR它是最简单,最方便的,相关代码如下:

HBRUSH br=::CreateSolidBrush (RGB(0,255,255)); if(pWnd->GetDlgCtrlID ()==IDC_LIST3) { pDC->SetTextColor (RGB(255,0,0)); pDC->SelectObject (&font); pDC->SetBkColor (RGB(0,255,255)); return br; }

 

第二种方法是我们自己写个,重新写void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)函数.

因为CListBox不支持OWNER DRAW,因此要增加String,要自己绘制。

一定要记住:CListBox的属性要这样设置,ower draw:选fixed。勾选下面的has string.

相关代码如下:

void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC dc; dc.Attach(lpDrawItemStruct->hDC); COLORREF clrItemText; CRect rect; switch ( lpDrawItemStruct->itemAction ) { case ODA_SELECT: case ODA_DRAWENTIRE: if ( lpDrawItemStruct->itemState & ODS_SELECTED ) { clrItemText = GetSysColor( COLOR_HIGHLIGHTTEXT ); FillRect( lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,(HBRUSH)(COLOR_ACTIVECAPTION+1)); } else { clrItemText = GetSysColor( COLOR_WINDOWTEXT ); FillRect( lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,(HBRUSH)(COLOR_WINDOW+1) ); } break; default: break; } DrawFocusRect(lpDrawItemStruct->hDC ,&lpDrawItemStruct->rcItem); CRect rectFull = lpDrawItemStruct->rcItem; rect = rectFull; rect.left = rect.Width(); CRect rect2 = rectFull; rect2.right = rect.left - 1; UINT nIndex = lpDrawItemStruct->itemID; if (nIndex != (UINT) -1) { CFont font, *pOldFont; font.CreatePointFont (100,_T("楷体")); pOldFont=dc.SelectObject(&font); CString strName; this->GetText(nIndex,strName); dc.SetBkMode(TRANSPARENT); dc.SetTextColor (RGB(255,0,0)); dc.SetBkColor (RGB(0,255,255)); dc.DrawText(strName,CRect(rect2.left+3,rect2.top+3, rect2.right-3,rect2.bottom+3), DT_LEFT | DT_SINGLELINE); dc.SelectObject (pOldFont); font.DeleteObject (); } dc.Detach(); }


 

你可能感兴趣的:(list,String)