15,980
社区成员




class CColorListBox : public CListBox
{
DECLARE_DYNAMIC(CColorListBox)
public:
CColorListBox();
virtual ~CColorListBox();
public:
int AddString(LPCTSTR lpszItem);
int AddString(LPCTSTR lpszItem, COLORREF rgb);
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//cpp
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
if ((int)lpDIS->itemID < 0)
return;
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
COLORREF crText;
CString sText;
COLORREF crNorm = (COLORREF)lpDIS->itemData; // Color information is in item data.
COLORREF crHilite = RGB(255 - GetRValue(crNorm), 255 - GetGValue(crNorm), 255 - GetBValue(crNorm));
// If item has been selected, draw the highlight rectangle using the item's color.
if ((lpDIS->itemState & ODS_SELECTED) &&
(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush brush(RGB(51, 153, 255));
pDC->FillRect(&lpDIS->rcItem, &brush);
}
// If item has been deselected, draw the rectangle using the window color.
if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
{
CBrush brush(::GetSysColor(COLOR_WINDOW));
pDC->FillRect(&lpDIS->rcItem, &brush);
}
// If item has focus, draw the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);
// If item does not have focus, redraw (erase) the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && !(lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);
// Set the background mode to TRANSPARENT to draw the text.
int nBkMode = pDC->SetBkMode(TRANSPARENT);
// If the item's color information is set, use the highlight color
// gray text color, or normal color for the text.
if (lpDIS->itemData)
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(crHilite);
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(crNorm);
}
// Else the item's color information is not set, so use the
// system colors for the text.
else
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
}
// Get and display item text.
GetText(lpDIS->itemID, sText);
CRect rect = lpDIS->rcItem;
// Setup the text format.
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
if (GetStyle() & LBS_USETABSTOPS)
nFormat |= DT_EXPANDTABS;
// Calculate the rectangle size before drawing the text.
pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
pDC->DrawText(sText, -1, &rect, nFormat);
pDC->SetTextColor(crText);
pDC->SetBkMode(nBkMode);
}
//-------------------------------------------------------------------
void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
lpMIS->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
}
//-------------------------------------------------------------------
int CColorListBox::AddString(LPCTSTR lpszItem)
{
return ((CListBox*)this)->AddString(lpszItem);
}
//-------------------------------------------------------------------
int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb)
{
int nItem = AddString(lpszItem);
if (nItem >= 0)
SetItemData(nItem, rgb);
return nItem;
}
//调用
void CCListBoxTestDlg::OnBnClickedButton1()
{
m_colorListBox.AddString("111000", RGB(0, 0, 0));
m_colorListBox.AddString("111000", RGB(128, 128, 128));
}
void CCListBoxTestDlg::OnBnClickedCheck1()
{
// TODO: 在此添加控件通知处理程序代码
my_bCheck = !my_bCheck;
m_colorListBox.EnableWindow(my_bCheck);
}
// Calculate the rectangle size before drawing the text.
pDC->SetBkMode(nBkMode);
pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
pDC->DrawText(sText, -1, &rect, nFormat);
pDC->SetTextColor(crText);