📄 dlggetpath.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
//
// DlgGetPath.cpp : implementation file
//
//PURPOSE
// Display a dialog to allow the user to select a file path.
//
//AUTHOR
// John McTainsh (john@mctainsh.com)
//
//THING TO DO
// 1) Display the recycle bin appropiatly.
// 2) Display the contents of the recycle bin with out S- numbers.
// 3) Sort items when added except for "Entire Network"
// 4) Check on a more complex network.
// 5) Get icons for the network items.
// 6) Determine why icon disappears from the ALT-TAB bar when active?
// 7) Handle WNetEnumResource for more than 16384 bytes of data.
// 8) Use SHGetFileInfo to load the correct icons to display.
//
//VERSION HISTORY
// Ver 1.0 - 21.5.99
// *Inital release.
//
// Ver 2.0 - 10.7.99
// *Corrected Bug identified by Sven Wiegand when locating Drives.
// *Added Set Path option to expand to the tree to
// the inital path on startup.
//
//BOGUS COPYRIGHT NOTICE
// Copyright 1999 (C) John McTainsh.
// I am happy for any developer to use this dialog. I think it is pretty
// cool and suites my purposes. Feel free to copy and alter whatever
// you please. Keep me informed of any good things you do to it
// and also what it has been used for (for my ego).
//
// This is shareware and can be copied or distrabuted without my concent,
// permision or knowledge. It would be uncool to use it without putting
// my name in the credits (ego) but that is up to you.
//
// This code is provide as is. If it dont work or dont do what it should
// then dont come crying to me. It is full of bugs (undocumented features)
// which you the user/developer must find to use it.
//
// Remember, you to can become a Jedi master (VC++ developer) if you can
// resist the temptations of the dark side of the force (VB).
//
// PS
// Comments regarding my spelling will NOT be kindly recieved.
// PSS
// All the best software is written by New Zealanders.
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Resource.h" //Definitions for resource ID's
#include "DlgGetPath.h" //Definition to get path
#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")
/////////////////////////////////////////////////////////////////////////////
// CDlgGetPath dialog
CDlgGetPath::CDlgGetPath(CWnd* pParent /*=NULL*/)
: CDialog(CDlgGetPath::IDD, pParent)
{
TRACE( _T("CDlgGetPath::CDlgGetPath(%p)\n"), pParent );
//{{AFX_DATA_INIT(CDlgGetPath)
//}}AFX_DATA_INIT
m_hNetworkRoot = NULL;
}
void CDlgGetPath::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgGetPath)
DDX_Control(pDX, IDC_TREE, m_Tree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgGetPath, CDialog)
//{{AFX_MSG_MAP(CDlgGetPath)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
ON_NOTIFY(TVN_ITEMEXPANDING, IDC_TREE, OnItemexpandingTree)
//}}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 )
{
ASSERT( psDirName );
//find first match
int nStart = 0; //Location in sPath to search from
while( nLevel )
{
nStart = sPath.Find( _T('\\'), nStart );
if( nStart < 0 )
return false;
nLevel--;
nStart++;
}
//We now have the start point to find the end.
int nEnd = sPath.Find( _T('\\'), nStart );
if( nEnd < 0 )
*psDirName = sPath.Mid( nStart );
else
*psDirName = sPath.Mid( nStart, nEnd-nStart);
return true;
}
/////////////////////////////////////////////////////////////////////////////
//FUNCTION
// Setup inital Dialog Data.
//CREATED
// 8-5-1999 19:57:09
//RETURNS
// See help
BOOL CDlgGetPath::OnInitDialog()
{
TRACE( _T("CDlgGetPath::OnInitDialog()\n") );
CDialog::OnInitDialog();
//
//Setup the Image lists
//
if( !m_ImageListTree.Create( IDB_FILE_TYPES, 17, 1, RGB( 255, 255, 255 ) ) )
TRACE( _T("Image list creation fault") );
m_Tree.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.
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_Tree.GetChildItem( hCurrent );
while( hItem )
{
if( sDirName.CompareNoCase( m_Tree.GetItemText( hItem ) ) == 0 )
{
hCurrent = hItem;
m_Tree.Expand( hCurrent, TVE_EXPAND );
break;
}
hItem = m_Tree.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 CDlgGetPath::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_LAST;
InsertStruct.itemex.mask = TVIF_IMAGE | TVIF_TEXT | TVIF_CHILDREN |
TVIF_SELECTEDIMAGE | TVIF_PARAM;
InsertStruct.itemex.pszText = sText.GetBuffer( sText.GetLength() ); sText.ReleaseBuffer();
InsertStruct.itemex.iImage = iImage;
InsertStruct.itemex.cChildren= 1;
InsertStruct.itemex.lParam = (LPARAM)pNetResource;
if( iImageSelected == -1 )
InsertStruct.itemex.iSelectedImage = iImage;
else
InsertStruct.itemex.iSelectedImage = iImageSelected;
return m_Tree.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 CDlgGetPath::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_Tree.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_Tree.SetItem( &item );
}
return bGotChildren;
}
/////////////////////////////////////////////////////////////////////////////
//FUNCTION
// 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 CDlgGetPath::GetItemPath( HTREEITEM hItem )
{
TRACE( _T("CDlgGetPath::GetItemPath(%p)\n"), hItem );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -