DirListCtrl.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:18k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // DirListCtrl.cpp : implementation file
- //
- /*********************************************
- **该文件是属于WolfFTP工程中的。如果有什么问题
- **请联系
- ** tablejiang@21cn.com
- **或者访问
- ** http://wolfftp.51.net
- **以得到最新的支持。
- *********************************************/
- #include "stdafx.h"
- #include "DirListCtrl.h"
- #include "SortStringArray.h"
- #include "shlwapi.h"
- #include "InputFileNameDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDirListCtrl
- CDirListCtrl::CDirListCtrl()
- {
- }
- CDirListCtrl::~CDirListCtrl()
- {
- m_DirImageList.Detach( ) ;
- m_DirImageList.m_hImageList = NULL ;
- }
- BEGIN_MESSAGE_MAP(CDirListCtrl, CListCtrl)
- //{{AFX_MSG_MAP(CDirListCtrl)
- ON_WM_CREATE()
- ON_WM_LBUTTONDOWN()
- ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
- ON_WM_KEYDOWN()
- ON_WM_RBUTTONDOWN()
- ON_WM_RBUTTONUP()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDirListCtrl message handlers
- int CDirListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- //get system default image list .
- GetSysImageList() ;
- InitList();
- return 0;
- }
- void CDirListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CListCtrl::OnLButtonDown(nFlags, point);
- }
- void CDirListCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- *pResult = 0;
- EnterSubDirectory( );
- }
- /****************************************************
- ** @Description
- ** This function get the system image list .
- **
- ** @Parameter
- ** void
- **
- ** @Return: if get success return true ,else return false .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::GetSysImageList()
- {
- SHFILEINFO shFinfo;
- HIMAGELIST hImgList = NULL;
- //get system image list handle .
- hImgList = (HIMAGELIST)SHGetFileInfo( "C:\",
- 0,
- &shFinfo,
- sizeof( shFinfo ),
- SHGFI_SYSICONINDEX |
- SHGFI_SMALLICON );
- if ( !hImgList )
- {
- return FALSE;
- }
- //attach the system image list .
- //m_DirImageList.Attach( hImgList ) ;
- m_DirImageList.m_hImageList = hImgList ;
- //attach the image list with CListCtrl .
- SetImageList( &m_DirImageList , LVSIL_SMALL ) ;
- return TRUE; // OK
- }
- /****************************************************
- ** @Description
- ** This public function is display all item in a directory
- the directory's path is stored int strPath var.
- **
- ** @Parameter
- ** LPCTSTR strPath : the directory's path
- **
- ** @Return: true
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::DisplayPath(LPCTSTR strPath)
- {
- CFileFind find ;
- CString Path = strPath ;
- BOOL bFind ;
- CSortStringArray strDirArray ;
- CSortStringArray strFileArray ;
- //must delete the old list item , before display the new path.
- DeleteAllItems( ) ;
- //Is the end char '' ? if not add it .
- if( Path.Right( 1 ) != '\' )
- Path += '\' ;
- //store the new path .
- m_NowListPath = Path ;
- //find the sub directroy in this directory .
- Path += "*.*" ;
- bFind = find.FindFile( Path ) ;
- while( bFind )
- {
- bFind = find.FindNextFile( ) ;
- if( find.IsDirectory() && !find.IsDots( ) )
- {
- //is directory .
- strDirArray.Add( find.GetFilePath() ) ;
- }
- if ( !find.IsDirectory() )
- {
- //is file .
- strFileArray.Add( find.GetFilePath() );
- }
- }
- //sort the item .
- strDirArray.Sort() ;
- m_dwItemNum = 0 ;
- //let the screen don't refurbish ,when insert item .
- SetRedraw( FALSE ) ;
- int i = 0 ;
- //add directory .
- for( i = 0 ; i < strDirArray.GetSize() ; i ++ )
- AddItem( strDirArray.GetAt( i ) );
- //add file.
- for( i = 0 ; i < strFileArray.GetSize() ; i ++ )
- AddItem( strFileArray.GetAt( i ) );
- SetRedraw( TRUE ) ;
- //notify the parent , the current directroy has changed.
- ::PostMessage( m_hParentWnd , LIST_CUR_DIR_CHANGED_MSG , 0 , 0 ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- ** this protected function is used for add a item to
- list .
- **
- ** @Parameter
- ** LPCTSTR strPath : the item path .
- **
- ** @Return: if success return true ,else false .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::AddItem(LPCTSTR strPath )
- {
- SHFILEINFO shFileInfo ;
- int iIcon ;
- CString strTemp = strPath ;
- //add ''
- if ( strTemp.Right(1) != '\' )
- strTemp += "\";
- //get the system icon index .
- if ( !SHGetFileInfo( strTemp,
- 0,
- &shFileInfo,
- sizeof( shFileInfo ),
- SHGFI_ICON | SHGFI_SMALLICON |
- SHGFI_TYPENAME |SHGFI_DISPLAYNAME ) )
- {
- return false ;
- }
- iIcon = shFileInfo.iIcon;
- // we only need the index from the system image list
- DestroyIcon( shFileInfo.hIcon );
- //del the ''
- if ( strTemp.Right(1) == "\" )
- strTemp.SetAt( strTemp.GetLength() - 1, ' ' );
- //insert this item . need image and text .
- InsertItem( LVIF_IMAGE | LVIF_TEXT , m_dwItemNum , GetDisplayString( strTemp ) , 0 , 0 ,
- iIcon , 0 ) ;
- //because we need the file detailed info , so open
- //the file read these .
- // CreateFile () function use FILE_FLAG_BACKUP_SEMANTICS flags can
- // open the directory .
- HANDLE hFile = CreateFile( (LPCTSTR)strTemp , 0 ,
- FILE_SHARE_READ | FILE_SHARE_WRITE , NULL , OPEN_EXISTING ,
- FILE_FLAG_BACKUP_SEMANTICS/*FILE_ATTRIBUTE_NORMAL*/ , 0 ) ;
- DWORD FileSizeHigh ;
- DWORD FileSizeLow ;
- FILETIME FileTime ;
- SYSTEMTIME SysTime ;
- char AddString[MAX_PATH] ;
- //get the file detailed info .
- if( hFile != INVALID_HANDLE_VALUE )
- {
- //file size
- FileSizeLow = GetFileSize( hFile , &FileSizeHigh ) ;
- if( FileSizeHigh != 0 )
- sprintf( AddString , "%d%d" , FileSizeHigh , FileSizeLow ) ;
- else
- sprintf( AddString , "%d" , FileSizeLow ) ;
- SetItemText( m_dwItemNum , 1 , AddString ) ;
- //file time
- GetFileTime( hFile , NULL , NULL , &FileTime ) ;
- FileTimeToSystemTime( &FileTime , &SysTime ) ;
- sprintf( AddString , "%d-%d-%d %d:%d" , SysTime.wYear , SysTime.wMonth ,
- SysTime.wDay , SysTime.wHour , SysTime.wMinute ) ;
- SetItemText( m_dwItemNum , 3 , AddString ) ;
- }
- CloseHandle( hFile ) ;
- hFile = NULL ;
- //file type
- SetItemText( m_dwItemNum , 2 , shFileInfo.szTypeName ) ;
- m_dwItemNum ++ ;
- return true ;
- }
- /****************************************************
- ** @Description
- ** Init the list , insert column .
- **
- ** @Parameter
- ** void
- **
- ** @Return: true ;
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::InitList()
- {
- RECT rect ;
- GetClientRect( &rect ) ;
- int iLength = ( rect.right - rect.left - 20 ) / 5 ;
- //insert Column
- InsertColumn( 0 , "名称" , LVCFMT_LEFT , iLength * 2 ) ;
- InsertColumn( 1 , "大小" , LVCFMT_RIGHT , iLength ) ;
- InsertColumn( 2 , "类型" , LVCFMT_LEFT, iLength ) ;
- InsertColumn( 3 , "修改时间" , LVCFMT_LEFT, iLength ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- ** getting the last SubPath from a PathString
- e.g. C:tempreadme.txt
- the result = readme.txt
- **
- ** @Parameter
- ** LPCTSTR strPath : the path .
- **
- ** @Return: the result string ;
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- LPCTSTR CDirListCtrl::GetDisplayString(LPCTSTR strPath)
- {
- static CString strTemp;
- int iPos;
- strTemp = strPath;
- if ( strTemp.Right(1) == '\' )
- strTemp.SetAt( strTemp.GetLength() - 1, ' ' );
- iPos = strTemp.ReverseFind( '\' );
- if ( iPos != -1 )
- strTemp = strTemp.Mid( iPos + 1);
- return (LPCTSTR)strTemp;
- }
- /****************************************************
- ** @Description
- ** this public function is use for enter father directroy .
- and refurbish the list .
- **
- ** @Parameter
- ** void
- **
- ** @Return: true .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::UpDirectory()
- {
- CString strDisplayPath ;
- strDisplayPath = GetFatherDirectory( m_NowListPath ) ;
- if( strDisplayPath == m_NowListPath )
- return true ;
- DisplayPath( strDisplayPath ) ;
- return true ;
- }
- /****************************************************
- ** @Description
- ** this protected function is used for get father path .
- ** e.g. C:windowstemp.
- the result: C:windows
- but : e.g. C:
- result is C:
- ** @Parameter
- ** LPCTSTR szPath : the input path .
- **
- ** @Return: the result path .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- CString CDirListCtrl::GetFatherDirectory(LPCTSTR szPath)
- {
- CString Path = szPath ;
- if( Path.Right( 1 ) == "\" )
- {
- Path.SetAt( Path.GetLength() - 1 , ' ' ) ;
- }
- int iPos = Path.ReverseFind( '\' ) ;
- if( iPos == -1 )
- return Path ;
- else
- Path.SetAt( iPos , ' ' ) ;
- return Path ;
- }
- /****************************************************
- ** @Description
- ** this protected function is used for enter the select
- item directory .
- **
- ** @Parameter
- ** void
- **
- ** @Return: if succsee return true ,else false .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::EnterSubDirectory()
- {
- //check the select count .
- int iCount = GetSelectedCount();
- if( iCount != 1 )
- return false ;
- int iCurSel = GetNextItem( -1 , LVNI_SELECTED ) ;
- char szText[MAX_PATH] ;
- szText[0] = 0 ;
- GetItemText( iCurSel , 0 , szText , MAX_PATH ) ;
- CString strDisplayPath ;
- strDisplayPath = m_NowListPath ;
- strDisplayPath += szText ;
- //check the path name .
- //if is a file , run it .
- //if is a directory , enter it .
- if( PathIsDirectory( strDisplayPath ) )
- DisplayPath( strDisplayPath ) ;
- else
- {
- ShellExecute( NULL , "open" , strDisplayPath , NULL , NULL , SW_NORMAL ) ;
- }
- return true ;
- }
- /****************************************************
- ** @Description
- ** this public function is used for get now select path.
- **
- ** @Parameter
- ** void
- **
- ** @Return: the select path .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- CString CDirListCtrl::GetNowSelectPath()
- {
- CString strDisplayPath ;
- strDisplayPath = m_NowListPath ;
- //check the select count .
- int iCount = GetSelectedCount();
- if( iCount > 1 || iCount <= 0)
- return strDisplayPath ;
- int iCurSel = GetNextItem( -1 , LVNI_SELECTED ) ;
- char szText[MAX_PATH] ;
- szText[0] = 0 ;
- GetItemText( iCurSel , 0 , szText , MAX_PATH ) ;
- strDisplayPath += szText ;
- return strDisplayPath ;
- }
- /****************************************************
- ** @Description
- ** this public function is used for get the now list path
- **
- ** @Parameter
- ** void
- **
- ** @Return: now list path .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- CString CDirListCtrl::GetNowListPath()
- {
- char szPath[MAX_PATH] ;
- int iLength = 0 ;
- strcpy( szPath , m_NowListPath ) ;
- iLength = strlen( szPath ) ;
- if( szPath[iLength - 1 ] != '\' )
- {
- szPath[iLength] = '\';
- szPath[iLength+1] = ' ' ;
- }
- return szPath ;
- }
- /****************************************************
- ** @Description
- ** this public function is used for set the parent
- window's handle.
- **
- ** @Parameter
- ** HWND hWnd : parent window handle
- **
- ** @Return: void
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- void CDirListCtrl::SetParentWnd(HWND hWnd)
- {
- m_hParentWnd = hWnd ;
- }
- void CDirListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- switch( nChar )
- {
- case VK_BACK :
- {
- UpDirectory() ;
- }
- break ;
- case VK_RETURN :
- {
- EnterSubDirectory( );
- }
- break ;
- case VK_DELETE :
- {
- DeleteSelectItem( ) ;
- }
- break ;
- case VK_F2 :
- {
- RenameSelectItem( ) ;
- }
- break ;
- case VK_F5:
- {
- DisplayPath( m_NowListPath ) ;
- }
- break ;
- default :
- break ;
- }
- CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- /****************************************************
- ** @Description
- ** this public function is used for
- delete select file .
- **
- ** @Parameter
- ** void
- **
- ** @Return: if delete success return true ,else return false
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::DeleteSelectItem()
- {
- UINT i, uSelectedCount ;
- int nItem = -1;
- char szPath[MAX_PATH] ;
- CFileManageLib fileman ;
- strcpy( szPath , m_NowListPath) ;
- int iLength = strlen( szPath );
- if( szPath[iLength-1] != '\' )
- {
- szPath[iLength] = '\';
- szPath[iLength+1] = ' ' ;
- }
- char szDelFile[MAX_PATH] ;
- uSelectedCount = GetSelectedCount() ;
- // delete all of the selected items.
- if (uSelectedCount > 0)
- {
- for (i=0;i < uSelectedCount;i++)
- {
- memset( szDelFile , 0 , MAX_PATH ) ;
- nItem = GetNextItem(nItem, LVNI_SELECTED);
- ASSERT(nItem != -1);
- char szText[MAX_PATH] ;
- szText[0] = 0 ;
- GetItemText( nItem , 0 , szText , MAX_PATH ) ;
- sprintf( szDelFile , "%s%s" , szPath , szText ) ;
- // add file path name to delete queue .
- fileman.AddFromFileList( szDelFile ) ;
- }
- }
- //delete!
- BOOL bRet = fileman.DeleteFile( ) ;
- DisplayPath( m_NowListPath ) ;
- return bRet ;
- }
- /****************************************************
- ** @Description
- ** this public function is used for
- rename select file .
- **
- ** @Parameter
- ** void
- **
- ** @Return: if delete success return true ,else return false
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::RenameSelectItem()
- {
- UINT uSelectedCount ;
- int nItem = -1;
- char szPath[MAX_PATH] ;
- CFileManageLib fileman ;
- //check count .
- uSelectedCount = GetSelectedCount( ) ;
- if( uSelectedCount != 1 )
- return false ;
- strcpy( szPath , m_NowListPath) ;
- int iLength = strlen( szPath );
- if( szPath[iLength-1] != '\' )
- {
- szPath[iLength] = '\';
- szPath[iLength+1] = ' ' ;
- }
- char szText[MAX_PATH] ;
- szText[0] = ' ';
- //make the path .
- int iCurSel = GetNextItem( nItem , LVNI_SELECTED ) ;
- GetItemText( iCurSel , 0 , szText , MAX_PATH ) ;
- CInputFileNameDlg dlg ;
- dlg.m_strName = szText ;
- if( dlg.DoModal() == IDOK )
- {
- char szDes[MAX_PATH] ;
- char szSrc[MAX_PATH] ;
- sprintf( szDes , "%s%s" , szPath , dlg.m_strName ) ;
- sprintf( szSrc , "%s%s" , szPath , szText ) ;
- fileman.AddFromFileList( szSrc ) ;
- fileman.AddToFileList( szDes ) ;
- BOOL bRet = fileman.RenameFile( );
- DisplayPath( m_NowListPath ) ;
- return bRet ;
- }
- return false ;
- }
- /****************************************************
- ** @Description
- ** this public function is used for
- delete select file .
- **
- ** @Parameter
- ** void
- **
- ** @Return: if delete success return true ,else return false
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::MoveSelectFile( LPCTSTR szDesPath )
- {
- UINT i, uSelectedCount ;
- int nItem = -1;
- char szPath[MAX_PATH] ;
- CFileManageLib fileman ;
- strcpy( szPath , m_NowListPath) ;
- int iLength = strlen( szPath );
- if( szPath[iLength-1] != '\' )
- {
- szPath[iLength] = '\';
- szPath[iLength+1] = ' ' ;
- }
- char szSrcFile[MAX_PATH] ;
- uSelectedCount = GetSelectedCount() ;
- // move all of the selected items.
- if (uSelectedCount > 0)
- {
- for (i=0;i < uSelectedCount;i++)
- {
- memset( szSrcFile , 0 , MAX_PATH ) ;
- nItem = GetNextItem(nItem, LVNI_SELECTED);
- ASSERT(nItem != -1);
- char szText[MAX_PATH] ;
- szText[0] = 0 ;
- GetItemText( nItem , 0 , szText , MAX_PATH ) ;
- sprintf( szSrcFile , "%s%s" , szPath , szText) ;
- if( strcmp( szSrcFile , szDesPath ) != 0 )
- {
- fileman.AddFromFileList( szSrcFile ) ;
- fileman.AddToFileList( szDesPath ) ;
- }
- }
- }
- BOOL bRet = fileman.MoveFile( ) ;
- DisplayPath( m_NowListPath ) ;
- return bRet ;
- }
- void CDirListCtrl::OnRButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- ::PostMessage( m_hParentWnd , DIR_LIST_RCLK_MSG , 0 , 0 ) ;
- CListCtrl::OnRButtonDown(nFlags, point);
- }
- void CDirListCtrl::OnRButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CListCtrl::OnRButtonUp(nFlags, point);
- }
- void CDirListCtrl::OnDestroy()
- {
- //must detach the system image list .
- //because the object will be destroy , the
- //system image list will be destroy too.
- SetImageList( NULL , LVSIL_SMALL ) ;
- m_DirImageList.Detach( ) ;
- CListCtrl::OnDestroy();
- // TODO: Add your message handler code here
- }
- /****************************************************
- ** @Description
- ** this function set all item selected status.
- **
- ** @Parameter
- ** void
- **
- ** @Return: true .
- ** @Author: Table.JHM.太子
- ** e-mail: tablejiang@21cn.com
- ** @Date: 2001 3 26
- ****************************************************/
- BOOL CDirListCtrl::SelectAllItem()
- {
- int iCount = GetItemCount( ) ;
- if( iCount == 0 )
- return true ;
- for( int i = 0 ; i < iCount ; i ++ )
- {
- SetItemState( i , LVIS_SELECTED , LVIS_SELECTED ) ;
- }
- return true ;
- }