CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2021
    Posts
    16

    Can't change CTreeCtrl item image

    Hi to everyone!!! Help me to change CTreeCtrl image on the fly.

    I add the image to the imagelist like this:

    Code:
    HICON    hChecked, hUnChecked;
    	// Set up the image list.
    
    	//m_imglist.Create(32, 32, ILC_COLOR16 | ILC_MASK, 4, 1);
    	m_imglist.Create(21, 21, ILC_COLOR32,1, 1);
    
    	hChecked = reinterpret_cast<HICON>(
    		::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_Check_ICO),
    			IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR));
    
    	hUnChecked = reinterpret_cast<HICON>(
    		::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_UnCheck_ICO),
    			IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR));
    	
    	
    	m_imglist.Add(hUnChecked);
    	m_imglist.Add(hUnChecked);
    	m_imglist.Add(hChecked);
    	
    	this->SetImageList(&m_imglist, TVIF_IMAGE);
    Then i am trying to change the image like this, but it doesn't work:

    Code:
    this->SetItem(this->GetRootItem(), TVIF_IMAGE, NULL, 2,2,0,0,0);
    How to change image dynamically? Help me please

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

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by al072 View Post
    ...
    Code:
    this->SetItem(this->GetRootItem(), TVIF_IMAGE, NULL, 2,2,0,0,0);
    How to change image dynamically? Help me please
    Why do you pass 2 as the index in the image list? Your image list has the size 2, thus the only valid indexes are 0 and 1.

    BTW, using SetItemImage method looks a little simpler that SetItem.
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2021
    Posts
    16

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by VictorN View Post
    Why do you pass 2 as the index in the image list? Your image list has the size 2, thus the only valid indexes are 0 and 1.

    BTW, using SetItemImage method looks a little simpler that SetItem.
    VictorN, thank you for quick reply!!! I found solution, my problem was in the following: when the custom ImageList is passed to CTreeCtrl it needs to pass 3 images for 3 different states (disabled, unchecked, checked), i passed only two images, also it needs a flag TVSIL_STATE in SetImageList.

    this code did a trick:


    Code:
    	m_imglist.Add(hUnChecked);
    	m_imglist.Add(hUnChecked);
    	m_imglist.Add(hChecked);
    	
    	this->SetImageList(&m_imglist, TVSIL_STATE);

  4. #4
    Join Date
    Jan 2021
    Posts
    16

    Re: Can't change CTreeCtrl item image

    Now i have another trouble, how to resize image properly? IF i pass values more then 21 to m_imglist.Create(21, 21, ILC_COLOR32,1, 1); , checkbox lookes like out of bounds (here attached a screenshot). What am i doing wrong?

    Attachment 35941

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

    Re: Can't change CTreeCtrl item image

    The simplest way would be "resize" your icons (or just create the icons of the size 21x21) in the image editor. And then use these 21x21 icons.
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2021
    Posts
    16

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by VictorN View Post
    The simplest way would be "resize" your icons (or just create the icons of the size 21x21) in the image editor. And then use these 21x21 icons.
    Already did it, my icons have size 32x32, but it doesnt help

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

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by al072 View Post
    Already did it, my icons have size 32x32, but it doesnt help
    You have to add a new custom (21x21) images to your icons.
    Then load them using
    Code:
    ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_UnCheck_ICO),
    			IMAGE_ICON, 21, 21, LR_DEFAULTCOLOR));
    Victor Nijegorodov

  8. #8
    Join Date
    Jan 2021
    Posts
    16

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by VictorN View Post
    You have to add a new custom (21x21) images to your icons.
    Then load them using
    Code:
    ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_UnCheck_ICO),
    			IMAGE_ICON, 21, 21, LR_DEFAULTCOLOR));
    Images 21x21 are to small and i able to load it like you show, but i need to show icon 32x32 and i am trying to load it the way you posted, but the result you can see on screeshot i provided (it is shown out of bounds).

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

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by al072 View Post
    Images 21x21 are to small and i able to load it like you show, but i need to show icon 32x32 and i am trying to load it the way you posted, but the result you can see on screeshot i provided (it is shown out of bounds).
    If you "need to show the icon 32x32" then do
    Code:
    m_imglist.Create(32, 32, ILC_COLOR32,1, 1);
    ...
    ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_UnCheck_ICO),
    			IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR));
    Victor Nijegorodov

  10. #10
    Join Date
    Jan 2021
    Posts
    16

    Re: Can't change CTreeCtrl item image

    Quote Originally Posted by VictorN View Post
    If you "need to show the icon 32x32" then do
    Code:
    m_imglist.Create(32, 32, ILC_COLOR32,1, 1);
    ...
    ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_UnCheck_ICO),
    			IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR));
    Thank you for reply! I already did the same, but the result is like you can see on screenshot(( It seems like the text lable stay small and icon overlap it
    Last edited by al072; January 17th, 2021 at 08:07 AM.

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

  12. #12
    Join Date
    Jan 2021
    Posts
    16

    Re: Can't change CTreeCtrl item image

    Thank you for the links, very helpful information!!! Finally i found the way, i don't know why, but bitmap instead the icon make a trick

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