-2

I have a simple tree control, so i want to add some icons to my tree control nodes. DDX_Control(pDX, IDC_TREE1, m_TreeView);

m_TreeView.InsertItem(L"Skills");
HTREEITEM main = m_TreeView.InsertItem(L"Technical");
m_TreeView.InsertItem(L"C++", main);
m_TreeView.InsertItem(L"Java", main);
m_TreeView.InsertItem(L".Net", main);
m_TreeView.InsertItem(L"Python", main);
HTREEITEM main1 = m_TreeView.InsertItem(L"Non_Technical");
m_TreeView.InsertItem(L"Admin", main1);
m_TreeView.InsertItem(L"HR", main1); 

The above lines are to create the Tree-Control, So i want to create the icons with my nodes..Can anyone tell me the code for adding icons to tree control. Thanks in advance...

Arif
  • 3
  • 3
  • 2
    Please review the online documentation: https://learn.microsoft.com/en-us/cpp/mfc/reference/ctreectrl-class?view=vs-2019#setitemimage – Andrew Truckle Apr 16 '19 at 12:16
  • You need to create an image-list and assign it to your treeview control. You can define a normal and a state image list. – Constantine Georgiou Apr 16 '19 at 12:48
  • Thanks for response, as am new to MFC am not able to understand msdn CTreeCtrl Class document i already read that, Can you please give me a lines of code to add the icons to the tree control if is is possible!!!! – Arif Apr 16 '19 at 12:57
  • 1
    See my answer here: https://stackoverflow.com/questions/41354446/add-icon-for-specific-tree-items-in-treectreectrl-in-mfc – Andrew Truckle Apr 16 '19 at 15:23
  • 2
    Or here: https://www.codeproject.com/Articles/14703/How-to-use-32-bit-icons-in-CTreeCtrl There are many resources with examples. – Andrew Truckle Apr 16 '19 at 15:24
  • m_imageList.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 1); m_TreeView.SetImageList(&m_imageList, LVSIL_SMALL); m_imageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); when I added the lines to my code am not able to seeing the icon symbols in my output – Arif Apr 17 '19 at 11:50

1 Answers1

0

First of all you need to create CImageList object instance.

m_TreeIcons.Create(16, 16, ILC_COLOR32|ILC_MASK, 0, 1);

You can use either bitmap or icon as image source.

m_FileIcons.Add(AfxGetApp()->LoadIcon(IDI_FOLDER));
m_FileIcons.Add(AfxGetApp()->LoadIcon(IDI_FILE));

And the last step is to bind your image list with your tree:

m_Tree.SetImageList(&m_TreeIcons, LVSIL_SMALL);
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • I insert the icon then I added the lines what ever you mentioned, but in the output window am not seeing any icon symbol with my tree control. please refer , m_imageList.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 1); m_TreeView.SetImageList(&m_imageList, LVSIL_SMALL); m_imageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); – Arif Apr 17 '19 at 11:47
  • when I uploaded the icon .ico file from the resource view, it shows... can't load external resource file – Arif Apr 17 '19 at 12:50
  • You need to add/import an icon in resource editor and assign it an ID like ID_MY_ICON. External resources are not allowed – Andrew Komiagin Apr 17 '19 at 15:11
  • @Arif You need to make sure the image list is a member variable for the class. Don’t declare it local to a method else it gets destroyed. It must exist for the duration of the tree being on screen. – Andrew Truckle Apr 18 '19 at 04:26
  • @Arif You also need to set the icon index for each entry in the tree, like I directed you to in my comments already. – Andrew Truckle Apr 18 '19 at 04:29