1,Inserts a new item in a tree view control.CTreeCtrl插入Item
HTREEITEM InsertItem(
LPCTSTR lpszItem,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST
HTREEITEM InsertItem(
LPCTSTR lpszItem,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST
);
例子:
void CTREE_GUI_CONTROLDlg::InitTreeControlSimple(){
HTREEITEM h_Item1;
h_Item1 = m_Tree_Simple.InsertItem(L"AlphaA", TVI_ROOT);
// h_Item1 Children
HTREEITEM h_Sub_Item1, h_Sub_Item2, h_Sub_Item3, h_Sub_Item4, h_Sub_Item5;
h_Sub_Item1 = m_Tree_Simple.InsertItem(L"AA", h_Item1);
h_Sub_Item2 = m_Tree_Simple.InsertItem(L"AB", h_Item1, h_Sub_Item1);
h_Sub_Item3 = m_Tree_Simple.InsertItem(L"AC", h_Item1, h_Sub_Item2);
h_Sub_Item4 = m_Tree_Simple.InsertItem(L"AD", h_Item1);
h_Sub_Item5 = m_Tree_Simple.InsertItem(L"AE", h_Item1, TVI_FIRST);
// h_Sub_Item5 Children
HTREEITEM h_SubSub_Item51, h_SubSub_Item52, h_SubSub_Item53;
h_SubSub_Item51 = m_Tree_Simple.InsertItem(L"AEA", h_Sub_Item5, TVI_FIRST);
h_SubSub_Item52 = m_Tree_Simple.InsertItem(L"AEB", h_Sub_Item5, h_SubSub_Item51);
h_SubSub_Item53 = m_Tree_Simple.InsertItem(L"AEC", h_Sub_Item5, h_SubSub_Item52);
// h_Item2
HTREEITEM h_Item2;
h_Item2 = m_Tree_Simple.InsertItem(L"AlphaB", TVI_ROOT, h_Item1);
// h_Item2 Children
HTREEITEM h_Sub2_Item1, h_Sub2_Item2, h_Sub2_Item3, h_Sub2_Item4, h_Sub2_Item5;
h_Sub2_Item1 = m_Tree_Simple.InsertItem(L"BA", h_Item2);
h_Sub2_Item2 = m_Tree_Simple.InsertItem(L"BB", h_Item2);
h_Sub2_Item3 = m_Tree_Simple.InsertItem(L"BC", h_Item2);
h_Sub2_Item4 = m_Tree_Simple.InsertItem(L"BD", h_Item2);
h_Sub2_Item5 = m_Tree_Simple.InsertItem(L"BE", h_Item2);
// BkColor
m_Tree_Simple.SetBkColor(RGB(250, 230, 200));
}
2,Inserts a new item in a tree view control.CTreeCtrl插入Item,基于ImageList
HTREEITEM InsertItem(
LPCTSTR lpszItem,
int nImage,
int nSelectedImage,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST
);
例子:
void CTREE_GUI_CONTROLDlg::InitImagelist(){
// 载入图标
const int ICON_LENGTH = 3;
HICON Icon[ICON_LENGTH];
Icon[0] = AfxGetApp()->LoadIcon(IDI_ICON3);
Icon[1] = AfxGetApp()->LoadIcon(IDI_ICON2);
Icon[2] = AfxGetApp()->LoadIcon(IDI_ICON1);
//创建图像列表控件
m_Imagelist = new CImageList();
m_Imagelist->Create(16, 16, ILC_COLOR4, 7, 7);
m_Imagelist->SetBkColor(RGB(255, 255, 255));
for (int n = 0; n<ICON_LENGTH; ++n){
//把图标载入图像列表控件
m_Imagelist->Add(Icon[n]);
}
}
void CTREE_GUI_CONTROLDlg::InitTreeControlColor(){
//为Tree设置一个图像列表,使TreeControl控件的节点显示不同的图标
m_Tree_Color.SetImageList(m_Imagelist, TVSIL_NORMAL);
HTREEITEM h_Item1;
h_Item1 = m_Tree_Color.InsertItem(L"AlphaA", 0, 1, TVI_ROOT);
// h_Item1 Children
HTREEITEM h_Sub_Item1, h_Sub_Item2, h_Sub_Item3, h_Sub_Item4, h_Sub_Item5;
h_Sub_Item1 = m_Tree_Color.InsertItem(L"AA", 0, 1, h_Item1);
h_Sub_Item2 = m_Tree_Color.InsertItem(L"AB", 1, 1, h_Item1, h_Sub_Item1);
h_Sub_Item3 = m_Tree_Color.Inse