// FolderTreeCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "MultiFolder.h"
#include "FolderTreeCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFolderTreeCtrl
CFolderTreeCtrl::CFolderTreeCtrl()
{
m_uFlags = 0;
}
CFolderTreeCtrl::~CFolderTreeCtrl()
{
}
BEGIN_MESSAGE_MAP(CFolderTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CFolderTreeCtrl)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFolderTreeCtrl message handlers
BOOL CFolderTreeCtrl::DisplayTree(LPCTSTR strRoot, BOOL bFiles)
{
DeleteAllItems();
m_bFiles = bFiles; // if TRUE, Display Path- and Filenames
if ( strRoot == NULL || strRoot[0] == '\0' )
{
if ( !DisplayDrives() )
return FALSE;
m_strRoot = "";
}
else
{
m_strRoot = strRoot;
if ( m_strRoot.Right(1) != '\\' )
m_strRoot += "\\";
HTREEITEM hParent = AddItem( TVI_ROOT, m_strRoot );
DisplayPath( hParent, strRoot );
}
return TRUE;
}
BOOL CFolderTreeCtrl::DisplayDrives()
{
DeleteAllItems();
char szDrives[128];
char* pDrive;
if ( !GetLogicalDriveStrings( sizeof(szDrives), szDrives ) )
{
m_strError = "Error Getting Logical DriveStrings!";
return FALSE;
}
pDrive = szDrives;
DWORD dwType;
while( *pDrive )
{
dwType = ::GetDriveType(pDrive);
if(dwType == DRIVE_FIXED)
{
HTREEITEM hParent = AddItem( TVI_ROOT, pDrive );
if ( FindSubDir( pDrive ) )
InsertItem( "", 0, 0, hParent );
}
pDrive += strlen( pDrive ) + 1;
}
return TRUE;
}
HTREEITEM CFolderTreeCtrl::AddItem(HTREEITEM hParent, LPCTSTR strPath)
{
CString strTemp = strPath;
if ( strTemp.Right(1) == "\\" )
strTemp.SetAt( strTemp.GetLength() - 1, '\0' );
if ( hParent == TVI_ROOT )
{
return InsertItem(TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT|TVIF_STATE,
(LPCTSTR)strTemp,0,1,INDEXTOSTATEIMAGEMASK(1),
TVIS_STATEIMAGEMASK,0,hParent,TVI_LAST);
}
return InsertItem(TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT|TVIF_STATE,
GetSubPath(strTemp),0,1,INDEXTOSTATEIMAGEMASK(1),
TVIS_STATEIMAGEMASK,0,hParent,TVI_LAST);
}
void CFolderTreeCtrl::DisplayPath(HTREEITEM hParent, LPCTSTR strPath)
{
CFileFind find;
CString strPathFiles = strPath;
CString strPathName;
BOOL bFind;
int nIndex = 0;
if ( strPathFiles.Right(1) != "\\" )
strPathFiles += "\\";
strPathFiles += "*.*";
bFind = find.FindFile( strPathFiles );
CWaitCursor wc;
SetRedraw( FALSE );
while ( bFind )
{
bFind = find.FindNextFile();
if ( find.IsDirectory() && !find.IsDots() )
{
HTREEITEM hItem = AddItem( hParent,find.GetFilePath());
if (FindSubDir(find.GetFilePath()))
InsertItem( "", 0, 0, hItem );
}
}
//nState: 0->无选择钮 1->没有选择 2->部分选择 3->全部选择
UINT nState = GetItemState( hParent, TVIS_STATEIMAGEMASK ) >> 12;
if(nState == 3)
SetItemState( hParent, INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK );
SetRedraw( TRUE );
}
BOOL CFolderTreeCtrl::FindSubDir( LPCTSTR strPath)
{
CFileFind find;
CString strTemp = strPath;
BOOL bFind;
if ( strTemp[strTemp.GetLength()-1] == '\\' )
strTemp += "*.*";
else
strTemp += "\\*.*";
bFind = find.FindFile( strTemp );
while ( bFind )
{
bFind = find.FindNextFile();
if ( find.IsDirectory() && !find.IsDots() )
{
return TRUE;
}
if ( !find.IsDirectory() && m_bFiles && !find.IsHidden() )
return TRUE;
}
return FALSE;
}
void CFolderTreeCtrl::ExpandItem(HTREEITEM hItem, UINT nCode)
{
CString strPath;
if ( nCode == TVE_EXPAND )
{
HTREEITEM hChild = GetChildItem( hItem );
while ( hChild )
{
DeleteItem( hChild );
hChild = GetChildItem( hItem );
}
strPath = GetFullPath( hItem );
DisplayPath( hItem, strPath );
}
}
CString CFolderTreeCtrl::GetFullPath(HTREEITEM hItem)
{
// get the Full Path of the item
CString strReturn;
CString strTemp;
HTREEITEM hParent = hItem;
strReturn = "";
while ( hParent )
{
strTemp = GetItemText( hParent );
strTemp += "\\";
strReturn = strTemp + strReturn;
hParent = GetParentItem( hParent );
}
strReturn.TrimRight( '\\' );
return strReturn;
}
LPCTSTR CFolderTreeCtrl::GetSubPath(LPCTSTR strPath)
{
//
// getting the last SubPath from a PathString
// e.g. C:\temp\readme.txt
// the result = readme.txt
static CString strTemp;
int iPos;
strTemp = strPath;
if ( strTemp.Right(1) == '\\' )
strTemp.SetAt( strTemp.GetLength() - 1, '\0' );
iPos = strTemp.ReverseFind( '\\' );
if ( iPos != -1 )
strTemp = strTemp.Mid( iPos + 1);
return (LPCTSTR)strTemp;
}
BOOL CFolderTreeCtrl::SetItemState(HTREEITEM hItem, UINT nState, UINT nStateMask, BOOL bSearch)
{
BOOL bReturn = CTreeCtrl::SetItemState( hItem, nState, nStateMask );
UINT iState = nState >> 12;
if(iState!=0)
{
if(bSearch) TravelChild(hItem, iState);
TravelSiblingAndParent(hItem,iState);
}
return bReturn;
}
void CFolderTreeCtrl::TravelChild(HTREEITEM hItem, int nState)
{
HTREEITEM hChildItem,hBrotherItem;
//查找子节点,没有就结束
hChildItem=GetChildItem(hItem);
if(hChildItem!=NULL)
{
//设置子节点的状态与当前节点的状态一致
CTreeCtrl::SetItemState( hChildItem, INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK );
//再递归处理子节点的子节点和兄弟节点
TravelChild(hChildItem, nState);
//处理子节点的兄弟节点和其子节点
hBrotherItem=GetNextSiblingItem(hChildItem);
while (hBrotherItem)
{
//设置子节点的兄弟节点状态与当前节点的状态一致
int nState1 = GetItemState( hBrotherItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=0)
{
CTreeCtrl::SetItemState( hBrotherItem, INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK );
}
//再递归处理子节点的兄弟节点的子节点和兄弟节点
TravelChild(hBrotherItem, nState);
hBrotherItem=GetNextSiblingItem(hBrotherItem);
}
}
}
void CFolderTreeCtrl::TravelSiblingAndParent(HTREEITEM hItem, int nState)
{
HTREEITEM hNextSiblingItem,hPrevSiblingItem,hParentItem;
//查找父节点,没有就结束
hParentItem=GetParentItem(hItem);
if(hParentItem!=NULL)
{
int nState1=nState;//设初始值,防止没有兄弟节点时出错
//查找当前节点下面的兄弟节点的状态
hNextSiblingItem=GetNextSiblingItem(hItem);
while(hNextSiblingItem!=NULL)
{
nState1 = GetItemState( hNextSiblingItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=nState && nState1!=0) break;
else hNextSiblingItem=GetNextSiblingItem(hNextSiblingItem);
}
if(nState1==nState)
{
//查找当前节点上面的兄弟节点的状态
hPrevSiblingItem=GetPrevSiblingItem(hItem);
while(hPrevSiblingItem!=NULL)
{
nState1 = GetItemState( hPrevSiblingItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=nState && nState1!=0) break;
else hPrevSiblingItem=GetPrevSiblingItem(hPrevSiblingItem);
}
}
if(nState1==nState || nState1==0)
{
nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=0)
{
//如果状态一致,则父节点的状态与当前节点的状态一致
CTreeCtrl::SetItemState( hParentItem, INDEXTOSTATEIMAGEMASK(nState), TVIS_STATEIMAGEMASK );
}
//再递归处理父节点的兄弟节点和其父节点
TravelSiblingAndParent(hParentItem,nState);
}
else
{
//状态不一致,则当前节点的父节点、父节点的父节点……状态均为第三态
hParentItem=GetParentItem(hItem);
while(hParentItem!=NULL)
{
nState1 = GetItemState( hParentItem, TVIS_STATEIMAGEMASK ) >> 12;
if(nState1!=0)
{
CTreeCtrl::SetItemState( hParentItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEM
三态树控件 VC++ 类
4星 · 超过85%的资源 需积分: 10 138 浏览量
2008-12-14
00:56:13
上传
评论 1
收藏 4KB RAR 举报
guofm001
- 粉丝: 0
- 资源: 6
最新资源
- IPv4-HEADER-02测试数据
- tensorflow-2.6.2-cp38-cp38-manylinux2010-x86-64.whl
- tensorflow-2.6.2-cp37-cp37m-win-amd64.whl
- RV180-Firmware-1.0.5.4.img
- 粒子群算法.docx 粒子群算法(Particle Swarm Optimization,PSO)是一种优化算法,受到鸟群或鱼
- asp代码ASP基于WEB楼宇专业网站毕业设计(源代码+论文)
- 九宫重排.docx 验证重排后的九宫格是否满足所有条件,包括每行、每列和每个子区域的目标和值相等,以及是否包含1到9的所有数字
- asp代码ASP基于WEB聊天室的设计与实现(源代码+论文+任务书)
- 质数的判断条件.docx
- android-ocr-master,android-ocr-master
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页