Can anyone tell me how to add icons to tree-control in MFC

701 views Asked by At

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...

1

There are 1 answers

5
Andrew Komiagin On

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);