实现 类似资源管理器功能

 

  下面是根据http://blog.csdn.net/shuilan0066/article/details/6789204算法 写的资源管理器实现类, 其中代码借鉴了网上资源,根据需要 有所改进

 

   实现效果如下:

 

   实现 类似资源管理器功能_第1张图片

  准备阶段:

  建立一个SDI  采用拆分窗口, 视图基类为CTreeView

 

  头文件:TreePathView.h

 

// TreePathView.h : CTreePathView 类的接口
//
#include "TreePathDoc.h"

#pragma once


class CTreePathView : public CTreeView
{
protected: // 仅从序列化创建
	CTreePathView();
	DECLARE_DYNCREATE(CTreePathView)
// 属性
public:
	CTreePathDoc* GetDocument() const;
// 操作
public:
// 重写
public:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);  // 更改树目录的形式 
protected:
	virtual void OnInitialUpdate(); // 构造后第一次调用   初始化树节点
// 实现
public:
	virtual ~CTreePathView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// 生成的消息映射函数
protected:
	DECLARE_MESSAGE_MAP()



//---------------------  需手动添加的变量函数

protected:
	CImageList m_imageList;
	HTREEITEM m_root;
protected:
	void FindFileInDir(TCHAR * rootDir, HTREEITEM hroot, int depth); //在指定目录下寻找文件夹和文件 并将其添加到hroot节点下
	CString GetFullPath(HTREEITEM hcurrent);  //获得TREE控件中某节点的全路径
	void ShowImage(CString imagePath);  //  在Image视图中显示图像
	void InitTree(); // 初始化tree 控件
	void ModifyIcon(HTREEITEM htree);     //  更改树节点的图标



public:
	afx_msg void OnTvnItemexpanding(NMHDR *pNMHDR, LRESULT *pResult);
public:
	afx_msg void OnNMClick(NMHDR *pNMHDR, LRESULT *pResult);
public:
	afx_msg void OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult);
};

#ifndef _DEBUG  // TreePathView.cpp 中的调试版本
inline CTreePathDoc* CTreePathView::GetDocument() const
   { return reinterpret_cast<CTreePathDoc*>(m_pDocument); }
#endif


 

CPP 实现文件 TreePathView.CPP

 

// TreePathView.cpp : CTreePathView 类的实现
//

#include "stdafx.h"
#include "TreePath.h"

#include "TreePathDoc.h"
#include "TreePathView.h"
#include "vector"     //----------vector 需添加

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTreePathView

IMPLEMENT_DYNCREATE(CTreePathView, CTreeView)

BEGIN_MESSAGE_MAP(CTreePathView, CTreeView)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, &CTreePathView::OnTvnItemexpanding)
	ON_NOTIFY_REFLECT(NM_CLICK, &CTreePathView::OnNMClick)
	ON_NOTIFY_REFLECT(NM_DBLCLK, &CTreePathView::OnNMDblclk)
END_MESSAGE_MAP()

// CTreePathView 构造/析构

CTreePathView::CTreePathView()
{
	// TODO: 在此处添加构造代码

}

CTreePathView::~CTreePathView()
{
}

BOOL CTreePathView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	cs.style|=TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT;   //若是想用CImageList的图标 ,则不要设置为TVS_HASBUTTONS形式


	return CTreeView::PreCreateWindow(cs);
}

void CTreePathView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();

	//	CMagazineEditDoc * pDoc=this->GetDocument();
