首页 > 代码库 > MFC树控件的使用
MFC树控件的使用
HICON hIcon[4]; // 图标句柄数组
// 加载三个图标,并将它们的句柄保存到数组
hIcon[0] = theApp.LoadIcon(IDI_ICON_TREE_CLOSE);
hIcon[1] = theApp.LoadIcon(IDI_ICON_TREE_EXPEND);
hIcon[2] = theApp.LoadIcon(IDI_ICON_VIDEO_USER_OFFLINE);
hIcon[3]=theApp.LoadIcon(IDI_ICON_VIDEO_USER_ONLINE);
hIcon[4]=theApp.LoadIcon(IDI_ICON_TREE_URING);
hIcon[5]=theApp.LoadIcon(IDI_ICON_TREE_TALKING);
// 创建图像序列CImageList对象
m_treeImageList.Create(32, 32, ILC_COLOR32, 6, 6);
// 将三个图标添加到图像序列
for (int i=0; i<6; i++)
{
m_treeImageList.Add(hIcon[i]);
}
// 为树形控件设置图像序列
m_ctrlTreeExtens.SetImageList(&m_treeImageList, TVSIL_NORMAL);
m_ctrlTreeExtens.ModifyStyle(0,TVS_HASLINES);
==========================================================================================
void CVideoShowDlg::OnNMDblclkTreeExtens(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
CString strSelLeave=m_ctrlTreeExtens.GetItemText(m_ctrlTreeExtens.GetSelectedItem());
int nIndex=strSelLeave.Find("|");
strSelLeave=strSelLeave.Mid(0,nIndex);
int iNode=atoi(strSelLeave.GetBuffer(0));
if( iNode <= 100 ) return ;
CString name, number, depart;
int stat=Dlg->m_mmMonitor.users.GetNameByExten(strSelLeave,&name,&depart,&number);
if(stat < USER_STATUS_ONLINE){
AfxMessageBox("当前用户不在线,无法进行视频通话");
return ;
}
ShowVideo(strSelLeave);
}
=========================================================================================
void CVideoShowDlg::UpdateExtenTree(){
if(!isInitedTree){
isInitedTree = TRUE;
m_ctrlTreeExtens.DeleteAllItems();
hRoot = m_ctrlTreeExtens.InsertItem(_T("设备列表"));
CString strDepName;
int strKey;
POSITION pos =Dlg->m_mmMonitor.m_mapDepartments.GetStartPosition();
while(pos)
{
Dlg->m_mmMonitor.m_mapDepartments.GetNextAssoc(pos, strKey, strDepName);
if(!strDepName.Compare("所有")){
continue;
}
HTREEITEM hCataItem; // 可表示任一分类节点的句柄
hCataItem = m_ctrlTreeExtens.InsertItem(strDepName.GetBuffer(0), 0, 1, hRoot, TVI_LAST);
m_ctrlTreeExtens.SetItemData(hCataItem, 1);
map<CString,int>::iterator it;
CString strLeaveInfo;
for(it=m_tDataMap.begin();it!=m_tDataMap.end();it++){
int stat;
CString exten, name, number, depart;
exten.Format("%s",it->first);
stat=Dlg->m_mmMonitor.users.GetNameByExten(exten,&name,&depart,&number);
if((-1 != stat ) && (it->second) && (!strDepName.Compare(depart)) && (exten.Compare(Dlg->m_mmMessage.m_strMessExten))){
HTREEITEM hArtItem;
strLeaveInfo.Empty();
strLeaveInfo.Append(exten);
strLeaveInfo.Append("|");
strLeaveInfo.Append(name);
switch(stat){
case USER_STATUS_OFFLINE:
hArtItem = m_ctrlTreeExtens.InsertItem(strLeaveInfo.GetBuffer(0), 2,2, hCataItem, TVI_LAST);
break;
case USER_STATUS_ONLINE:
hArtItem = m_ctrlTreeExtens.InsertItem(strLeaveInfo.GetBuffer(0), 3,3, hCataItem, TVI_LAST);
break;
case USER_STATUS_TALKING:
hArtItem = m_ctrlTreeExtens.InsertItem(strLeaveInfo.GetBuffer(0), 5,5, hCataItem, TVI_LAST);
break;
case USER_STATUS_CALLED_RING:
hArtItem = m_ctrlTreeExtens.InsertItem(strLeaveInfo.GetBuffer(0), 4,4, hCataItem, TVI_LAST);
break;
case USER_STATUS_CALLING_RING:
hArtItem = m_ctrlTreeExtens.InsertItem(strLeaveInfo.GetBuffer(0), 4,4, hCataItem, TVI_LAST);
break;
case USER_STATUS_HANGUP:
hArtItem = m_ctrlTreeExtens.InsertItem(strLeaveInfo.GetBuffer(0), 3,3, hCataItem, TVI_LAST);
break;
}
m_ctrlTreeExtens.SetItemData(hArtItem, 2);
}
}
}
}
}
========================================================================================
void CVideoShowDlg::SetTreeItemStatus(CString strExten,int newStatus){
HTREEITEM hTarget=FindItem(m_ctrlTreeExtens,hRoot,strExten);
if(hTarget){
switch(newStatus){
case USER_STATUS_OFFLINE:
m_ctrlTreeExtens.SetItemImage(hTarget,2,2);
break;
case USER_STATUS_ONLINE:
m_ctrlTreeExtens.SetItemImage(hTarget,3,3);
break;
case USER_STATUS_TALKING:
m_ctrlTreeExtens.SetItemImage(hTarget,5,5);
break;
case USER_STATUS_CALLED_RING:
m_ctrlTreeExtens.SetItemImage(hTarget,4,4);
break;
case USER_STATUS_CALLING_RING:
m_ctrlTreeExtens.SetItemImage(hTarget,4,4);
break;
case USER_STATUS_HANGUP:
m_ctrlTreeExtens.SetItemImage(hTarget,3,3);
break;
}
}
}
//item:待遍历树的根节点,strtext:待查找节点名称
HTREEITEM CVideoShowDlg::FindItem(CTreeCtrl &ctrlTree ,HTREEITEM item, CString strtext)
{
HTREEITEM hfind;
//空树,直接返回NULL
if(item == NULL)
return NULL;
//遍历查找
while(item!=NULL)
{
//当前节点即所需查找节点
if(ctrlTree.GetItemText(item) == strtext)
return item;
//查找当前节点的子节点
if(ctrlTree.ItemHasChildren(item))
{
item=ctrlTree.GetChildItem(item);
//递归调用查找子节点下节点
hfind = FindItem(ctrlTree,item,strtext);
if(hfind)
{
return hfind;
}
else
{
//子节点中未发现所需节点,继续查找兄弟节点
item = ctrlTree.GetNextSiblingItem(ctrlTree.GetParentItem(item));
}
}else
{
//若无子节点,继续查找兄弟节点
item = ctrlTree.GetNextSiblingItem(item);
}
}
return item;
}
========================================================================================
以上代码仅供参考。。。
本文出自 “日知其所无” 博客,请务必保留此出处http://shenyantao.blog.51cto.com/1957956/1543269