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?
 
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.

0
 
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??

0
 
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.

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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.  
0
 
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).
0
 
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.
0
 
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.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.