QQ登录

只需一步,快速开始

84上位机VC MFC树视图三种节点状态效果

[ 复制链接 ]

84上位机VC MFC树视图三种节点状态效果

84上位机VC MFC树视图三种节点状态效果

84上位机VC MFC树视图三种节点状态效果


功能展示

树视图节点有不同状态的复选框效果,可以让用户更多清楚的了解自己当前的操作情况,我们当前这例程就来实现三种不同状态的节点功能,效果如图;
要点提示
树状视图的复选框是通过图像状态索引绘制,我们先设计一个位图,包含视图项的各个状态,再将位图添加械CImageList中,然后调用CTreeCtrl的SetImageList()就可以设置位图状态索引;
实现功能
1.新建基于对话框的应用程序
2.派生一个自己的类class CStateTreeCtrl : publicCTreeCtrl;给类添加三个函数  voidRansackParentAndChild(HTREEITEM hItem,UINT State);
void RansackChild(HTREEITEM hItem,UINT State);
BOOL SetItemState(HTREEITEM hItem, UINT State, UINTStateMask, BOOL IsSearch=TRUE);

再添加nLButtonDown(UINTnFlags, CPoint point) 和voidCStateTreeCtrl:reSubclassWindow() 函数,下面是这五个函数的函数体
  1. void CStateTreeCtrl::PreSubclassWindow()
  2. {
  3.         ModifyStyle(0,TVS_CHECKBOXES|TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT|TVS_FULLROWSELECT,0);
  4.         CTreeCtrl::PreSubclassWindow();
  5. }
  6. void CStateTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  7. {
  8.         HTREEITEM hItemInfo =HitTest(point, &nFlags);
  9.         //TVHT_ONITEMSTATEICON表示用户定义的视图项的图标状态
  10.         if ( nFlags &TVHT_ONITEMSTATEICON )
  11.         {
  12.                 //State: 0无状态 1没有选择 2部分选择 3全部选择
  13.                 //12到15位表示视图项的图像状态索引
  14.                 UINT State = GetItemState( hItemInfo, TVIS_STATEIMAGEMASK ) >> 12;
  15.                 State=(State==3)?1:3;
  16.                 SetItemState( hItemInfo, INDEXTOSTATEIMAGEMASK(State), TVIS_STATEIMAGEMASK );
  17.         }
  18.         else
  19.                 CTreeCtrl::OnLButtonDown(nFlags, point);
  20. }
复制代码
  1. //遍历子节点
  2. void CStateTreeCtrl::RansackChild(HTREEITEM hItem, UINT State)
  3. {
  4.         HTREEITEM hChildItem,hBrotherItem;
  5.         //查找子节点
  6.         hChildItem=GetChildItem(hItem);
  7.         if(hChildItem!=NULL)
  8.         {
  9.                 //将所有子节点的状态设置与父节点相同
  10.                 CTreeCtrl::SetItemState( hChildItem, INDEXTOSTATEIMAGEMASK(State), TVIS_STATEIMAGEMASK );
  11.                 //再递归处理子节点的子节点
  12.                 RansackChild(hChildItem, State);
  13.                
  14.                 //搜索子节点的兄弟节点
  15.                 hBrotherItem=GetNextSiblingItem(hChildItem);
  16.                 while (hBrotherItem)
  17.                 {
  18.                         //设置子节点的兄弟节点状态与当前节点的状态一致
  19.                         int nState = GetItemState( hBrotherItem, TVIS_STATEIMAGEMASK ) >> 12;
  20.                         if(nState!=0)
  21.                         {
  22.                                 CTreeCtrl::SetItemState( hBrotherItem, INDEXTOSTATEIMAGEMASK(State), TVIS_STATEIMAGEMASK );
  23.                         }
  24.                         //再递归处理兄弟节点
  25.                         RansackChild(hBrotherItem, State);
  26.                         hBrotherItem=GetNextSiblingItem(hBrotherItem);
  27.                 }
  28.         }
  29. }
  30. void CStateTreeCtrl::RansackParentAndChild(HTREEITEM hItem, UINT State)
  31. {
  32.         HTREEITEM hNextItem,hPrevItem,hParentItem;
  33.         
  34.         //查找父节点,没有就结束
  35.         hParentItem=GetParentItem(hItem);
  36.         if(hParentItem!=NULL)
  37.         {
  38.                 UINT nState1=State;//设初始值,防止没有兄弟节点时出错
  39.                
  40.                 //查找当前节点的所有兄弟节点,如果所有兄弟节点状态都相同,
  41.                 //设置父节点的状态

  42.                
复制代码
  1. //查找当前节点下面的兄弟节点的状态
  2.                 hNextItem=GetNextSiblingItem(hItem);
  3.                 while(hNextItem!=NULL)
  4.                 {
  5.                         nState1 = GetItemState( hNextItem, TVIS_STATEIMAGEMASK ) >> 12;
  6.                         if(nState1!=State && nState1!=0) break;
  7.                         else hNextItem=GetNextSiblingItem(hNextItem);
  8.                 }
  9.                
  10.                 if(nState1==State)
  11.                 {
  12.                         //查找当前节点上面的兄弟节点的状态
  13.                         hPrevItem=GetPrevSiblingItem(hItem);
  14.                         while(hPrevItem!=NULL)
  15.                         {
  16.                                 nState1 = GetItemState( hPrevItem, TVIS_STATEIMAGEMASK ) >> 12;
  17.                                 if(nState1!=State && nState1!=0) break;
  18.                                 else hPrevItem=GetPrevSiblingItem
  19.                                         (hPrevItem);
  20.                         }
  21.                 }        
  22.                 if(nState1==State || nState1==0)
  23.                 {
  24.                         nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
  25.                         if(nState1!=0)
  26.                         {
  27.                                 //如果状态一致,则父节点的状态与当前节点的状态一致
  28.                                 CTreeCtrl::SetItemState( hParentItem, INDEXTOSTATEIMAGEMASK(State), TVIS_STATEIMAGEMASK );
  29.                         }
  30.                         //再递归处理父节点的兄弟节点和其父节点
  31.                         RansackParentAndChild(hParentItem,State);
  32.                 }
  33.                 else
  34.                 {
  35.                         //状态不一致,则当前节点的父节点、父节点的父节点……状态均为第三态
  36.                         hParentItem=GetParentItem(hItem);
  37.                         while(hParentItem!=NULL)
  38.                         {
  39.                                 nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
  40.                                 if(nState1!=0)
  41.                                 {
  42.                                         CTreeCtrl::SetItemState( hParentItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK );
  43.                                 }
  44.                                 hParentItem=GetParentItem(hParentItem);
  45.                         }
  46.                 }
  47.         }
  48. }
