初始化树控件
VOID CScale3DCUserManageView::InitTree() { TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("全部用户"); m_treeRoot = m_TreeUser.InsertItem(&tvInsert); //添加根节点 tvInsert.hParent = m_treeRoot; tvInsert.hInsertAfter = TVI_LAST; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("超级管理员"); m_itemSuperUser = m_TreeUser.InsertItem(&tvInsert); m_TreeUser.SetItemData(m_itemSuperUser, 0);//超级管理员节点 tvInsert.hParent = m_treeRoot; tvInsert.hInsertAfter = TVI_LAST; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("管理员"); m_itemAmdin = m_TreeUser.InsertItem(&tvInsert); m_TreeUser.SetItemData(m_itemAmdin, 0);//管理员节点 tvInsert.hParent = m_treeRoot; tvInsert.hInsertAfter = TVI_LAST; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = _T("普通用户"); m_itemUser = m_TreeUser.InsertItem(&tvInsert); m_TreeUser.SetItemData(m_itemUser, 0);//超级管理员节点 }
点击树控件节点,获取节点信息
void CScale3DCUserManageView::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CPoint pt; UINT nFlags; GetCursorPos(&pt); ::ScreenToClient(m_TreeUser.m_hWnd, &pt); HTREEITEM hItemSelect = m_TreeUser.HitTest(pt, &nFlags); CString sMsg= m_TreeUser.GetItemText(hItemSelect); INT nData = m_TreeUser.GetItemData(hItemSelect); if (1 == nData) { for (INT i=0;i<m_stAllUserInfo.size();i++) { if (sMsg == m_stAllUserInfo[i].szName) { m_EditUserName.SetWindowText(sMsg); m_editPassword1.SetWindowText(m_stAllUserInfo[i].szPassWord); m_editPassword2.SetWindowText(m_stAllUserInfo[i].szPassWord); m_comLimits.SetCurSel(m_stAllUserInfo[i].nIdentity-1); } } } if (hItemSelect != NULL) { if (m_TreeUser.ItemHasChildren(hItemSelect)) { m_TreeUser.Expand(hItemSelect, TVE_EXPAND); } } *pResult = 0; }
创建CImageList给CTreeCtrl控件使用,如下:
void CScale3DCDeviceManageView::CreateImageList() { m_imgList.Create(35, 35, ILC_COLOR32 | ILC_MASK, 0, 2); //0.系统 CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP6); m_imgList.Add(&bmp,RGB(0xff,0xff,0xff)); //1.秤 bmp.DeleteObject(); bmp.LoadBitmap(IDB_BITMAP2); m_imgList.Add(&bmp, RGB(0xf1, 0xf7, 0xff)); //2.仪表 bmp.DeleteObject(); bmp.LoadBitmap(IDB_BITMAP7); m_imgList.Add(&bmp, RGB(0xf1, 0xf7, 0xff)); //3.print bmp.DeleteObject(); bmp.LoadBitmap(IDB_BITMAP8); m_imgList.Add(&bmp, RGB(0xf1, 0xf7, 0xff)); m_treeScale.SetImageList(&m_imgList, TVSIL_NORMAL);//绑定图标集合 }
为指定节点设置图标,如下:
m_treeScale.SetItemImage(m_treeItemFirstScale, 1, 1);
展开所有节点
VOID CScale3DCBillDesignView::MyExpandTree(HTREEITEM hTreeItem) {//展开节点 if (!m_treeBillFile.ItemHasChildren(hTreeItem)) { return; } HTREEITEM hNextItem = m_treeBillFile.GetChildItem(hTreeItem); while (hNextItem != NULL) { MyExpandTree(hNextItem); hNextItem = m_treeBillFile.GetNextItem(hNextItem, TVGN_NEXT); } m_treeBillFile.Expand(hTreeItem, TVE_EXPAND); }
点击时某项时会闪烁
m_treeCtrl.SetExtendedStyle(TVS_EX_DOUBLEBUFFER, TVS_EX_DOUBLEBUFFER);