⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getfolder.cpp

📁 一个简单的学生学籍管理工具。vc编写。 有喜欢的可以批评指正^_^
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GetFolder.cpp : implementation file
//

//original functionality and code provided by John McTainsh
//modifications to ParsePath and InitDialog made by 
//Brian Convery to fix infinite loop problems and to properly
//parse to the desired directory.

#include "stdafx.h"
//#include "testapp.h"	//replace with whatever your app is called
#include "GetFolder.h"
#include <winnetwk.h>  //need to add mpr.lib to project link settings
						//It gets added in the Object/Libraries modules field
						//best if done for all configurations...

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
//Handy local functions.

TCHAR*	MakeObjectDynamic( LPTSTR szData );
CString WindowName( CString sName );
bool	ParsePath( CString sPath, int nLevel, CString *const psDirName );

#define NETWORK_NEIGHBOUR   _T("Network Neighborhood")

/////////////////////////////////////////////////////////////////////////////
// CGetFolder dialog


CGetFolder::CGetFolder(CWnd* pParent /*=NULL*/)
	: CDialog(CGetFolder::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGetFolder)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CGetFolder::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGetFolder)
	DDX_Control(pDX, IDC_DIR_TREE, m_ctlTree);
	//}}AFX_DATA_MAP

	m_hNetworkRoot = NULL;
}


BEGIN_MESSAGE_MAP(CGetFolder, CDialog)
	//{{AFX_MSG_MAP(CGetFolder)
	ON_NOTIFY(TVN_SELCHANGED, IDC_DIR_TREE, OnSelchangedDirTree)
	ON_NOTIFY(TVN_ITEMEXPANDING, IDC_DIR_TREE, OnItemexpandingDirTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
//FUNCTION
//	Parse the path and extract the given directory or drive name.
//	nLevel 0 will return the drive number or "" for a network name.
//CREATED
//	10-7-1999  11:37:47
//PARAMS
//	sPath		Full path to parse.
//	nLevel		Level to return 0 = the root or drive name.
//	psDirName	returned directory name
//RETURNS
//	true	if a directory or drive name was returned.
//	false	if no more directorys

bool ParsePath( CString sPath, int nLevel, CString *const psDirName )
{
	//find first match
	int nStart = 0;									//Location in sPath to search from
	int testCondition = 0;
	while( nLevel )
	{
		testCondition = sPath.Mid(nStart).Find( _T('\\') );
		nStart += sPath.Mid(nStart).Find( _T('\\') );
		if( testCondition < 0 )
			return false;
		nLevel--;
		nStart++;
	}

	//We now have the start point to find the end.
	int nEnd = sPath.Mid(nStart).Find( _T('\\') );
	if( nEnd < 0 )
		*psDirName = sPath.Mid( nStart );
	else
		*psDirName = sPath.Mid( nStart, nEnd);

	return true;
}


/////////////////////////////////////////////////////////////////////////////
// CGetFolder message handlers

void CGetFolder::OnSelchangedDirTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	TRACE( _T("CDlgGetPath::OnSelchangedTree(%p)\n"), pNMHDR );
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	//Disable search on Workstation roots
	if( m_ctlTree.GetItemData( pNMTreeView->itemNew.hItem ) || 
			pNMTreeView->itemNew.hItem == m_hNetworkRoot )
		GetDlgItem( IDOK )->EnableWindow( false );
	else
		GetDlgItem( IDOK )->EnableWindow( true );

	*pResult = 0;
}

