Colored TreeView Items

Environment: MFC 4.0, MSVC 4; Win95

Hi,
I need a TreeView that displays text and an associated color as text background or as icon.
The associated colors are not predefined, but can be selected with a CColorDialog.
This seems a problem that turns up every day - but I found no solution in MFC.
I tied:
1) Using CImageList  does  not seem to make sense, since I don’t know the colors selected in advance and CImageList::SetBkColor seems to change the back ground color for all images in the list.
2) I_IMAGECALLBACK seems to call the parent of my TreeView, but I want to handle it all in TreeView.

any help is welcomed

Edgar
EHaimerlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
re: 2) above, use a message reflector instead, ie:

ON_NOTIFY_REFLECT ( LVN_GETDISPINFO, OnGetdispinfo )

Then you can handle the requests generated by I_IMAGECALLBACK within your tree control class.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EHaimerlAuthor Commented:
OK - now I am in OnGetdispinfo. But how do I change the background color for a single item in the TreeView?

I tried this:

void CAutoTaxView::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult)
{      
      TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
      // TODO: Add your control notification handler code here

      // set new BkgColor - THIS DOES NOT WORK !!
      SetBkColor( ::GetDC( pNMHDR->hwndFrom), RGB (255,0, 0));
      *pResult = 0;
}

Seems as if something's not OK with pNMHDR->hwndFrom. I suppose hwndFrom belongs to one single textlable, is this right? Why can I not get the DC and define the background color??

RONSLOWCommented:
The won't work - GetDC won't necessarily return the same DC as the control is using for drawing.

What I assumed you would do is check the pTVDispInfo->item.mask bits to dertermine that is was actually an image you wanted, and change the image for this item to be the appropriate color.

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

EHaimerlAuthor Commented:
It tried to find some way around this problem but it seems, that painting the background for each TreeView item separatly does not work. So I am trying the first method now: Use CImageList, fill the list with the colors I need and attach it to the Tree Items. But no success. I find no way to create bitmaps from RGB information only without a hDC.  
RONSLOWCommented:
Simply create a compatible DC and select your desitnation bitmap (image list) into it then use PatBlt to fill it, or just use std GDI calls to fill a rectangle with color(s).
EHaimerlAuthor Commented:
Thats what I tried (beside many other things): But I cannot change the Bitmaps in m_imageList.
I  load 1 defaultBitmap 8 times into m_imageList  in CImageListView() constructor.
Here I try to change the images but nothing happens.
The defautl image is printed (m_imageList.draw) without any changes.

int CImageListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{   int i;
     CBrush Brush;
     CBitmap Bitmap;
     CSize Size(32,32);  
     if (CView::OnCreate(lpCreateStruct) == -1)
       return -1;
    // Simply create a compatible DC and select your
    //desitnation bitmap   (image-list) into it
    CDC dcMem;
    dcMem.CreateCompatibleDC ( GetDC() );
    dcMem.DPtoLP(&Size);
    for (i=0; i < 8; i++)
    {   dcMem.SelectObject ( m_imageList.ExtractIcon(i) ) ;
        // then use PatBlt to fill it, or
       //    just use std GDI calls to fill a rectangle with color(s).
      dcMem.FillSolidRect ( 0, 0, Size.cx, Size.cy, ( RGB ( 255, 250, 0 ) ) );
}

/* Brush.CreateSolidBrush(RGB (255,0,120) );
for (i=0; i < 8; i++)
{dcMem.SelectObject ( Brush );
dcMem.SelectObject ( m_imageList.ExtractIcon(i) );
dcMem.PatBlt( 0, 0, 32, 32, PATCOPY );
}
*/
return 0;      
}


thanks for you answer.
EHaimerlAuthor Commented:
Hi Mr. Ronslow,I am waiting for an answer or at least a good hint ...I tried a lot of differnt things in the last days, no success. The problem seems to be, that you cannot change the bitmaps in the imageList as you would do with ordinary bitmaps?If you have no solution be so kind an hand the problem over to another expert.thanks for your response.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.