CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2006
    Posts
    19

    Question SetItemImage() does not work on a CTreeCtrl object

    Hello everybody. In a small dialog-based program of mine I am using a CTreeCtrl with a variable amount of child items. Now I want some items to have an icon besides the text which reflects the item state (the icons should not depend on whether the tree is expanded or not!). From what I read I should be using state based images in that case. "Normal" icons work well, but there seems to be something wrong with state based icons - they are not showing up at all.

    The code that I got up to now:

    Code:
    BOOL CMyClassDlg::OnInitDialog()
    {
    	//...
    
    	m_ImageList.Create(IDB_ICONS, 10, 1, RGB(255,255,255));
    	m_tcDeviceList.SetImageList(&m_ImageList, TVSIL_STATE);
    	//m_tcDeviceList.SetImageList(&m_ImageList, TVSIL_NORMAL);
    	
    	m_hTreeRoot = m_tcDeviceList.InsertItem(L"Root", TVI_ROOT);
    	m_tcDeviceList.SetItemData(m_hTreeRoot, IDR_MENU_ROOT);
    	m_tcDeviceList.SetItemImage(m_hTreeRoot, 1, 1);
    	
    	HTREEITEM item;
    	item = m_tcDeviceList.InsertItem(L"Child", m_hTreeRoot);
    	m_tcDeviceList.SetItemData(item, IDR_MENU_CHILD);
    	m_tcDeviceList.SetItemImage(item, 0, 0);
    
    	//...
    }
    IDB_ICONS is a bitmap of 30x10 pixels in size (three 10x10 icons). What am I missing? Do I have to redraw the CTreeCtrl somehow to make the images show up?

    Thanks in advance.
    Old C programmers never die. They're just cast into void.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: SetItemImage() does not work on a CTreeCtrl object

    Hi!

    Try individual icons rather than the bitmap and see if it makes a difference.

    Example code:
    Code:
    	ImageListTree.Create(16,16,ILC_COLOR4,0,1);
    	ImageListTree.Add(AfxGetApp()->LoadIcon(IDI_CLOSEDFOLDER));
    	ImageListTree.Add(AfxGetApp()->LoadIcon(IDI_OPENFOLDER));
    	ImageListTree.SetBkColor(ImageListTree.GetBkColor());
    	TreeCtrl.SetImageList(&ImageListTree,LVSIL_NORMAL);
    When you use SetItemImage, simply pass the indexes of the images you want to use.
    The default image indexes is 0 for non-selected and 1 for selected I think.
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    May 2006
    Posts
    19

    Re: SetItemImage() does not work on a CTreeCtrl object

    Thanks for your reply. Unfortunately only empty white fields appear next to the item name (I tried using the default icons IDI_ERROR and IDI_EXCLAMATION). But anyways, TVSIL_NORMAL icons are already working, I'm only having these issues with TVSIL_STATE icons. When using TVSIL_NORMAL icons all tree elements show the same image. Once I change the icon type to TVSIL_STATE no image is shown at all in the tree (even after calling SetItemImage()).

    Maybe my understanding is wrong here but I thought creating a CImageList and assigning it to my CTreeCtrl is enough to be able to use SetItemImage() on a HTREEITEM to make one of the images in the list to show up?

    Thanks.
    Old C programmers never die. They're just cast into void.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: SetItemImage() does not work on a CTreeCtrl object

    Although this example TvwState.exe Simulates Multiple-Selection TreeView Ctrl does not use MFC, it could give some ideas...
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: SetItemImage() does not work on a CTreeCtrl object

    Nobody cares how it works as long as it works

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: SetItemImage() does not work on a CTreeCtrl object

    Quote Originally Posted by zerver View Post
    Wow!
    It looks like much better!
    Victor Nijegorodov

  7. #7
    Join Date
    May 2006
    Posts
    19

    Re: SetItemImage() does not work on a CTreeCtrl object

    Quote Originally Posted by zerver View Post
    Perfect, that was the information I was looking for. Thank you!
    Old C programmers never die. They're just cast into void.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured