CTreeCtrl二三

一:选中父节点下的子节点

Step 1:

 HTREEITEM hParItem = m_ctrlTreeXzq.GetSelectedItem();
 

 HTREEITEM hRoot = m_ctrlTreeXzq.GetRootItem(); 
 SetAllChildrenCheck(hRoot,FALSE);//取消前面选中的
 SetAllChildrenCheck(hParItem,TRUE);

 

//定义:CArray m_SelectedItemAry;

 

Step 2:

void  SetAllChildrenCheck(HTREEITEM hChildItem,BOOL bCheck)
{

    if( hChildItem == NULL) return;

    CString sText;
    m_ctrlTree.SetCheck(hChildItem,bCheck);
    if( bCheck)
   {
      m_ctrlTree.Expand(hChildItem,TVE_EXPAND);
      sText = m_ctrlTree.GetItemText(hChildItem);
      m_SelectedItemAry.Add(hChildItem);

   }
 
    HTREEITEM hChildCircle = NULL;

   hChildCircle = m_ctrlTree.GetChildItem(hChildItem);

   while ( hChildCircle != NULL)
   {


      HTREEITEM hChildTmp = hChildCircle;
  
     SetAllSiblingsCheck(hChildTmp,bCheck);

     hChildCircle = m_ctrlTree.GetChildItem(hChildCircle);
   }

 
}

 

Step 3:

void  SetAllSiblingsCheck(HTREEITEM hSibItem,BOOL bCheck)
{

    if( hSibItem == NULL) return;

    CString sText;
    m_ctrlTree.SetCheck(hSibItem,bCheck);
    if ( bCheck)
    {
       m_ctrlTree.Expand(hSibItem,TVE_EXPAND);
       sText =  m_ctrlTree.GetItemText(hSibItem);
       m_SelectedItemAry.Add(hSibItem);
    }
 

    HTREEITEM hSibCircle = NULL;

    hSibCircle = m_ctrlTree.GetNextSiblingItem(hSibItem);
    while ( hSibCircle != NULL)
    {
       HTREEITEM hSibTmp = hSibCircle;
       SetAllChildrenCheck(hSibTmp,bCheck);
      hSibCircle = m_ctrlTree.GetNextSiblingItem(hSibCircle);
     }

}

 

 

2.根据树节点Text查找节点

Step 1:

HTREEITEM hRoot = NULL;
 HTREEITEM hFindItem = NULL;

hRoot = m_ctrlTree.GetRootItem();

CString sItemText;

hFindItem = FindChildItemByText(sItemText,hRoot);

///

Step 2:

HTREEITEM CDlgXzqToMap::FindChildItemByText(CString sItemText,HTREEITEM hCurrItem)
{

     CString sCurrText, sTmp;

     sCurrText = m_ctrlTree.GetItemText(hCurrItem);

   

     if ( sItemText == sCurrText )
         return hCurrItem;

     HTREEITEM hChildCircle = NULL;
     HTREEITEM hFindItem    = NULL;
     hChildCircle = m_ctrlTree.GetChildItem(hCurrItem);

 

     while ( hChildCircle != NULL)
     {

        sCurrText = m_ctrlTree.GetItemText(hChildCircle);

        if ( sItemText == sCurrText )
             return hChildCircle;

 

       hFindItem = FindSiblingItemByText(sItemText,hChildCircle);
       sCurrText = m_ctrlTree.GetItemText(hFindItem);

       if ( sItemText == sCurrText )
          return hFindItem;

 

      hChildCircle = m_ctrlTree.GetChildItem(hChildCircle);
   }

   return NULL;
}

Step 3:

HTREEITEM CDlgXzqToMap::FindSiblingItemByText(CString sItemText,HTREEITEM hCurrItem)
{

       CString sCurrText, sTmp;
       sTmp = sCurrText = "";

       sCurrText = m_ctrlTree.GetItemText(hCurrItem);

if ( sItemText == sCurrText )
   return hCurrItem;

 

 HTREEITEM hSibCircle  = NULL;
 HTREEITEM hFindItem   = NULL;
 HTREEITEM hSibTmp     = NULL;

 

 hSibCircle = m_ctrlTree.GetNextSiblingItem(hCurrItem);
 while ( hSibCircle != NULL)
 {

     sCurrText = m_ctrlTree.GetItemText(hSibCircle);
     if ( sItemText == sCurrText )
       return hSibCircle;

 

    hFindItem = FindChildItemByText(sItemText,hSibCircle);

    sCurrText = m_ctrlTree.GetItemText(hFindItem);
    if ( sItemText == sCurrText )
         return hFindItem;

 

  hSibCircle = m_ctrlTree.GetNextSiblingItem(hSibCircle);
 }

 return NULL;

}

 

2.让ListCtrl可编辑

http://www.pudn.com/downloads192/sourcecode/windows/control/listview/detail901597.html 下载

你可能感兴趣的:(jVC++,null)