复制代码
  1. while(hParentItem!=NULL)
  2.                         {
  3.                                 nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
  4.                                 if(nState1!=0)
  5.                                 {
  6.                                         CTreeCtrl::SetItemState( hParentItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK );
  7.                                 }
  8.                                 hParentItem=GetParentItem(hParentItem);
  9.                         }
  10.                 }
  11.         }
  12. }
  13. //设置节点状态
  14. BOOL CStateTreeCtrl::SetItemState(HTREEITEM hItem, UINT State, UINT StateMask, BOOL IsSearch)
  15. {
  16.         BOOL ret=CTreeCtrl::SetItemState( hItem, State, StateMask );

  17.         UINT nState =State >> 12;
  18.         if(nState!=0)
  19.         {
  20.                 if(IsSearch)
  21.                         RansackChild(hItem, nState);
  22.                 RansackParentAndChild(hItem,nState);
  23.         }
  24.         return ret;
  25. }
复制代码
4.最后是这个自定义类的使用;主对话框中添加数着视图关联变量 CStateTreeCtrl m_Tree;添加两变量 CImageList m_ImageList; CImageList m_StateList;

5.初始化给树状视图添加数据
  1. m_ImageList.Create(IDB_BITMAP1,16,1,RGB(255,255,255));
  2.         m_StateList.Create(IDB_BITMAP2,12,1,RGB(255,255,255));
  3.         m_Tree.SetImageList(&m_ImageList,TVSIL_NORMAL);
  4.         m_Tree.SetImageList(&m_StateList,TVSIL_STATE);
  5.         TV_INSERTSTRUCT hInsert;
  6.         hInsert.hParent=NULL;
  7.         hInsert.hInsertAfter=TVI_LAST;
  8.         hInsert.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT|TVIF_STATE;
  9.         hInsert.item.hItem=NULL;
  10.         hInsert.item.state=INDEXTOSTATEIMAGEMASK( 1 );
复制代码
  1. hInsert.item.stateMask=TVIS_STATEIMAGEMASK;
  2.         hInsert.item.cchTextMax=6;
  3.         hInsert.item.iSelectedImage=1;
  4.         hInsert.item.cChildren=0;
  5.         hInsert.item.lParam=0;
  6.         hInsert.item.pszText="工控编程";
  7.         hInsert.item.iImage=0;
  8.         HTREEITEM hRoot=m_Tree.InsertItem(&hInsert);
  9.         m_Tree.SetItemState( hRoot, INDEXTOSTATEIMAGEMASK(0), TVIS_STATEIMAGEMASK );
  10.         hInsert.hParent=hRoot;
  11.         hInsert.item.iImage=0;
  12.         hInsert.item.pszText="欧姆龙PLC编程";
  13.         m_Tree.InsertItem(&hInsert);
  14.         hInsert.hParent=hRoot;
  15.         hInsert.item.pszText="三菱PLC编程";
  16.         HTREEITEM h1=m_Tree.InsertItem(&hInsert);
  17.         hInsert.hParent=hRoot;
  18.         hInsert.item.pszText="西门子PLC编程";
  19.         m_Tree.InsertItem(&hInsert);
  20.         hInsert.hParent=hRoot;
  21.         hInsert.item.pszText="松下PLC编程";
  22.         m_Tree.InsertItem(&hInsert);
  23.         hInsert.hParent=hRoot;
  24.         hInsert.item.pszText="罗克韦尔PLC编程";
  25.         m_Tree.InsertItem(&hInsert);        
  26.         hInsert.hParent=hRoot;
  27.         hInsert.item.pszText="其他编程";
  28.         HTREEITEM h2=m_Tree.InsertItem(&hInsert);
  29.         hInsert.hParent=h2;
  30.         hInsert.item.pszText="上位机VC编程";
  31.         m_Tree.InsertItem(&hInsert);
  32.     hInsert.hParent=h2;
  33.         hInsert.item.pszText="触摸屏编程";
  34.         m_Tree.InsertItem(&hInsert);
  35.     hInsert.hParent=h2;
  36.         hInsert.item.pszText="贝加莱PLC编程";
  37.         m_Tree.InsertItem(&hInsert);
  38.         
  39.     hInsert.hParent=h2;
  40.         hInsert.item.pszText="组态编程";
  41.         m_Tree.InsertItem(&hInsert);
  42. <div style="text-align: center;"><span style="line-height: 1.5;"><b><font size="4">我们来演示下实现过程</font></b></span></div>
复制代码


请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:84.上位机VC MFC树视图三种节点状态效果.rar 
文件大小:138.29 KB  售价:10金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我


  

您的支持是我们创作的动力!  

  

您可花点闲钱积分自助任意充值

  

成为VIP会员 全站资源任意下载永久更新!


回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索