// MyTreeCtrl.cpp : 实现文件
//
#include "stdafx.h"
#include "MyTreeCtrl.h"
// CMyTreeCtrl
IMPLEMENT_DYNAMIC(CMyTreeCtrl, CTreeCtrl)
CMyTreeCtrl::CMyTreeCtrl()
{
}
CMyTreeCtrl::~CMyTreeCtrl()
{
}
BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
END_MESSAGE_MAP()
// CMyTreeCtrl 消息处理程序
void CMyTreeCtrl::ListInit(DWORD dwNewStyles)//初始化控件
{
DWORD dwStyles = GetWindowLong(this->m_hWnd, GWL_STYLE);
dwStyles |= dwNewStyles;
SetWindowLong(this->m_hWnd, GWL_STYLE, dwStyles);
//m_tree.SetItemHeight(25);
//m_tree.SetTextColor(RGB(0,0,255));
//m_tree.SetBkColor(RGB(255,255,220));
}
HTREEITEM CMyTreeCtrl::InsertRoot(TV_INSERTSTRUCT &TCItem, CString strTemp)//插入根项
{
TCItem.hParent = TVI_ROOT;
TCItem.hInsertAfter = TVI_LAST;
TCItem.item.mask = TVIF_TEXT|TVIF_PARAM;
TCItem.item.pszText = (LPSTR)(LPCSTR)strTemp;
TCItem.item.lParam = 0;
return this->InsertItem(&TCItem);
}
HTREEITEM CMyTreeCtrl::InsertParent(TV_INSERTSTRUCT TCItem, HTREEITEM hRoot, CString strTemp, int i)//插入父项
{
TCItem.hParent = hRoot;
TCItem.item.pszText = (LPSTR)(LPCSTR)strTemp;
TCItem.item.lParam = (i + 1) * 10;
return this->InsertItem(&TCItem);
}
void CMyTreeCtrl::InsertChild(TV_INSERTSTRUCT TCItem, HTREEITEM hCur, CString strTemp, int i, int j)//插入子项
{
TCItem.hParent = hCur;
TCItem.item.pszText = (LPSTR)(LPCSTR)strTemp;
TCItem.item.lParam = (i + 1) * 10 + (j + 1);
this->InsertItem(&TCItem);
}
void CMyTreeCtrl::ExpandTree(HTREEITEM hItem)//展开树中的某一项
{
this->Expand(hItem,TVE_EXPAND);
HTREEITEM hChild = this->GetChildItem(hItem);
while (hChild)
{
ExpandTree(hChild);
hChild = this->GetNextSiblingItem(hChild);
}
}
void CMyTreeCtrl::ExpandAllTree()//展开该树的所有项
{
HTREEITEM hRoot = this->GetRootItem();
ExpandTree(hRoot);
}
void CMyTreeCtrl::CollapseTree(HTREEITEM hItem)//收起树中的某一项
{
this->Expand(hItem,TVE_COLLAPSE);
HTREEITEM hChild = this->GetChildItem(hItem);
while (hChild)
{
CollapseTree(hChild);
hChild = this->GetNextSiblingItem(hChild);
}
}
void CMyTreeCtrl::CollapseAllTree()//收起该树的所有项
{
HTREEITEM hRoot = this->GetRootItem();
CollapseTree(hRoot);
}
void CMyTreeCtrl::CheckSubItems(HTREEITEM hParent,BOOL bCheck)
{
TreeView_SetCheckState(this->m_hWnd,hParent,bCheck);
HTREEITEM hItem = TreeView_GetChild(this->m_hWnd,hParent);
while(hItem)
{
CheckSubItems(hItem,bCheck);
hItem = TreeView_GetNextSibling(this->m_hWnd,hItem);
}
}
HTREEITEM CMyTreeCtrl::FindItem(HTREEITEM hItem, CString strText)//在某项及其子项中查找内容,返回匹配项的HTREEITEM,否则返回NULL
{
HTREEITEM hFind;
if(hItem == NULL)
return NULL;
while(hItem != NULL)
{
if(GetItemText(hItem) == strText)
return hItem;
if(ItemHasChildren(hItem))
{
hItem = GetChildItem(hItem);
hFind = FindItem(hItem,strText);
if(hFind)
{
return hFind;
}
else
{
hItem = GetNextSiblingItem(GetParentItem(hItem));
}
}
else
{
hItem = GetNextSiblingItem(hItem);
if(hItem == NULL)
return NULL;
}
}
return hItem;
}
HTREEITEM CMyTreeCtrl::FindItem(CString strText)//在整个树及其子项中查找文本,返回匹配项的HTREEITEM,否则返回NULL
{
HTREEITEM hRoot = this->GetRootItem();
return FindItem(hRoot,strText);
}
CString CMyTreeCtrl::GetSel()//返回选中项的文本
{
HTREEITEM hItem = this->GetSelectedItem();
if(hItem == NULL)
return _T("");
return this->GetItemText(hItem);
}
BOOL CMyTreeCtrl::SetSel(CString strText)
{
this->CollapseAllTree();
BOOL bResult = this->SelectItem(this->FindItem(strText));
this->SetFocus();
return bResult;
}