BOOL CGetFolder::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	//
	//Setup the Image lists
	//

	if( !m_ImageListTree.Create( IDB_FILE_TYPES, 17, 1, RGB( 255, 255, 255 ) ) )
		TRACE( _T("Image list creation fault") );
	m_ctlTree.SetImageList( &m_ImageListTree, TVSIL_NORMAL );

	//
	//List the local drives
	//

	for( TCHAR cLetter = _T('A'); cLetter <= _T('Z'); cLetter++ )
	{
		CString sDrive = cLetter;
		sDrive += _T(":");						
		UINT nType = GetDriveType( sDrive + _T("\\") );
		if( DRIVE_REMOVABLE <= nType && nType <= DRIVE_RAMDISK )
			InsertItem( TVI_ROOT, NULL, sDrive, nType );
	}

	//Network neighbour hood
	m_hNetworkRoot = InsertItem( TVI_ROOT, NULL, NETWORK_NEIGHBOUR, DRIVE_RAMDISK+1 );

	//
	//Expand the Tree to the Inital Path.
	//

	int nLevel = 0;
	CString sDirName;
	HTREEITEM hCurrent = TVI_ROOT;					//Current item in the list being expanded.
	if( m_sPath.IsEmpty() )
		m_sPath = _T("C:\\");

	while( ParsePath( m_sPath, nLevel, &sDirName ) )
	{
		//Network or Drive expand
		if( !nLevel && sDirName.IsEmpty() )
				sDirName = NETWORK_NEIGHBOUR;

		//Search for the matching tree item and expand
		HTREEITEM hItem = m_ctlTree.GetChildItem( hCurrent );	
		while( hItem )
		{
			if( sDirName.CompareNoCase( m_ctlTree.GetItemText( hItem ) ) == 0 )
			{
				hCurrent = hItem;
				m_ctlTree.Expand( hCurrent, TVE_EXPAND );
				break;
			}
			hItem = m_ctlTree.GetNextSiblingItem( hItem );
		}

		nLevel++;

	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

///////////////////////////////////////////////////////////////////////////////
//DESCRIPTION:
//	Insert an Item in to the List
//CREATED:
//	10-5-1999  13:49:20
//PARAMS:
//	hParent		Parent to Attach to.
//	sText		Text to Add.
//	iImage		Image.
//RETURN:
//	Handle of Item created

HTREEITEM CGetFolder::InsertItem( HTREEITEM hParent, NETRESOURCE *const pNetResource, CString sText, int iImage, int iImageSelected /* = -1 */ )
{
	TRACE( _T("CDlgGetPath::InsertItem(%p,%p,%s +++ )\n"), hParent, pNetResource, sText );

	sText = WindowName( sText );
	TVINSERTSTRUCT InsertStruct;
	InsertStruct.hParent		= hParent;	
	InsertStruct.hInsertAfter	= TVI_SORT;
	InsertStruct.item.mask	= TVIF_IMAGE | TVIF_TEXT | TVIF_CHILDREN | 
								  TVIF_SELECTEDIMAGE | TVIF_PARAM;
	InsertStruct.item.pszText	= sText.GetBuffer( sText.GetLength() );  sText.ReleaseBuffer();
	InsertStruct.item.iImage	= iImage;
	InsertStruct.item.cChildren= 1;
	InsertStruct.item.lParam	= (LPARAM)pNetResource;

	if( iImageSelected == -1 )
		InsertStruct.item.iSelectedImage = iImage;
	else
		InsertStruct.item.iSelectedImage = iImageSelected;

	return m_ctlTree.InsertItem( &InsertStruct );

}


/////////////////////////////////////////////////////////////////////////////
//FUNCTION
//	Called in request to expand an item in the tree.
//CREATED
//	8-5-1999  19:57:09
//PARAMS
//	sPath	Path to use for populating from
//	hParent	Where to add the data
//RETURNS
//	True if some children were added

bool CGetFolder::PopulateTree( CString sPath, HTREEITEM hParent ) 
{
	TRACE( _T("CDlgGetPath::PopulateTree( %s )\n"), sPath );

	bool bGotChildren = false;						//True if a child is added.	

	//
	//Populate Network neighbourhood tree (Entire network and Local-Computers)
	//

	if( hParent == m_hNetworkRoot )
	{
		bGotChildren = EnumNetwork( hParent );
	}

	//
	//Network item(Search deep into the network)
	//

	else if( m_ctlTree.GetItemData( hParent ) )
	{
		bGotChildren = EnumNetwork( hParent );
	}

	//
	//Search for files and populate the CTreeCtrl
	//

	else
	{
		CFileFind finder;
		BOOL bWorking = finder.FindFile( sPath+_T("*.*") );
		while (bWorking)   
		{
			bWorking = finder.FindNextFile();
			if( finder.IsDots() )
				continue;
			if( finder.IsDirectory() )
			{
				InsertItem( hParent, NULL, finder.GetFileName(), DRIVE_NO_ROOT_DIR, DRIVE_UNKNOWN );
				bGotChildren = true;
			}
		}
	}

	//
	//Remove the [+] if no children
	//

	if( !bGotChildren )
	{
		TVITEM item		= { 0 };
		item.mask		= TVIF_HANDLE | TVIF_CHILDREN;
		item.hItem		= hParent;
		item.cChildren	= 0;
		m_ctlTree.SetItem( &item );
	}

	return bGotChildren;

}

/////////////////////////////////////////////////////////////////////////////
//FUNCTION

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -