Solved

dynamically change background color for certain cells in the CListCtrl using report style

Posted on 2008-10-23
2
1,528 Views
Last Modified: 2013-11-20
I use CListCtrl with report style to list some text with grids.
I would like to change the background color for some cells.
For example, if the user push button A, the background for the cell at row 3 and col 4 will be changed to yellow. And if the user push button B, the the background for the cell at row 3 and col 4 will be changed to red. I have been searched the internet for good solutions but they are all too complicated for me. Currently, I use CustomDraw below. However, it will change the background clolor for the "whole" row, which is not what I really want. In the code, indexVector is an vector variable in the Mydialog object used for indicating which row needs to change the background.
I would like to have a function that I can pass in the row and col and it change the background for that cell for me in the CListCtrl.
Thanks very much for any comments.

void Mydialog::OnCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	//SAMPLE: for this notification, the structure is actually a
	// NMLVCUSTOMDRAW that tells you what's going on with the custom
	// draw action. So, we'll need to cast the generic pNMHDR pointer.
	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
	//SAMPLE: The first notification we'll receive is CDDS_PREPAINT,
	// right at the beginning of the paint cycle. We'll want to return
	// item-specific notifications so we can deal with things the right
	// way.
	if (lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	// otherwise, we might be really getting a notification about
	// painting something.
	else if (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
	// Because we returned CDRF_NOTIFYITEMDRAW in response to
    // CDDS_PREPAINT, CDDS_ITEMPREPAINT is sent when the control is
    // about to paint an item.
	{
		if (indexVector.at(lplvcd->nmcd.dwItemSpec) == 1 )
		{
            lplvcd->clrText = RGB(255, 0, 0);
            lplvcd->clrTextBk = RGB(0, 255, 255);
            *pResult = CDRF_NEWFONT;
		}
		else
		{
		// no change; tell the control to do the default
			*pResult = CDRF_DODEFAULT;
        }
	}
	else
	{
		*pResult = 0;
	}
}

Open in new window

0
Comment
Question by:lily1102
2 Comments
 
LVL 3

Accepted Solution

by:
Naveen_R earned 500 total points
ID: 22793163
Currently in your code, you are handling CDDS_ITEMPREPAINT to draw the entrie row. But if you return CDRF_NOTIFYSUBITEMDRAW as a response to the CDDS_ITEMPREPAINT , the custome draw will get called before each column is draw. Below is the sample code. try it.
void Mydialog::OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)
{
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
    *pResult = CDRF_DODEFAULT;
    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )	
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT )
    {
         *pResult = CDRF_NOTIFYSUBITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))	
    {
        if( indexVector.at(pLVCD->nmcd.dwItemSpec) == 1 && 
            ( pLVCD->iSubItem == 3 || pLVCD->iSubItem == 4 ))
        {
          // All conditions met, chnage the color
           pLVCD->clrTextBk = RGB(0, 255, 255);
           pLVCD->clrText = RGB(255, 0, 0);
        }
        else
        {
           // Default color
           pLVCD->clrTextBk = RGB(255,255,255);
           pLVCD->clrText = RGB(0,0,0);
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:lily1102
ID: 31509327
Thanks so much for the wonderful solution that you provided. Super!!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 54
maxBlock challenge 30 146
Message not shown 5 87
moto 4g phone enable finger print 4 48
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question