/*	m_imageList.Create(12,12,ILC_COLORDDB | ILC_MASK, 3, 1);
	HICON hAdd=::LoadIcon(::AfxGetInstanceHandle(), (LPCTSTR)IDI_ADD);
	HICON hRemove=::LoadIcon(::AfxGetInstanceHandle(), (LPCTSTR)IDI_REMOVE);
	HICON hLeaf=::LoadIcon(::AfxGetInstanceHandle(), (LPCTSTR)IDI_LEAF);
	m_imageList.Add(hAdd);
	m_imageList.Add(hRemove);
	m_imageList.Add(hLeaf);
	GetTreeCtrl().SetImageList(&m_imageList,TVSIL_NORMAL);

*/   //设置节点图片 若默认,则不需要这一段语句


	InitTree();
	GetTreeCtrl().SetLineColor(RGB(0,0,255));
	GetTreeCtrl().SetBkColor(RGB(196,226,216));

}


// CTreePathView 诊断

#ifdef _DEBUG
void CTreePathView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CTreePathView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CTreePathDoc* CTreePathView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreePathDoc)));
	return (CTreePathDoc*)m_pDocument;
}
#endif //_DEBUG



// CTreePathView 消息处理程序

void CTreePathView::FindFileInDir(TCHAR * rootDir, HTREEITEM hroot, int depth)
{

	CTreeCtrl & m_treeCtrl=GetTreeCtrl();

	struct Node{
		TCHAR fname[256];
		TCHAR filePathName[256];
	};

	Node  directoryTemp,imageFileTemp;
	std::vector <Node> directory,imageFile;
	CString imageName;
	TCHAR fname[256];   
	WIN32_FIND_DATA fd;   
	HANDLE hSearch;  
	TCHAR filePathName[256];  
	TCHAR tmpPath[256];  

	BOOL bSearchFinished = FALSE;  

	ZeroMemory(fname,256);  
	ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));  

	ZeroMemory(filePathName, 256);  
	ZeroMemory(tmpPath, 256);  
	_tcscpy(filePathName, rootDir);  //兼容UNICODE ASIC copy函数


	if( filePathName[_tcslen(filePathName) -1] != L'\\' )  
	{  
		_tcscat(filePathName, L"\\");  
		_tcscat(rootDir,L"\\");
	}  

	_tcscpy(tmpPath,filePathName);
	_tcscat(filePathName, L"*");
	hSearch = FindFirstFile(filePathName, &fd); 


	if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&!(fd.dwFileAttributes&FILE_ATTRIBUTE_SYSTEM)&&!(fd.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..")&&_tcscmp((const TCHAR *)&fd.cFileName,L"RECYCLER")&&_tcscmp((const TCHAR *)&fd.cFileName,L"System Volume Information"))   //文件夹
	{  
		_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
		_tcscpy(directoryTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
		_tcscpy(directoryTemp.filePathName,tmpPath);
		directory.push_back(directoryTemp);
		_tcscpy(tmpPath,rootDir);
		if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"

		{
			HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
			if (NULL==child)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
				m_treeCtrl.InsertItem(directoryTemp.fname,hroot,TVI_LAST);
			return;
		}	
	}

	else if( _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..") )  
	{  
		imageName=fd.cFileName;
		int dotPosition=imageName.ReverseFind(L'.');  // 反向查找
		int num=imageName.GetLength()-dotPosition-1;
		CString temp=imageName.Right(num);



		if(!temp.CompareNoCase(L"jpg")||!temp.CompareNoCase(L"jpeg")||!temp.CompareNoCase(L"bmp")||!temp.CompareNoCase(L"tif")||!temp.CompareNoCase(L"tiff"))

		{        
			_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
			_tcscpy(imageFileTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
			_tcscpy(imageFileTemp.filePathName,tmpPath);
			imageFile.push_back(imageFileTemp);
			_tcscpy(tmpPath,rootDir);
			if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"
			{
				HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
				//				imageName.Format("%.8s",imageFileTemp.fname);

				if (child==NULL)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
					child=m_treeCtrl.InsertItem(imageFileTemp.fname,hroot,TVI_LAST);
				return;
			}
		}
	}  

	while( !bSearchFinished )  
	{  
		if( FindNextFile(hSearch, &fd) )  
		{  
			if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&!(fd.dwFileAttributes&FILE_ATTRIBUTE_SYSTEM) &&!(fd.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..") &&_tcscmp((const TCHAR *)&fd.cFileName,L"RECYCLER")&&_tcscmp((const TCHAR *)&fd.cFileName,L"System Volume Information"))   
			{  

				_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
				_tcscpy(directoryTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
				_tcscpy(directoryTemp.filePathName,tmpPath);
				directory.push_back(directoryTemp);
				_tcscpy(tmpPath,rootDir);
				if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"
				{
					HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
					if (NULL==child)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
						m_treeCtrl.InsertItem(directoryTemp.fname,hroot,TVI_LAST);
					return;
				}
			}  
			else if( _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..") )  
			{
				imageName=fd.cFileName;
				int dotPosition=imageName.ReverseFind(L'.');
				int num=imageName.GetLength()-dotPosition-1;
				CString temp=imageName.Right(num);
				if(!temp.CompareNoCase(L"jpg")||!temp.CompareNoCase(L"jpeg")||!temp.CompareNoCase(L"bmp")||!temp.CompareNoCase(L"tif")||!temp.CompareNoCase(L"tiff"))
				{       
					_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
					_tcscpy(imageFileTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
					_tcscpy(imageFileTemp.filePathName,tmpPath);
					imageFile.push_back(imageFileTemp);
					_tcscpy(tmpPath,rootDir);

					if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"
					{
						HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
						if (child==NULL)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
							child=m_treeCtrl.InsertItem(imageFileTemp.fname,hroot,TVI_LAST);
						return;
					}

				}
			}  
		}  
		else  
		{  
			if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished   
				bSearchFinished = TRUE;  
			else  
				bSearchFinished = TRUE; //Terminate Search   
		}  
	}  

	FindClose(hSearch);  
	HTREEITEM  sub;

	for(unsigned int i=0;i<directory.size();i++)
	{
		CString directoryName=directory.at(i).fname;
		BOOL flag=FALSE;
		sub=m_treeCtrl.GetChildItem(hroot);
		while (sub!=NULL)
		{
			CString tem=m_treeCtrl.GetItemText(sub);
			if (m_treeCtrl.GetItemText(sub)==directoryName)
			{
				flag=TRUE;
				break;
			}		
			sub=m_treeCtrl.GetNextItem(sub,TVGN_NEXT);
		}        
		if(!flag)
			sub=m_treeCtrl.InsertItem(directory.at(i).fname,hroot,TVI_LAST);
		if (depth>0)
			FindFileInDir(directory.at(i).filePathName,sub,depth-1);
	}
	std::vector<Node>().swap(directory); //释放vector资源

	for( unsigned int i=0;i<imageFile.size();i++)
	{
		CString fileName;
		fileName=imageFile.at(i).fname;    // 使用CA2CT 将字符数组转换为CString串

		BOOL flag=FALSE;
		sub=m_treeCtrl.GetChildItem(hroot);
		while (sub!=NULL)
		{
			if (m_treeCtrl.GetItemText(sub)==fileName)
			{
				flag=TRUE;
				break;
			}		
			sub=m_treeCtrl.GetNextItem(sub,TVGN_NEXT);
		}
		if(!flag)
		{	
			HTREEITEM hsub=m_treeCtrl.InsertItem(fileName,hroot,TVI_LAST);
		}
	}
	std::vector<Node>().swap(imageFile);

}

CString CTreePathView::GetFullPath(HTREEITEM hcurrent)
{
	CTreeCtrl & m_treeCtrl=GetTreeCtrl();
	CString fullPathName,tmpPathName;
	HTREEITEM hparent=hcurrent;
	fullPathName=m_treeCtrl.GetItemText(hcurrent);
	while (hparent!=m_root)
	{
		hcurrent=hparent;
		fullPathName=tmpPathName+fullPathName;
		hparent=m_treeCtrl.GetParentItem(hcurrent);
		tmpPathName=m_treeCtrl.GetItemText(hparent);
		if (tmpPathName.Right(1)!=L'\\')
		{
			tmpPathName+=L"\\";
		}
	}
	tmpPathName=m_treeCtrl.GetItemText(hcurrent);
	if (tmpPathName==L"桌面")
	{
		TCHAR myDesktop[_MAX_PATH];
		SHGetSpecialFolderPath(NULL,myDesktop,CSIDL_DESKTOPDIRECTORY,0);
		fullPathName.Delete(0,2);  //删除根目录 桌面 
		fullPathName=myDesktop + fullPathName;
		return fullPathName;
	}
	if (tmpPathName==L"我的文档")
	{
		TCHAR myDocument[_MAX_PATH];
		SHGetSpecialFolderPath(NULL,myDocument,CSIDL_PERSONAL,0);
		fullPathName.Delete(0,4);  //删除根目录 桌面 
		fullPathName=myDocument + fullPathName;
		return fullPathName;
	}
	return fullPathName;
}

void CTreePathView::ShowImage(CString imagePath)  //在另一个视图中显示图像  需根据需要手动更改 
{
/*	CImageView * pView=(CImageView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));
	CRect rect;
	pView->GetClientRect(&rect);
	Graphics graphics(pView->GetDC()->m_hDC);

	m_pImage=Image::FromFile(imagePath);
	graphics.DrawImage(m_pImage,0,0,rect.Width(),rect.Height());

	*/
}


void  CTreePathView::InitTree()

{

	CTreeCtrl & treeCtrl=GetTreeCtrl();
	HTREEITEM root;
	m_root=treeCtrl.InsertItem(reinterpret_cast<LPCTSTR>(L"我的电脑"),TVI_ROOT);
	TCHAR buf[100];
	GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf);  //获得逻辑盘信息 并将其保存在buf中
	CString strDisks;

	for (TCHAR *s=buf;*s;s+=_tcslen(s)+1)
	{
		strDisks=s;  // s为获得的每一个逻辑盘
		root=treeCtrl.InsertItem(strDisks,m_root,TVI_LAST);
		treeCtrl.SetItemImage(root,0,0);
		FindFileInDir(reinterpret_cast<TCHAR *>(s),root,1);  

		ModifyIcon(root);
	}

	// 获得桌面目录
	TCHAR myDesktop[_MAX_PATH],myDocument[_MAX_PATH];
	SHGetSpecialFolderPath(NULL,myDesktop,CSIDL_DESKTOPDIRECTORY,0);
	HTREEITEM hDesktop=treeCtrl.InsertItem(reinterpret_cast<LPCTSTR>(L"桌面"),m_root,TVI_FIRST);
	FindFileInDir(reinterpret_cast<TCHAR *>(myDesktop),hDesktop,1);
	ModifyIcon(hDesktop);


	//获得我的文档目录
	SHGetSpecialFolderPath(NULL,myDocument,CSIDL_PERSONAL,0);
	HTREEITEM hmyDocument=treeCtrl.InsertItem(reinterpret_cast<LPCTSTR>(L"我的文档"),m_root,hDesktop);
	FindFileInDir(reinterpret_cast<TCHAR *>(myDocument),hmyDocument,1);
	ModifyIcon(hmyDocument);
	treeCtrl.Expand(m_root,TVE_COLLAPSE);

}

void CTreePathView::ModifyIcon(HTREEITEM htree)  // 设置节点图片 若是希望默认时,则不需要这个函数
{
	CTreeCtrl & treeCtrl=GetTreeCtrl();
	if (treeCtrl.ItemHasChildren(htree))
	{
		if ((treeCtrl.GetItemState(htree,TVIS_EXPANDED)&TVIS_EXPANDED)==TVIS_EXPANDED)
		{
			treeCtrl.SetItemImage(htree,1,1);
			return;
		}else{
			treeCtrl.SetItemImage(htree,0,0);
			return;
		}
	}
	CString strName=treeCtrl.GetItemText(htree);
	if(strName.Find(L'.')!=-1)
		treeCtrl.SetItemImage(htree,2,2);
	else
		treeCtrl.SetItemImage(htree,4,4);
}


void CTreePathView::OnTvnItemexpanding(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码


	CTreeCtrl &m_treeCtrl=GetTreeCtrl();

	HTREEITEM htree=pNMTreeView->itemNew.hItem; // 获得即将被扩展节点的句柄



	if (htree!=m_root)
	{
		CString directorPath=GetFullPath(htree);
		TCHAR *path;
		path=reinterpret_cast<TCHAR *>(directorPath.GetBuffer(directorPath.GetLength()+1));   //  getlength +1  否则内存出错
		FindFileInDir(path,htree,1);
		directorPath.ReleaseBuffer();
	}

	HTREEITEM hChild=m_treeCtrl.GetChildItem(htree);

	while (hChild!=NULL)
	{
		ModifyIcon(hChild);
		hChild=m_treeCtrl.GetNextItem(hChild,TVGN_NEXT);		
	}




	if ((m_treeCtrl.GetItemState(htree,TVIS_EXPANDED)&TVIS_EXPANDED)==TVIS_EXPANDED)
	{
		m_treeCtrl.SetItemImage(htree,0,0); //+
	}else{
		m_treeCtrl.SetItemImage(htree,1,1); //-
	}



	*pResult = 0;
}


void CTreePathView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)  //若是改变默认+-号  则需重载此函数;若使用默认的+-号,则不要重载此函数 否则出错
{
	// TODO: 在此添加控件通知处理程序代码
/*

	CPoint pt;
	GetCursorPos(&pt);
	GetTreeCtrl().ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hCurSel = GetTreeCtrl().GetSelectedItem();
	HTREEITEM hItem = GetTreeCtrl().HitTest(pt, &uFlag);  //返回鼠标单击的那个节点句柄

	if(hItem)
	{
		if(hCurSel != hItem)
		{
			GetTreeCtrl().SelectItem(hItem);
		}
		GetTreeCtrl().Expand(hItem,TVE_TOGGLE);
		ModifyIcon(hItem);
	}
*/

	*pResult = 0;
}


void CTreePathView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult) // 扩展操作 ,双击时,用于显示图片等 需自己根据需要添加
{
	// TODO: 在此添加控件通知处理程序代码



	CString tempPath;
	HTREEITEM htree=GetTreeCtrl().GetSelectedItem();
	tempPath=GetFullPath(htree);
	int dotPosition=tempPath.ReverseFind(L'.');  // 反向查找
	if(dotPosition!=-1)
	{
		int num=tempPath.GetLength()-dotPosition-1;
		CString temp=tempPath.Right(num);  //temp 保存文件扩展名
		if(!temp.CompareNoCase(L"jpg")||!temp.CompareNoCase(L"jpeg")||!temp.CompareNoCase(L"bmp")||!temp.CompareNoCase(L"tif")||!temp.CompareNoCase(L"tiff"))
		{   
		/*  m_imagePath=tempPath;

		  ShowImage(m_imagePath);
		  */
		}
	}





	*pResult = 0;
}


 

 

注: 上面的程序FindFileInDir 不严谨, 没有及时释放资源 ,导致在程序运行时,修改系统文件夹名称时 ,出现文件或文件夹不可修改 问题

一直,没有找到原因,今天终于弄清楚了,原来没有释放系统资源。

 

FindFirstFile、FindNextFile  查找结束后 一定要调用FindClose释放资源

程序中虽然也有调用FindClose, 但是,程序中有好几个 return ,使得程序提前退出,但提前退出时,未能FindClose释放资源,由此导致了程序运行时,一直占用着资源,而不释放,从而导致问题。

 

解决方案:  提前return退出时,同样需要释放系统资源

 

在每个return之前,添加 FindClose(hSearch)释放资源

				FindClose(hSearch);  // 提前退出时,需要先释放系统资源
				return;

 

这样,在程序运行时,就可以继续对文件和文件夹进行操作了


修改后的FindFileInDir 程序如下:

 

 

void FindFileInDir(TCHAR * rootDir, HTREEITEM hroot, int depth)
{

	

	CTreeCtrl & m_treeCtrl=GetTreeCtrl();

	struct Node{
		TCHAR fname[256];
		TCHAR filePathName[256];
	};

	Node  directoryTemp,imageFileTemp;
	std::vector <Node> directory,imageFile;
	CString imageName;
	TCHAR fname[256];   
	WIN32_FIND_DATA fd;   
	HANDLE hSearch;  
	TCHAR filePathName[256];  
	TCHAR tmpPath[256];  

	BOOL bSearchFinished = FALSE;  

	ZeroMemory(fname,256);  
	ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));  

	ZeroMemory(filePathName, 256);  
	ZeroMemory(tmpPath, 256);  
	_tcscpy(filePathName, rootDir);  //兼容UNICODE ASIC copy函数


	if( filePathName[_tcslen(filePathName) -1] != L'\\' )  
	{  
		_tcscat(filePathName, L"\\");  
		_tcscat(rootDir,L"\\");
	}  

	_tcscpy(tmpPath,filePathName);
	_tcscat(filePathName, L"*");
	hSearch = FindFirstFile(filePathName, &fd); 


	if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&!(fd.dwFileAttributes&FILE_ATTRIBUTE_SYSTEM)&&!(fd.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..")&&_tcscmp((const TCHAR *)&fd.cFileName,L"RECYCLER")&&_tcscmp((const TCHAR *)&fd.cFileName,L"System Volume Information"))   //文件夹
	{  
		_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
		_tcscpy(directoryTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
		_tcscpy(directoryTemp.filePathName,tmpPath);
		directory.push_back(directoryTemp);
		_tcscpy(tmpPath,rootDir);
		if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"

		{
			HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
			if (NULL==child)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
				m_treeCtrl.InsertItem(directoryTemp.fname,hroot,TVI_LAST);

	        FindClose(hSearch);  // 提前退出时,需要先释放系统资源
			return;
		}	
	}

	else if( _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..") )  
	{  
		imageName=fd.cFileName;
		int dotPosition=imageName.ReverseFind(L'.');  // 反向查找
		int num=imageName.GetLength()-dotPosition-1;
		CString temp=imageName.Right(num);



		if(!temp.CompareNoCase(L"jpg")||!temp.CompareNoCase(L"jpeg")||!temp.CompareNoCase(L"bmp")||!temp.CompareNoCase(L"tif")||!temp.CompareNoCase(L"tiff"))

		{        
			_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
			_tcscpy(imageFileTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
			_tcscpy(imageFileTemp.filePathName,tmpPath);
			imageFile.push_back(imageFileTemp);
			_tcscpy(tmpPath,rootDir);
			if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"
			{
				HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
				//				imageName.Format("%.8s",imageFileTemp.fname);

				if (child==NULL)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
					child=m_treeCtrl.InsertItem(imageFileTemp.fname,hroot,TVI_LAST);

				FindClose(hSearch);  // 提前退出时,需要先释放系统资源
				return;
			}
		}
	}  

	while( !bSearchFinished )  
	{  
		if( FindNextFile(hSearch, &fd) )  
		{  
			if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&!(fd.dwFileAttributes&FILE_ATTRIBUTE_SYSTEM) &&!(fd.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..") &&_tcscmp((const TCHAR *)&fd.cFileName,L"RECYCLER")&&_tcscmp((const TCHAR *)&fd.cFileName,L"System Volume Information"))   
			{  

				_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
				_tcscpy(directoryTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
				_tcscpy(directoryTemp.filePathName,tmpPath);
				directory.push_back(directoryTemp);
				_tcscpy(tmpPath,rootDir);
				if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"
				{
					HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
					if (NULL==child)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
						m_treeCtrl.InsertItem(directoryTemp.fname,hroot,TVI_LAST);

					FindClose(hSearch);  // 提前退出时,需要先释放系统资源
					return;
				}
			}  
			else if( _tcscmp((const TCHAR *)&fd.cFileName, L".") && _tcscmp((const TCHAR *)&fd.cFileName, L"..") )  
			{
				imageName=fd.cFileName;
				int dotPosition=imageName.ReverseFind(L'.');
				int num=imageName.GetLength()-dotPosition-1;
				CString temp=imageName.Right(num);
				if(!temp.CompareNoCase(L"jpg")||!temp.CompareNoCase(L"jpeg")||!temp.CompareNoCase(L"bmp")||!temp.CompareNoCase(L"tif")||!temp.CompareNoCase(L"tiff"))
				{       
					_tcscat(tmpPath, (const TCHAR *)&fd.cFileName);  
					_tcscpy(imageFileTemp.fname,reinterpret_cast<const TCHAR *>(fd.cFileName));
					_tcscpy(imageFileTemp.filePathName,tmpPath);
					imageFile.push_back(imageFileTemp);
					_tcscpy(tmpPath,rootDir);

					if (depth==0)// 判断子目录下是否有文件夹或图像 如果有 则将其中一个加入tree控件中,目的是为了子目录前面可以显示"+"
					{
						HTREEITEM child=m_treeCtrl.GetChildItem(hroot);  
						if (child==NULL)  // 判断是否已经存在 已经存在的话就不用再添加了 ,否则Parent'Parent 扩展时 会出现连续往树控件中添加此节点的错误
							child=m_treeCtrl.InsertItem(imageFileTemp.fname,hroot,TVI_LAST);

						FindClose(hSearch);  // 提前退出时,需要先释放系统资源
						return;
					}

				}
			}  
		}  
		else  
		{  
			if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished   
				bSearchFinished = TRUE;  
			else  
				bSearchFinished = TRUE; //Terminate Search   
		}  
	}  

	FindClose(hSearch);  
	HTREEITEM  sub;

	for(unsigned int i=0;i<directory.size();i++)
	{
		CString directoryName=directory.at(i).fname;
		BOOL flag=FALSE;
		sub=m_treeCtrl.GetChildItem(hroot);
		while (sub!=NULL)
		{
			CString tem=m_treeCtrl.GetItemText(sub);
			if (m_treeCtrl.GetItemText(sub)==directoryName)
			{
				flag=TRUE;
				break;
			}		
			sub=m_treeCtrl.GetNextItem(sub,TVGN_NEXT);
		}        
		if(!flag)
			sub=m_treeCtrl.InsertItem(directory.at(i).fname,hroot,TVI_LAST);
		if (depth>0)
			FindFileInDir(directory.at(i).filePathName,sub,depth-1);
	}

	for( unsigned int i=0;i<imageFile.size();i++)
	{
		CString fileName;
		fileName=imageFile.at(i).fname;    // 使用CA2CT 将字符数组转换为CString串

		BOOL flag=FALSE;
		sub=m_treeCtrl.GetChildItem(hroot);
		while (sub!=NULL)
		{
			if (m_treeCtrl.GetItemText(sub)==fileName)
			{
				flag=TRUE;
				break;
			}		
			sub=m_treeCtrl.GetNextItem(sub,TVGN_NEXT);
		}
		if(!flag)
		{	
			HTREEITEM hsub=m_treeCtrl.InsertItem(fileName,hroot,TVI_LAST);
		}
	}
}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(vector,tree,null,System,扩展,imagelist)