-
January 16th, 2021, 09:16 PM
#1
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
-
January 17th, 2021, 03:58 AM
#2
Re: Can't change CTreeCtrl item image
Originally Posted by al072
...
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
-
January 17th, 2021, 04:46 AM
#3
Re: Can't change CTreeCtrl item image
Originally Posted by VictorN
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);
-
January 17th, 2021, 04:54 AM
#4
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
-
January 17th, 2021, 06:08 AM
#5
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
-
January 17th, 2021, 06:19 AM
#6
Re: Can't change CTreeCtrl item image
Originally Posted by VictorN
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
-
January 17th, 2021, 07:08 AM
#7
Re: Can't change CTreeCtrl item image
Originally Posted by al072
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
-
January 17th, 2021, 07:13 AM
#8
Re: Can't change CTreeCtrl item image
Originally Posted by VictorN
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).
-
January 17th, 2021, 07:23 AM
#9
Re: Can't change CTreeCtrl item image
Originally Posted by al072
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
-
January 17th, 2021, 08:05 AM
#10
Re: Can't change CTreeCtrl item image
Originally Posted by VictorN
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.
-
January 17th, 2021, 08:35 AM
#11
Re: Can't change CTreeCtrl item image
Victor Nijegorodov
-
January 18th, 2021, 12:53 PM
#12
Re: Can't change CTreeCtrl item image
Originally Posted by VictorN
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|