(创建图像链表、加载位图并添加到图像链表中、创建视图控件、设置视图控件图像链表、创建要插入树视图的结构体并设置属性及获取返回的句柄作为节点继续操作。。。)
1、基本知识:
HICON hIcon[3];
int n;
m_imagelist.Create(16,16,0,8,8);
//m_imagelist.Create(32,32,0,8,8);
hIcon[0]=AfxGetApp()->LoadIcon(IDI_ICON1);
hIcon[1]=AfxGetApp()->LoadIcon(IDI_ICON2);
hIcon[2]=AfxGetApp()->LoadIcon(IDI_ICON3);
hIcon[3]=AfxGetApp()->LoadIcon(IDI_ICON4);
for(n=0;n<4;n++)
{
m_imagelist.Add(hIcon[n]);
}
CTreeCtrl * pTree=(CTreeCtrl *)GetDlgItem(IDC_TREE1);
pTree->SetImageList(&m_imagelist,TVSIL_NORMAL);
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent=NULL;
tvinsert.hInsertAfter=TVI_LAST;
tvinsert.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT;
//tvinsert.item.hItem=NULL;
tvinsert.item.hItem=TVI_ROOT;
tvinsert.item.state=0;
tvinsert.item.stateMask=0;
tvinsert.item.cchTextMax=6;
tvinsert.item.iSelectedImage=3;
tvinsert.item.cChildren=0;
tvinsert.item.lParam=0;
//第一层
tvinsert.item.pszText="学校";
tvinsert.item.iImage=0;
HTREEITEM hDad=pTree->InsertItem(&tvinsert);
//第二层
tvinsert.hParent=hDad;
tvinsert.item.pszText="计算机系";
tvinsert.item.iImage=1;
HTREEITEM hDad21=pTree->InsertItem(&tvinsert);
tvinsert.item.pszText="管理系";
HTREEITEM hDad22=pTree->InsertItem(&tvinsert);
//第三层
//第二层第一个项目的两个子项目
tvinsert.hParent=hDad21;
tvinsert.item.pszText="网络工程";
tvinsert.item.iImage=2;
HTREEITEM hDad211=pTree->InsertItem(&tvinsert);
tvinsert.item.pszText="软件工程";
HTREEITEM hDad212=pTree->InsertItem(&tvinsert);
//第二层第二个项目的两个子项目
tvinsert.hParent=hDad22;
tvinsert.item.pszText="电子商务";
tvinsert.item.iImage=2;
HTREEITEM hDad221=pTree->InsertItem(&tvinsert);
/*
CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
ASSERT(pCtrl != NULL);
// Insert a root item using the structure. We must
// initialize a TVINSERTSTRUCT structure and pass its
// address to the call.
TVINSERTSTRUCT tvInsert;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("United States");
HTREEITEM hCountry = pCtrl->InsertItem(&tvInsert);
// Insert subitems of that root. Pennsylvania is
// a state in the United States, so its item will be a child
// of the United States item. We won't set any image or states,
// so we supply only the TVIF_TEXT mask flag. This
// override provides nearly complete control over the
// insertion operation without the tedium of initializing
// a structure. If you're going to add lots of items
// to a tree, you might prefer the structure override
// as it affords you a performance win by allowing you
// to initialize some fields of the structure only once,
// outside of your insertion loop.
HTREEITEM hPA = pCtrl->InsertItem(TVIF_TEXT,
_T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL);
// Insert the "Washington" item and assure that it is
// inserted after the "Pennsylvania" item. This override is
// more appropriate for conveniently inserting items with
// images.
HTREEITEM hWA = pCtrl->InsertItem(_T("Washington"),
0, 0, hCountry, hPA);
// We'll add some cities under each of the states.
// The override used here is most appropriate
// for inserting text-only items.
pCtrl->InsertItem(_T("Pittsburgh"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Altoona"), hPA, TVI_SORT);
pCtrl->InsertItem(_T("Seattle"), hWA, TVI_SORT);
pCtrl->InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
pCtrl->InsertItem(_T("Yakima"), hWA, TVI_SORT);
*/