📄 getfolder.cpp
字号:
// Get the Part from a given selection point.
//CREATED
// 8-5-1999 22:29:00
//PARAMS
// hItem Item to Get Path for.
//RETURNS
// Full path as a string.
CString CGetFolder::GetItemPath( HTREEITEM hItem )
{
TRACE( _T("CDlgGetPath::GetItemPath(%p)\n"), hItem );
CString sRet;
do
{
//End with a share name.
NETRESOURCE *const pNetResource = (NETRESOURCE *)(m_ctlTree.GetItemData( hItem ) );
if( pNetResource )
{
sRet = CString(pNetResource->lpRemoteName) + _T('\\')+ sRet;
break;
}
//Add the directory name to the path.
sRet = m_ctlTree.GetItemText( hItem ) + _T('\\')+ sRet;
hItem = m_ctlTree.GetParentItem( hItem );
} while( hItem );
return sRet;
}
BOOL CGetFolder::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
//Get the current selection before heading home
m_sPath = GetItemPath( m_ctlTree.GetSelectedItem( ) );
//Start at the root deleting.
HTREEITEM hItemCur = m_ctlTree.GetRootItem( ); //Current item under inspection
//
//While their is still data in the tree
//
while( hItemCur )
{
HTREEITEM hItem = m_ctlTree.GetChildItem( hItemCur );
//Has children then make the child current
if( hItem )
{
hItemCur = hItem;
continue;
}
//Item has not children so we shall destroy it.
//but first we must decide who is to take its place.
HTREEITEM hNextSibItem = m_ctlTree.GetNextSiblingItem( hItemCur );
HTREEITEM hPrevSibItem = m_ctlTree.GetPrevSiblingItem( hItemCur );
HTREEITEM hParentItem = m_ctlTree.GetParentItem( hItemCur );
//Get item data to check if lparam is to be destroyed
NETRESOURCE *const pNetResource = (NETRESOURCE *)m_ctlTree.GetItemData( hItemCur );//(item.lParam);
if( pNetResource )
{
delete [] (pNetResource->lpLocalName);
delete [] (pNetResource->lpRemoteName);
delete [] (pNetResource->lpComment);
delete [] (pNetResource->lpProvider);
delete pNetResource;
}
m_ctlTree.DeleteItem( hItemCur );
hItemCur = NULL;
//Determine which item is next to recieve the focus
if( hParentItem )
hItemCur = hParentItem;
if( hPrevSibItem )
hItemCur = hPrevSibItem;
if( hNextSibItem )
hItemCur = hNextSibItem;
}
//All items removed from list now. Lets trash this place and go home.
return CDialog::DestroyWindow();
}
void CGetFolder::OnItemexpandingDirTree(NMHDR* pNMHDR, LRESULT* pResult)
{
TRACE( _T("CDlgGetPath::OnItemexpandingTree(%p)\n"), pNMHDR );
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
CWaitCursor CursorWaiting; //Show the wait cursor while expanding
//Only action 2 notifications
if( pNMTreeView->action == 2 )
{
//Update location display
CString sPath = GetItemPath( pNMTreeView->itemNew.hItem );
//Refresh children
if( !m_ctlTree.GetChildItem( pNMTreeView->itemNew.hItem ) )
{
PopulateTree( sPath, pNMTreeView->itemNew.hItem );
if( m_ctlTree.GetSelectedItem( ) != pNMTreeView->itemNew.hItem )
m_ctlTree.SelectItem( pNMTreeView->itemNew.hItem );
}
}
*pResult = 0;
}
/////////////////////////////////////////////////////////////////////////////
//FUNCTION
// To display the same was window explorer
// ..names all uppercase letters are converted
// ..to inital caps.
//CREATED
// 23-5-1999 16:10:34
//PARAMS
// sName sName to convert.
//RETURNS
// Converted name
CString WindowName( CString sName )
{
CString sRet;
//Scan all charactors to determine if their are any lower case items
for( int n = 0; n < sName.GetLength(); n++ )
{
TCHAR ch = sName[n];
if ((ch >= 'a') && (ch <= 'z') )
return sName;
}
sName.MakeLower();
if( sName.GetLength() > 0 )
{
CString sFirstChar = sName[0];
sFirstChar.MakeUpper();
sName = sFirstChar + sName.Mid( 1 );
}
return sName;
}
/////////////////////////////////////////////////////////////////////////////
//FUNCTION
// Convert the
//CREATED
// 23-5-1999 15:55:53
//PARAMS
//
//RETURNS
//
TCHAR* MakeObjectDynamic( LPTSTR szData )
{
TRACE( _T("MakeObjectDynamic( %s )\n"), szData );
//Assume a NULL empty string
TCHAR * szRet = NULL;
int nLength = 0;
if( szData )
nLength = _tcslen( szData )+1;
if( nLength > 0 )
{
szRet = new TCHAR[nLength];
_tcscpy( szRet, szData );
}
return szRet;
}
///////////////////////////////////////////////////////////////////////////////
//DESCRIPTION:
// Enumerate the given network resource. This is where the magic happens.
//CREATED:
// 10-5-1999 15:19:02
//PARAMS:
// hParent Item to create the child items from
// ..(item.lParam) == NULL if finding the NETWORK root.
//RETURN:
// ture if one or more items were found
bool CGetFolder::EnumNetwork( HTREEITEM hParent )
{
TRACE( _T("CDlgGetPath::EnumNetwork( %p )\n"), hParent );
bool bGotChildren = false; //True if a child is added.
//Check if the item already has a network resource and use it.
NETRESOURCE *const pNetResource = (NETRESOURCE *)(m_ctlTree.GetItemData( hParent ) );
//
//Setup
//
DWORD dwResult;
HANDLE hEnum;
DWORD cbBuffer = 16384;
DWORD cEntries = 0xFFFFFFFF;
LPNETRESOURCE lpnrDrv;
DWORD i;
dwResult = WNetOpenEnum( pNetResource ? RESOURCE_GLOBALNET : RESOURCE_CONTEXT,
RESOURCETYPE_ANY,//RESOURCETYPE_DISK,
0,
pNetResource ? pNetResource : NULL,
&hEnum );
//Was the read sucessfull
if (dwResult != NO_ERROR)
{
TRACE( _T("*** ERROR %d - Cannot enumerate network drives.\n"), dwResult );
return false;
}
//
//Get items until no more remain.
//
do
{
lpnrDrv = (LPNETRESOURCE) GlobalAlloc( GPTR, cbBuffer );
dwResult = WNetEnumResource( hEnum, &cEntries, lpnrDrv, &cbBuffer );
if (dwResult == NO_ERROR)
{
//Scann through the results
for( i = 0; i < cEntries; i++ )
{
CString sNameRemote = lpnrDrv[i].lpRemoteName;
int nType = 9;
if( sNameRemote.IsEmpty() )
{
sNameRemote = lpnrDrv[i].lpComment;
nType = 8;
}
//
//Remove leading back slashes
//
if( sNameRemote.GetLength() > 0 && sNameRemote[0] == _T('\\') )
sNameRemote = sNameRemote.Mid( 1 );
if( sNameRemote.GetLength() > 0 && sNameRemote[0] == _T('\\') )
sNameRemote = sNameRemote.Mid( 1 );
//
//Display a share or the appropiate icon
//
if( lpnrDrv[i].dwDisplayType == RESOURCEDISPLAYTYPE_SHARE )
{
//Display only the share name
int nPos = sNameRemote.Find( _T('\\') );
if( nPos >= 0 )
sNameRemote = sNameRemote.Mid( nPos+1 );
InsertItem( hParent, NULL, sNameRemote, DRIVE_NO_ROOT_DIR, DRIVE_UNKNOWN );
}
else
{
NETRESOURCE* pResource = new NETRESOURCE;
*pResource = lpnrDrv[i];
pResource->lpLocalName = MakeObjectDynamic( pResource->lpLocalName );
pResource->lpRemoteName = MakeObjectDynamic( pResource->lpRemoteName );
pResource->lpComment = MakeObjectDynamic( pResource->lpComment );
pResource->lpProvider = MakeObjectDynamic( pResource->lpProvider );
InsertItem( hParent, pResource, sNameRemote, pResource->dwDisplayType+7 );
}
bGotChildren = true;
}
}
GlobalFree( (HGLOBAL) lpnrDrv );
if( dwResult != ERROR_NO_MORE_ITEMS )
{
TRACE( _T("*** ERROR %d - Cannot complete network drive enumeration\n"), dwResult );
break;
}
}
while( dwResult != ERROR_NO_MORE_ITEMS );
//
//Let go and go home
//
WNetCloseEnum(hEnum);
return bGotChildren;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -