Click to See Complete Forum and Search --> : CListCtrl
mooneymdp
October 20th, 1999, 04:14 AM
I am trying to change the text background colour in different rows in my app using CListCtrl, but if I change the first and then reset the rest of the rows to a default colour the first changes to match the default colour. I am using the code:
if (index == 0) {
ptr->SetTextBkColor(0x000000);
}
else
{
ptr->SetTextBkColor(0xffff00);
}
Does anybody know why the first index does not stay as the orignal colour?
J Diamond
October 20th, 1999, 04:42 AM
Hi,
The answer is simple (and sad, from your point of view). The method you use enable you to set the background color of *all* items (and not for a single row only).
If you want to have a different background color in different rows, you will have to work (a bit) harder. You should use the draw notification mechanism in your list control, and then set the bakcground color for the text in each item as you please.
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
//{{AFX_MSG_MAP(CListCtrlEx)
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT( NM_CUSTOMDRAW, OnCustomDraw )
END_MESSAGE_MAP()
void CListCtrlEx::OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult )
{
// Change backword color as specified
LPNMLVCUSTOMDRAW pNMLVCUSTOMDRAW = ( LPNMLVCUSTOMDRAW )pNMHDR;
switch ( pNMLVCUSTOMDRAW->nmcd.dwDrawStage )
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
if ( pNMLVCUSTOMDRAW->nmcd.dwItemSpec == 0 )
pNMLVCUSTOMDRAW->clrTextBk = 0x000000;
else
pNMLVCUSTOMDRAW->clrTextBk = 0xffff00;
break;
}
}
J
October 20th, 1999, 08:16 AM
That works if you have a class derived from CListCtrl, but what if you have a CListView class and use the embedded listctrl? How does this work then? And the listctrl is not an onwnerdrawn listctrl...
If you have a solution, please let me know.
Thankx.
vance
June 3rd, 2000, 03:04 AM
put this in your dialog message map
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_DATA, OnD)
add this function to your class declaration:
void OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult );
and then use the same function:
void CListTestDlg::OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult )
{
// Change backword color as specified
LPNMLVCUSTOMDRAW pNMLVCUSTOMDRAW = ( LPNMLVCUSTOMDRAW )pNMHDR;
switch ( pNMLVCUSTOMDRAW->nmcd.dwDrawStage )
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break; case CDDS_ITEMPREPAINT:
if ( pNMLVCUSTOMDRAW->nmcd.dwItemSpec == 0 )
pNMLVCUSTOMDRAW->clrTextBk = 0x000000;
else
pNMLVCUSTOMDRAW->clrTextBk = 0xffff00;
break;
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.