Click here to Skip to main content
15,935,259 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are lots of articles on how to set color using custom draw but whenever I try to get the current background color I always get 0xffff back. Where am I going wrong?
Posted
Comments
Jochen Arndt 18-Jul-12 3:16am    
How did you try to get the current background color for a specific row? Using CListCtrl::[GS]etBkColor() you can only get/set the color for normal (neither selected nor hot) items. Selected and hot items are colored as last step of the list control draw process.
Maybe you should us the code portion that did not work as expected.

GetBkColor() and SetBkColor() will get and set the default color for all list items. If you use custom draw to change the color of individual rows or cells, the color value is passed as argument to the drawing function and not stored anywhere. If you want to retrieve the color for specific rows later, you must store the value somewhere or implement a function that can detect the value that has been passed using the same condition as in the custom draw handler.

One option would be to use the data member of list control items. You may store a flag value in that member using SetItemData() and select the colors according to that value in the custom draw function. To determine which color has been set, just check the flag value using GetItemData().

When setting the flag value for a single row, you can use RedrawItems() to redraw only the affected row.

For example:

void CDDListCtrl::SetFlag(int nItem, DWORD dwFlag)
{
    SetItemData(nItem, dwFlag);
    RedrawItems(nItem, nItem);
}
void CDDListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
...
    // I'm not sure if cd->nmcd.lItemlParam can be used here
    DWORD dwFlag = GetItemData(cd->nmcd.dwItemSpec);
    // set color here according to flag value and sub-item
}
COLORREF CDDListCtrl::GetCellColor(int nItem, int nSubItem)
{
    DWORD dwFlag = GetItemData(nItem);
    // return color here according to flag value and sub-item
}
 
Share this answer
 
Comments
bobe001 19-Jul-12 9:58am    
Thank you, that does seem to make sense.. It does seem daft that you can set a rows color but not retrieve it though..
Hi Thank you.. Using Custon Draw I can set the background color of individual rows no problem.. But then I want to find out what the color is in a particular row to change it..I have tried all combination of getbkcolor and gettextbkcolor and just get rubbish back..

C++
void CDDListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* cd = reinterpret_cast<nmlvcustomdraw*>(pNMHDR); 

*pResult = CDRF_DODEFAULT;

if ( CDDS_PREPAINT == cd->nmcd.dwDrawStage )

  *pResult = CDRF_NOTIFYITEMDRAW;
  }
else if ( CDDS_ITEMPREPAINT == cd->nmcd.dwDrawStage )
	{
  *pResult = CDRF_NOTIFYSUBITEMDRAW;
  }
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == cd->nmcd.dwDrawStage )
  {
  	if (cd->nmcd.hdr.idFrom == IDC_PLAYLIST)
		{
		if (PlaylistInfo.m_AddDataType == ADD_TYPE_NORMAL)
			{
			if ((RegValue == SINGLE_MANUAL) || (RegValue == SINGLE_RANDOM) || (RegValue == SINGLE_FORMAT))
				{
				cd->clrTextBk = RED;
				}
			else
				{
				cd->clrTextBk = GREEN;
				}
			}
		}
	if (PlaylistInfo.m_AddDataType == ADD_TYPE_SEGUE)
		{
		if ( 1 == cd->iSubItem )
			{
		cd->clrTextBk = GetTextBkColor();
		SetTextBkColor(cd->clrTextBk);
 
Share this answer
 
v2
Comments
[no name] 18-Jul-12 12:35pm    
Is this an answer?
bobe001 18-Jul-12 13:27pm    
No sorry, wrong box, it's the problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900