MFC-控件-树的实现并添加ICO图标
程序员文章站
2024-02-14 13:38:28
...
(1)为控件添加变量
public:
CTreeCtrl m_tree;
CImageList m_imgList;
(2)导入图标多个ico
BOOL CtreeDlg::OnInitDialog()
{
HTREEITEM root;//根
HTREEITEM first_sub;//一级
HTREEITEM second_sub;//二级
//创建图标
m_imgList.Create(32,32,ILC_COLOR32|ILC_MASK,4,4);
for (int i = 0; i < 8; i++)
{
HICON icon = theApp.LoadIcon(IDI_ICON1 + i);
m_imgList.Add(icon);
}
m_tree.SetImageList(&m_imgList, TVSIL_NORMAL);
//参数第一个0表示不选中用哪张图,第二个0表示选中用哪张图
root = m_tree.InsertItem(_T("Root"),0,0);
first_sub = m_tree.InsertItem(_T("First"),1,1,root);
m_tree.InsertItem(_T("1"),3,3,root);
m_tree.InsertItem(_T("2"),4,4, root);
m_tree.InsertItem(_T("3"),5,5, root);
m_tree.InsertItem(_T("4"),6,6, root);
second_sub = m_tree.InsertItem(_T("Second"),first_sub);
m_tree.InsertItem(_T("5"),7,7, first_sub);
m_tree.InsertItem(_T("6"),6,6, first_sub);
m_tree.InsertItem(_T("7"),5,5, first_sub);
m_tree.InsertItem(_T("8"),4,4, first_sub);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}