可编辑多行列表框






4.25/5 (18投票s)
2003年3月26日

132806

2537
一个提供多行支持的ListBox控件。
引言
如果你看一下快照并运行.exe文件,你就会明白我这里没什么需要多说的了。我需要这个控件来支持项目编辑、插入、删除和移动功能,所以我问自己:“为什么不与大家分享呢?”
Using the Code
它非常容易使用,只需添加一个基于该类的变量即可。你像使用普通的ListBox一样使用它。
// // powered by NetControl.ro // // ............. void CListBoxXP::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CString szBuf=""; COLORREF col; if(((int)lpDrawItemStruct->itemID==edited)&&(IsWindow(hHelper))) return; { GetText(lpDrawItemStruct->itemID,szBuf); SetTextColor(lpDrawItemStruct->hDC,RGB(0,0,0)); RECT r; memcpy((void*)&r,&lpDrawItemStruct->rcItem,sizeof(RECT)); if(itm==(int)lpDrawItemStruct->itemID) { HBRUSH br; br=::CreateSolidBrush(LightenColor(
GetSysColor(COLOR_HIGHLIGHT),0.9)); ::FillRect(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,br); ::DeleteObject(br); } else { FillRect(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,
(HBRUSH)GetStockObject(WHITE_BRUSH)); }
r.left+=5; r.top+=2; col=RGB(255,255,255); if(lpDrawItemStruct->itemState & ODS_SELECTED) { col=RGB(220,220,220); HBRUSH br; br=::CreateSolidBrush(LightenColor(
GetSysColor(COLOR_HIGHLIGHT),0.75)); ::FillRect(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,br); ::DeleteObject(br); } if(lpDrawItemStruct->itemState & ODS_FOCUS) { HBRUSH br; br = ::CreateSolidBrush(LightenColor(
GetSysColor(COLOR_HIGHLIGHT),0.5)); ::FrameRect(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,br); ::DeleteObject(br); } SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
r.left-=3; r.top-=2; if(CountLines(szBuf) > 1) DrawText(lpDrawItemStruct->hDC,szBuf,
strlen(szBuf),&r,
DT_LEFT|DT_TOP|DT_WORDBREAK); else DrawText(lpDrawItemStruct->hDC,szBuf,
strlen(szBuf),&r,
DT_LEFT|DT_VCENTER|DT_SINGLELINE); } } void CListBoxXP::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { HDC hDC; TEXTMETRIC tm; char szBuf[512]=""; ::SendMessage(this->m_hWnd, LB_GETTEXT,
(WPARAM)lpMeasureItemStruct->itemID,(LPARAM)szBuf);
hDC = ::GetDC(this->m_hWnd); CRect r; GetClientRect(r); ::GetTextMetrics(hDC, &tm); lpMeasureItemStruct->itemHeight=(tm.tmHeight)*CountLines(szBuf); ::ReleaseDC(this->m_hWnd, hDC); } // ...