搜到一篇文章(可能讲的不全),没有做出来,不知道哪里错了,请大家帮忙看看。1. 在.H文件中定义 protected:void OnDrawColorForMyList( NMHDR* pNmHdr, LRESULT *pResult );2.在.CPP中加入消息映射ON_NOTIFY( NM_CUSTOMDRAW, IDC_LIST, OnDrawColorForMyList ) //为改变颜色添加的消息3.在.CPP中加入函数定义
//改变 m_List 控件单行的颜色
void CMyService::OnDrawColorForMyList( NMHDR *pNmHdr, LRESULT *pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNmHdr );
*pResult = CDRF_DODEFAULT;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
//设置背景色
if( nItem%2 ==0 )
{
clrNewBkColor = RGB( 240, 240, 240 ); //偶数行背景色为灰色
}
else
{
clrNewBkColor = RGB( 255, 255, 255 ); //奇数行背景色为白色
}
// pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
}
}
出处:http://blog.csdn.net/chinafe/archive/2009/01/24/3852064.aspx谢过!
//改变 m_List 控件单行的颜色
void CMyService::OnDrawColorForMyList( NMHDR *pNmHdr, LRESULT *pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNmHdr );
*pResult = CDRF_DODEFAULT;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
//设置背景色
if( nItem%2 ==0 )
{
clrNewBkColor = RGB( 240, 240, 240 ); //偶数行背景色为灰色
}
else
{
clrNewBkColor = RGB( 255, 255, 255 ); //奇数行背景色为白色
}
// pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
}
}
出处:http://blog.csdn.net/chinafe/archive/2009/01/24/3852064.aspx谢过!
我现在时间不是很多,只是想学习一下这方面的用法。
上面那位朋友在blog上写的文章看的不是很明白,所以就请大家帮忙看一下,不知道是哪个地方写错了。
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
//设置背景色
if( nItem%2 ==0 )
{
clrNewBkColor = RGB( 240, 240, 240 ); //偶数行背景色为灰色
}
else
{
clrNewBkColor = RGB( 255, 255, 255 ); //奇数行背景色为白色
}
// pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
}
//
这显示没问题的丫..你是不是只是纯COPY函数的?你还要添加声明,和捕捉事件的afx_msg void OnCustomdraw(NMHDR*, LRESULT*);
....
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, &CList::OnCustomdraw);
建议你自己通过类属性筐那里点击添加..
请问这个控件是list box还是list control控件?
界面与逻辑彻底分离的利器:DirectUI
界面开发网站:www.uipower.com
还有NM_CUSTOMDRAW是什么消息?
#define NM_CUSTOMDRAW (NM_FIRST-12)
//
// Return Value: None.
//
// Parameters : lpDIS - A long pointer to a DRAWITEMSTRUCT structure
// that contains information about the type of drawing required.
//
// Res : Called by the framework when a visual aspect of
// an owner-draw list box changes.
//
{
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(crNorm);
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);
} // DrawItem
int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb){
int nItem = AddString(lpszItem);
if (nItem >= 0)
SetItemData(nItem, rgb);
return nItem;
}
晚上结贴