mfc CTreeCtrl 添加节点 和绑定图标

1、添加控件并绑定变量

1、修改属性Line At Root为True Has Bootons改成True 使节点有加减号(展开和收缩)

3、初始化函数中如下:

TVINSERTSTRUCT tvInsert;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("Tagge Card");
HTREEITEM selTreeRoot = m_selTreeCtrl.InsertItem(&tvInsert); //添加根节点


tvInsert.hParent = selTreeRoot;
tvInsert.hInsertAfter = TVI_LAST;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("TG-550C(PCI)d");
HTREEITEM pciItem = m_selTreeCtrl.InsertItem(&tvInsert);
m_selTreeCtrl.SetItemData(pciItem,0);


tvInsert.hParent = selTreeRoot;
tvInsert.hInsertAfter = TVI_LAST;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("TG-550FCPCI)");
HTREEITEM cpciItem = m_selTreeCtrl.InsertItem(&tvInsert);
m_selTreeCtrl.SetItemData(cpciItem,1);


CString str;
CString str2;
for(int i=0;i<4;i++)
{
tvInsert.hParent = pciItem;
tvInsert.hInsertAfter = TVI_LAST;
tvInsert.item.mask = TVIF_TEXT;
str2.Format(_T("%d"),i);
str = "通道:";
str += str2;
tvInsert.item.pszText = str.GetBuffer();


HTREEITEM typeNodeName = m_selTreeCtrl.InsertItem(&tvInsert);
m_selTreeCtrl.SetItemData(typeNodeName, i);
}


for(int i=0;i<4;i++)
{
tvInsert.hParent = cpciItem;
tvInsert.hInsertAfter = TVI_LAST;
tvInsert.item.mask = TVIF_TEXT;
str2.Format(_T("%d"),i);
str = "通道:";
str += str2;
tvInsert.item.pszText = str.GetBuffer();


HTREEITEM typeNodeName = m_selTreeCtrl.InsertItem(&tvInsert);
m_selTreeCtrl.SetItemData(typeNodeName, i);
}
m_selTreeCtrl.Expand(m_selTreeCtrl.GetRootItem(),TVE_EXPAND); //根节点展开

获取选择树的那个节点

void selectPanel::OnBnClickedOk()
{
//CTreeCtrl* selTreeCtrl = (CTreeCtrl*)GetDlgItem(IDC_SELECT_TREE);
HTREEITEM   sItem = m_selTreeCtrl.GetSelectedItem();
HTREEITEM sParItem = m_selTreeCtrl.GetParentItem(sItem);
HTREEITEM sGraItem = m_selTreeCtrl.GetParentItem(sParItem);
CComboBox* hCarNo = (CComboBox*)GetDlgItem(IDC_COMBO1);


if(sGraItem == m_selTreeCtrl.GetRootItem()) //Tagge Card
{
int vCarNO = hCarNo->GetCurSel();
int typeVal = m_selTreeCtrl.GetItemData(sParItem); //获取所在的这一层的索引
int channelVal = m_selTreeCtrl.GetItemData(sItem);
int test =10;
}


}

//绑定节点

bcTreeCtrl->SetItemData(typeNodeName, 32); //typeNodeName 节点

//获取节点绑定的值
int num = bcTreeCtrl->GetItemData(typeNodeName);

二、绑定图标

1、添加图标资源

2、绑定CtreeTtrl和CImageList代码如下:

DWORD Rgb = GetSysColor(COLOR_WINDOW);

m_imageList.Create(20,20,ILC_COLOR24|iLC_MASK,0,0);//m_imageList类型CImageList

m_bm[0].LoadBitmap(资源ID1);

m_imageList.Add(&m_bm[0],Rgb);

m_bm[1].LoadBitmap(资源ID2);

m_imageList.Add(&m_bm[0],Rgb);

m_bm[2].LoadBitmap(资源ID2);

m_imageList.Add(&m_bm[0],Rgb);

m_bm[3].LoadBitmap(资源ID3);

m_imageList.Add(&m_bm[0],Rgb);

m_bm[4].LoadBitmap(资源ID4);

m_imageList.Add(&m_bm[0],Rgb);

m_bm[5].LoadBitmap(资源ID4);

m_imageList.Add(&m_bm[0],Rgb);


m_wndTree.SetImageList(&m_imageList, TVSIL_NORMAL); //m_wndTree为CtreeCtrl

3、添加图标,代码如下:

CString str = _T("设备");

HTREETEM hRoot = m_wndTree.InsertItem(str ,3,3);//添加which图标


你可能感兴趣的:(MFC)