HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here if(nCtlColor == CTLCOLOR_LISTBOX) { if(flag) pDC->SetTextColor(RGB(255,0,0)); else pDC->SetTextColor(RGB(0,255,0)); } // TODO: Return a different brush if the default is not desired return hbr; }
继承CListBox类,重载DrawItem()函数,如下:void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX); LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData; ASSERT(lpszText != NULL); CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); // Save these value to restore them when done drawing. COLORREF crOldTextColor = pDC->GetTextColor(); COLORREF crOldBkColor = pDC->GetBkColor(); if( ! m_bValuable[lpDrawItemStruct->itemID] ) { pDC->SetTextColor(RGB(128, 128, 128)); pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); pDC->FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT)); } // If this item is selected, set the background color // and the text color to appropriate values. Also, erase // rect by filling it with the background color. if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) { if( m_bValuable[lpDrawItemStruct->itemID] ) { pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT)); pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); pDC->FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT)); } else { pDC->SetTextColor(RGB(200, 100, 200)); pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); pDC->FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT)); } } else pDC->FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor); // Draw the text. pDC->DrawText( lpszText, strlen(lpszText), &lpDrawItemStruct->rcItem, DT_LEFT|DT_SINGLELINE|DT_VCENTER); // Reset the background color and the text color back to their // original values. pDC->SetTextColor(crOldTextColor); pDC->SetBkColor(crOldBkColor);// dc.Detach(); }
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor == CTLCOLOR_LISTBOX)
{
if(flag)
pDC->SetTextColor(RGB(255,0,0));
else
pDC->SetTextColor(RGB(0,255,0));
} // TODO: Return a different brush if the default is not desired
return hbr;
}
{
// TODO: Add your code to draw the specified item ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData;
ASSERT(lpszText != NULL); CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); // Save these value to restore them when done drawing.
COLORREF crOldTextColor = pDC->GetTextColor();
COLORREF crOldBkColor = pDC->GetBkColor(); if( ! m_bValuable[lpDrawItemStruct->itemID] )
{
pDC->SetTextColor(RGB(128, 128, 128));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT));
} // If this item is selected, set the background color
// and the text color to appropriate values. Also, erase
// rect by filling it with the background color.
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
if( m_bValuable[lpDrawItemStruct->itemID] )
{
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->FillSolidRect(&lpDrawItemStruct->rcItem,
::GetSysColor(COLOR_HIGHLIGHT));
}
else
{
pDC->SetTextColor(RGB(200, 100, 200));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT));
}
}
else
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor); // Draw the text.
pDC->DrawText(
lpszText,
strlen(lpszText),
&lpDrawItemStruct->rcItem,
DT_LEFT|DT_SINGLELINE|DT_VCENTER); // Reset the background color and the text color back to their
// original values.
pDC->SetTextColor(crOldTextColor);
pDC->SetBkColor(crOldBkColor);// dc.Detach();
}
zhucde(【風間苍月】)(MVP)::是动态过程中的 ,不是静止的,
在滚动过程中,改变某一行的背景色或字体颜色。
if(flag) to be Red;
if(!flag) to be Green;改变某一行的背景色或字体颜色
up
up
up