Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an SDI app with a tree view in which the user must be able to click on some items to disable/enable them. I wish to change the icon when this happens. I have a bit map containing multiple 16x16 images which can be referenced for different states.

In
C#
CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
    m_bitmapState.LoadBitmap(IDB_STATE);//this is a member of type CBitMap 

    //a member of type CImageList
    m_stateImageList.Create(IDB_STATE, 16, 2, RGB (255, 255, 255));
......
}


CMyView::OnInitialUpdate()
{
CTreeCtrl& tree;
tree.SetImageList(&m_stateImageList, TVSIL_STATE);
...
}


I have tried various things to get different bitmas to be shown but nothing works
e.g.
tree.SetItemImage(hti, 0, TVIS_STATEIMAGEMASK);

tree.SetItemState(hti, 1, TVIS_STATEIMAGEMASK);


but nothing works - please could somebody tell me what I am doing wrong.

I did manage to get the checkboxes working with my own images by setting the TVS_CHECKBOXES flag, but I don't want the state shown by every tree view item - is there a way to do this but not have it shown by every tree-view item?

Many many thanks for anything that gets me a bit further with this.
Posted

The normal way of doing this is:

0) Create a bit map with 2 bitmap images per each tree item (one for selected state, one for non selected)
Define an enum that matches the order of images, for example:

enum
{
	TREE_IMAGE_SERVER,
	TREE_IMAGE_CLIENT,
	// and so on
};


1) In OnInitialUpdate do this:


	if (!m_imageList.Create(IDB_IMAGELIST_TREE_SML,16,1,RGB(255,255,255)))
	{
		// @todo - handle somehow
	}

	//
	// Now associate with image list with tree control. Not interested in return being 
	// the handle to the previous image list
	//
	(void)tree.SetImageList(&m_imageList,TVSIL_NORMAL);

2) When you insert a tree item, do this:

	tree.InsertItem(ti.m_Name, 2*TREE_IMAGE_SERVER, 2*TREE_IMAGE_SERVER+1);


It works.
 
Share this answer
 
Comments
Jackie Lloyd 30-Nov-11 2:57am    
does this just allow one tree item to be selected at a time? When I used TVSIL_NORMAL it set the two images to be used for unselected and selected but selecting one item would deselect another item. I want the user to be able to select as many tree items as they like.
michaelmel 30-Nov-11 18:22pm    
As posted, the solution would allow only one item to be selected at a time. However, this is not directly related to how you do selected and unselected images. I never needed to have more than one item selected in a tree control as it is rather rare user interface style. But sounds you already worked it out, well done.
Try handling clicks (NM_CLICK notification) and possibly keyhits (TVN_KEYDOWN notification) in the tree and use SetItemImage[^] to change the image according to your needs. I belive with the state images you can only set what image to display next to the tree item when it is selected or when it is not selected, and a tree control is single-select.
 
Share this answer
 
I think you may need to add your bitmap into your image list with a function call of the form:
C++
ImageList_Add(hImageList, hBitmap, NULL);  // Win32 macro

I'm not sure what the MFC equivalent is but it should be easy to find.
 
Share this answer
 
Comments
Jackie Lloyd 29-Nov-11 9:22am    
Thankyou - but I don't think this is the problem as I think this line:
m_stateImageList.Create(IDB_STATE, 16, 2, RGB (255, 255, 255));

is equivalent to:
m_stateImageList.Create(16,16, ILC_COLOR32, 1, 1);
m_stateImageList.Add(&m_bitmapState, RGB(255, 0, 255));
http://msdn.microsoft.com/en-US/library/7w95665f(v=VS.80).aspx[^]

Use this link to know about tree class members and various flags
 
Share this answer
 
v2
Thanks to you all for your help, I did a combination of the solutions and got it to work.
 
Share this answer
 
Comments
Albert Holguin 30-Nov-11 14:32pm    
Not a solution, don't post as a solution.

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