DriverCombo.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:13k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // DriverCombo.cpp : implementation file
- //
- /*********************************************
- **该文件是属于WolfFTP工程中的。如果有什么问题
- **请联系
- ** tablejiang@21cn.com
- **或者访问
- ** http://wolfftp.51.net
- **以得到最新的支持。
- *********************************************/
- #include "stdafx.h"
- #include "DriverCombo.h"
- #include "shlwapi.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDriverCombo
- CDriverCombo::CDriverCombo()
- {
- //empty buffer .
- m_NowDisplayPath.Empty( ) ;
- }
- CDriverCombo::~CDriverCombo()
- {
- m_ImageList.Detach( ) ;
- m_ImageList.m_hImageList = NULL ;
- }
- BEGIN_MESSAGE_MAP(CDriverCombo, CComboBoxEx)
- //{{AFX_MSG_MAP(CDriverCombo)
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDriverCombo message handlers
- int CDriverCombo::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CComboBoxEx::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- InitCombox( ) ;
- //if count > 0 select the item .
- return 0;
- }
- /***********************************************************
- ** @Description:
- ** this protected function is used for init the
- combobox.
- ** @Note:
- ** void
- ** @return :if init success return true ,else return false .
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::InitCombox()
- {
- //get system image list .
- GetSysImgList() ;
- //add driver item .
- AddDriver( );
- //add directroy item .
- AddDirectory( m_NowDisplayPath ) ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** Get system default image list.
- ** @Note:
- ** void
- ** @Return: if get success return true ,else return false .
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::GetSysImgList()
- {
- SHFILEINFO shFinfo;
- HIMAGELIST hImgList = NULL;
- if ( GetImageList( ) )
- m_ImageList.Detach();
- //get system image list handle .
- hImgList = (HIMAGELIST)SHGetFileInfo( "C:\",
- 0,
- &shFinfo,
- sizeof( shFinfo ),
- SHGFI_SYSICONINDEX |
- SHGFI_SMALLICON );
- if ( !hImgList )
- {
- return FALSE;
- }
- //attach .
- m_ImageList.m_hImageList = hImgList ;
- SetImageList( &m_ImageList ) ;
- return TRUE;
- }
- /***********************************************************
- ** @Description:
- ** use add the system drivers into combo box.
- ** @Note:
- ** void
- ** @Return: if add success return true , else return false .
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::AddDriver()
- {
- char szDrives[128];
- char* pDrive;
- ZeroMemory( szDrives , 128 ) ;
- //get the system logical driver info .
- //the result string such as "A:\0B:\0C:\0" .
- if ( !GetLogicalDriveStrings( sizeof(szDrives), szDrives ) )
- {
- return FALSE;
- }
- pDrive = szDrives;
- //Convert string to uppercase.
- pDrive = strupr( pDrive ) ;
- int nItem = 0 ;
- CString DisplayPath = m_NowDisplayPath ;
- CString driver ;
- DisplayPath.MakeLower() ;
- //add all driver .
- while( *pDrive )
- {
- AddItem( pDrive , 0 , nItem );
- pDrive += strlen( pDrive ) + 1;
- nItem ++ ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** this function is add the specify path item in combo.
- ** @Note:
- ** CString strPath : the specify path .
- ** @Return: if add success return true , else return false .
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::AddDirectory( CString strPath )
- {
- //find the directroy item insert pos
- int iPos = FindInsertPos( strPath ) ;
- //insert .
- if( InsertPathItem( iPos ) )
- return true ;
- else
- return false ;
- }
- /***********************************************************
- ** @Description:
- ** the function find the directory will insert in
- which driver .
- ** @Note:
- ** CString strPath : the display path .
- ** @Return: the driver index in combo.
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- int CDriverCombo::FindInsertPos( CString strPath )
- {
- char szDrives[128];
- char* pDrive;
- //get driver info.
- if ( !GetLogicalDriveStrings( sizeof(szDrives), szDrives ) )
- {
- return FALSE;
- }
- pDrive = szDrives;
- pDrive = strupr( pDrive ) ;
- m_nItem = 0 ;
- int iPos = 0 ;
- CString DisplayPath = m_NowDisplayPath ;
- CString driver ;
- DisplayPath.MakeLower() ;
- while( *pDrive )
- {
- driver = pDrive ;
- driver.GetLength() ;
- driver.SetAt( 2 , ' ' ) ;
- driver.MakeLower( ) ;
- //if this driver same to display path drvier.
- if( strcmp( driver , DisplayPath.Left( 2 ) ) == 0 )
- {
- return iPos ;
- break ;
- }
- //compare next driver.
- pDrive += strlen( pDrive ) + 1;
- iPos ++ ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** this protected function is add the item to combo.
- ** @Note:
- **
- ** @Return:
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::AddItem(LPCTSTR pDriver , int level , int iPos )
- {
- SHFILEINFO shFileInfo ;
- int iIcon , iSelIcon ;
- CString strTemp = pDriver ;
- //add the char ''
- if ( strTemp.Right(1) != '\' )
- strTemp += "\";
- //get the item icon index from system image list.
- 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 );
- //get the item selected icon index from system image list .
- if ( !SHGetFileInfo( strTemp,
- 0,
- &shFileInfo,
- sizeof( shFileInfo ),
- SHGFI_ICON | SHGFI_SMALLICON
- ) )
- {
- return false ;
- }
- iSelIcon = shFileInfo.iIcon ;
- // we only need the index from the system image list
- DestroyIcon( shFileInfo.hIcon ) ;
- if ( strTemp.Right(1) == "\" )
- strTemp.SetAt( strTemp.GetLength() - 1, ' ' );
- //insert item .
- COMBOBOXEXITEM cbi;
- CString DisplayString ;
- DisplayString = GetDisplayPath( strTemp );
- cbi.mask = CBEIF_IMAGE | CBEIF_INDENT | CBEIF_OVERLAY |
- CBEIF_SELECTEDIMAGE | CBEIF_TEXT;
- if( level == 0 )
- DisplayString.MakeUpper() ;
- cbi.iItem = iPos ;
- cbi.pszText = (LPTSTR)(LPCTSTR)DisplayString ;
- cbi.cchTextMax = DisplayString.GetLength() ;
- cbi.iImage = iIcon ;
- cbi.iSelectedImage = iSelIcon ;
- cbi.iOverlay = 0 ;
- //make indent .
- cbi.iIndent = ( level*0x0002 ) ;
- InsertItem( &cbi ) ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** insert the directory item under the specify driver item .
- ** @Note:
- ** int iPos : the driver index .
- ** @Return:
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::InsertPathItem( int iPos )
- {
- m_nLevel = 0 ;
- CString strDisPath ;
- CString strNowPath ;
- char cBackup ;
- //get now display path
- strNowPath = m_NowDisplayPath ;
- //check the path is exist ?
- if( !PathFileExists( (LPCTSTR)m_NowDisplayPath ) )
- return false ;
- //check the path is directory ?
- if( !PathIsDirectory( (LPCTSTR)m_NowDisplayPath ) )
- {
- //if not , get it parent directory
- strDisPath = GetDisplayPath ( strNowPath ) ;
- strNowPath.SetAt( strNowPath.GetLength() - strDisPath.GetLength() , 0 ) ;
- }
- //now , split the path .
- int iStart = 0 ;
- int iEnd = strNowPath.GetLength() ;
- int iLevel = 1 ;
- int nItem = iPos + 1;
- if( strNowPath.Right(1) != "\" )
- strNowPath += "\" ;
- iStart = strNowPath.Find( "\" , iStart+1 );
- while( true )
- {
- iStart = strNowPath.Find( "\" , iStart+1 );
- if( iStart == -1 )
- {
- break ;
- }
- cBackup = strNowPath.GetAt( iStart ) ;
- strNowPath.SetAt( iStart , 0 ) ;
- AddItem( strNowPath , iLevel , nItem ) ;
- iLevel ++ ;
- nItem ++ ;
- strNowPath.SetAt( iStart , cBackup ) ;
- }
- //select the path end item .
- SetCurSel( nItem - 1 ) ;
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** getting the last SubPath from a PathString
- e.g. C:tempreadme.txt
- the result = readme.txt
- ** @Note:
- ** LPCTSTR strPath : the path.
- ** @Return: the result path
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- CString CDriverCombo::GetDisplayPath(LPCTSTR strPath)
- {
- //
- // getting the last SubPath from a PathString
- // e.g. C:tempreadme.txt
- // the result = readme.txt
- 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:
- ** Delete all combo item .
- ** @Note:
- ** void
- ** @Return: true ;
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::DeleteAllItem()
- {
- int iNum ;
- iNum = GetCount() ;
- for( int i = 0 ; i < iNum ; i ++ )
- {
- DeleteItem( 0 ) ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** the function return the combo current display path.
- ** @Note:
- ** void
- ** @Return: current display path .
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- CString CDriverCombo::GetNowPath()
- {
- COMBOBOXEXITEM cbi;
- int iSel = GetCurSel() ;
- //if get error return NULL ;
- if( iSel == CB_ERR )
- return "" ;
- CString strPath ;
- char szName[MAX_PATH] ;
- strPath.Empty() ;
- int iLevel = 0 ;
- //get the path item.
- do
- {
- cbi.mask = CBEIF_IMAGE | CBEIF_INDENT | CBEIF_OVERLAY |
- CBEIF_SELECTEDIMAGE | CBEIF_TEXT;
- cbi.iItem = iSel ;
- szName[0] = 0 ;
- cbi.pszText = szName ;
- cbi.cchTextMax = MAX_PATH ;
- if( GetItem( &cbi ) )
- {
- iLevel = cbi.iIndent / 2 ;
- strPath.Insert( 0 , "\" ) ;
- strPath.Insert( 0 , cbi.pszText ) ;
- }
- else
- break ;
- iSel -- ;
- }
- while( iLevel != 0 ) ;
- if( strPath.Right(1) == "\" )
- strPath.SetAt( strPath.GetLength() - 1, ' ' );
- return strPath ;
- }
- /***********************************************************
- ** @Description:
- ** Delete the directory item .
- ** @Note:
- ** void
- ** @Return: true ;
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::DeleteDirectory()
- {
- int iNum ;
- int iLevel ;
- int iPos = 0 ;
- COMBOBOXEXITEM cbi;
- char szName[MAX_PATH] ;
- iNum = GetCount() ;
- for( int i = 0 ; i < iNum ; i ++ )
- {
- cbi.mask = CBEIF_IMAGE | CBEIF_INDENT | CBEIF_OVERLAY |
- CBEIF_SELECTEDIMAGE | CBEIF_TEXT;
- cbi.iItem = iPos ;
- szName[0] = 0 ;
- cbi.pszText = szName ;
- cbi.cchTextMax = MAX_PATH ;
- if( GetItem( &cbi ) )
- {
- iLevel = cbi.iIndent / 2 ;
- }
- else
- return false ;
- //if the item is Indent , delete it!
- if( iLevel != 0 )
- {
- //MessageBox( szName ) ;
- DeleteItem( iPos ) ;
- }
- else
- iPos ++ ;
- }
- return true ;
- }
- /***********************************************************
- ** @Description:
- ** display the specify path.
- ** @Note:
- ** LPCTSTR szPath : the path .
- ** @Return: true ;
- ** @Author :Table.JHM.太子
- ** e-mail :tablejiang@21cn.com
- ** Date :2001 3 26
- *************************************************************/
- BOOL CDriverCombo::SetDisplayStr(LPCTSTR szPath)
- {
- m_NowDisplayPath = szPath ;
- DeleteDirectory( ) ;
- AddDirectory( m_NowDisplayPath ) ;
- return true ;
- }