Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CListCtrl and Colors

Status
Not open for further replies.

Zyrenthian

Programmer
Mar 30, 2001
1,440
US
I know how to set the text color of a CListCtrl to a single color but is there a way to have multiple colors. For simplicity... lets say i wanted a Traffic Light control and i wanted stop to be red, slow down to be yellow and go to be green. Is this possible????


Matt
 
I have it partially working. When I insert the item data, I also get a pointer to the DC, and use pDC to SetTextColor of the item and then DrawText. I see a quick flash of the correct colors but then it goes back to just black. I assume there is some sort of refresh that is called. I need to know that function because i think when it does a redraw it resets it to black. In the item data of the item is the COLORREF for its particular color so adjusting it will not be a problem. Does anyone know what function I need to over-ride. I assume it is an

afxmsg <type> On<something>(...) function


Matt
 

I had the same problems when changing the background color for individual rows. You must over ride DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct). I'll post and example. What I did for the color was add a COLORREF array. Each time I added an item I made the colorRefArray[itemIndex] = colorwanted. In DrawItem I made the row color = colorRefArray[lpDrawItemStruct->itemID}. Read about DrawItem and it will make more sense. I did this for background color but you can do the same for text color.

Here's some code to help you out. It's only part of my CSumView class so you can't compile and run it but it will give you some ideas of what DrawItem is all about.


void CSumView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CListCtrl& ListCtrl=GetListCtrl();
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem(lpDrawItemStruct->rcItem);
UINT uiFlags = ILD_TRANSPARENT;
int nItem = lpDrawItemStruct->itemID;
BOOL bFocus = (GetFocus() == this);
COLORREF clrTextSave, clrBkSave;
COLORREF clrImage = m_clrBkgnd;
static _TCHAR szBuff[MAX_PATH];
LPCTSTR pszText;

// get item data
LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
lvi.iItem = nItem;
lvi.iSubItem = 0;
lvi.pszText = szBuff;
lvi.cchTextMax = sizeof(szBuff);
lvi.stateMask = 0xFFFF; // get all state flags
ListCtrl.GetItem(&lvi);

// check if item selected
BOOL bSelected = (bFocus || (GetStyle() & LVS_SHOWSELALWAYS)) && lvi.state & LVIS_SELECTED;
bSelected = bSelected || (lvi.state & LVIS_DROPHILITED);
bSelected = FALSE;

// get window sizes
CRect rcAllLabels;
ListCtrl.GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS);//LVIR_BOUNDS

CRect rcLabel;
ListCtrl.GetItemRect(nItem, rcLabel, LVIR_LABEL);

rcAllLabels.left = rcLabel.left;

if (m_bClientWidthSel && rcAllLabels.right<m_cxClient)
rcAllLabels.right = m_cxClient;

clrBkSave = pDC->SetBkColor(ColorArray[nItem] );
pDC->FillRect(rcAllLabels, &CBrush(ColorArray[nItem] ));

// draw item label
ListCtrl.GetItemRect(nItem, rcItem, LVIR_LABEL);
rcItem.right -= m_cxStateImageOffset;

pszText = MakeShortString(pDC, szBuff,
rcItem.right-rcItem.left, 2*OFFSET_FIRST);

rcLabel = rcItem;
rcLabel.left += OFFSET_FIRST;
rcLabel.right -= OFFSET_FIRST;

pDC->DrawText(pszText,-1,rcLabel,DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);

// draw labels for extra columns
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH;

for(int nColumn = 1; ListCtrl.GetColumn(nColumn, &lvc); nColumn++)
{
rcItem.left = rcItem.right;
rcItem.right += lvc.cx;

int nRetLen = ListCtrl.GetItemText(nItem, nColumn,
szBuff, sizeof(szBuff));
if (nRetLen == 0)
continue;

pszText = MakeShortString(pDC, szBuff,
rcItem.right - rcItem.left, 2*OFFSET_OTHER);

UINT nJustify = DT_LEFT;

if(pszText == szBuff)
{
switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
{
case LVCFMT_RIGHT:
nJustify = DT_RIGHT;
break;
case LVCFMT_CENTER:
nJustify = DT_CENTER;
break;
default:
break;
}
}
rcLabel = rcItem;
rcLabel.left += OFFSET_OTHER;
rcLabel.right -= OFFSET_OTHER;

pDC->DrawText(pszText, -1, rcLabel,
nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);
}
}

void CSumView::setRowColor(int iIndex, COLORREF Color)
{
ColorArray[iIndex] = Color;
RedrawWindow();
}

 
Thanx! I found something like this as msdn. What I failed to notice was YOU MUST SET THE CONTROL TO OWNER DRAW OMG I guess that is what I get for programming at work all day and then coming home and dabbling on my side project hehe. Once it is fully tested i will post my code for text color changing. Thanx for telling me to read it again.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top