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

📄 cshellfileop.cpp

📁 一个不同目录中多个文件拷贝的助手程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////
//
// CShellFileOp.cpp: implementation of the CShellFileOp class.
//
//////////////////////////////////////////////////////////////////////
//
// This class written and copyright by Michael Dunn (mdunn at inreach
// dot com).  You may freely use and redistribute this source code and
// accompanying documentation as long as this notice is retained.
//
// Contact me if you have any questions, comments, or bug reports.  My 
// homepage is at http://home.inreach.com/mdunn/
//
//////////////////////////////////////////////////////////////////////
// 
// Revision history:
//  Oct 11, 1998: Version 1.0: First release.
//
//  Feb 27, 2000: Version 1.1: Fixed a bug in CShellFileOp::Go() that
//  would allocate twice the memory needed in Unicode builds.  The 'new'
//  statements now allocate BYTEs instead of TCHARs.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "shlobj.h"
#include "objbase.h"
#include "CShellFileOp.h"

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


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShellFileOp::CShellFileOp()
{
    ResetInternalData();
}

CShellFileOp::~CShellFileOp()
{
}


//////////////////////////////////////////////////////////////////////
// Public operations
//////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::AddSourceFile
//
// Description:
//  Adds a file/dir to the list of source files for the next file operation.
//
// Input:
//  szPath: [in] The path to the file/dir to be added.
//
// Returns:
//  TRUE if the path was added successfully.  A CMemoryException will be
//  thrown in an out-of-memory condition.
//
/////////////////////////////////////////////////////////////////////////////

BOOL CShellFileOp::AddSourceFile ( LPCTSTR szPath )
{
    ASSERT ( AfxIsValidString ( szPath ) );

    try
        {
        m_lcstrSourceFiles.AddTail ( szPath );
        }
    catch ( CMemoryException )
        {
        TRACE0("Memory exception in CShellFileOp::AddSourceFile()!\n");
        throw;
        }

    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::AddDestFile
//
// Description:
//  Adds a file/dir to the list of destination files for the next file
//  operation.
//
// Input:
//  szPath: [in] The path to the file/dir to be added.
//
// Returns:
//  TRUE if the path was added successfully.  A CMemoryException will be
//  thrown in an out-of-memory condition.
//
/////////////////////////////////////////////////////////////////////////////

BOOL CShellFileOp::AddDestFile ( LPCTSTR szPath )
{
    ASSERT ( AfxIsValidString ( szPath ) );

    try
        {
        m_lcstrDestFiles.AddTail ( szPath );
        }
    catch ( CMemoryException )
        {
        TRACE0("Memory exception in CShellFileOp::AddDestFile()!\n");
        throw;
        }

    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::GetSourceFileList
//
// Description:
//  Returns a reference to the internal list of source files for the next
//  file operation.
//
// Input:
//  Nothing.
//
// Returns:
//  A CStringList reference.  Mess with this at your own risk. :)
//
/////////////////////////////////////////////////////////////////////////////

const CStringList& CShellFileOp::GetSourceFileList()
{
    return m_lcstrSourceFiles;
}


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::GetDestFileList
//
// Description:
//  Returns a reference to the internal list of destination files for the next
//  file operation.
//
// Input:
//  Nothing.
//
// Returns:
//  A CStringList reference.  Mess with this at your own risk. :)
//
/////////////////////////////////////////////////////////////////////////////

const CStringList& CShellFileOp::GetDestFileList()
{
    return m_lcstrDestFiles;
}


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::SetOperationFlags
//
// Description:
//  Sets various parameters for the next file operation.
//
// Input:
//  uOpType: [in] FO_COPY, FO_DELETE, FO_MOVE, or FO_RENAME.
//  pWnd: [in] Pointer to the CWnd which will the parent window for any UI that
//             the shell displays during the operation.
//  Along with a bunch of other flags whose names I hope are self-explanatory.
//  They're explained in full in the documentation.
//
// Returns:
//  Nothing.
//
/////////////////////////////////////////////////////////////////////////////

void CShellFileOp::SetOperationFlags ( UINT  uOpType,
                                       CWnd* pWnd,
                                       BOOL  bSilent,
                                       BOOL  bAllowUndo,
                                       BOOL  bWildcardFilesOnly,
                                       BOOL  bNoConfirmation,
                                       BOOL  bNoConfirmationToMakeDir,
                                       BOOL  bRenameOnCollision,
                                       BOOL  bSimpleProgressDlg )
{
FILEOP_FLAGS fFlags = 0;

    // Validate the operation type.  If this assert fires, you sent in
    // an invalid op type.

    ASSERT ( uOpType == FO_COPY  ||  uOpType == FO_DELETE  ||
             uOpType == FO_MOVE  ||  uOpType == FO_RENAME );
    ASSERT_VALID ( pWnd );

                                        // store the op type
    m_rFOS.wFunc = uOpType;

                                        // store the parent window
    m_rFOS.hwnd = pWnd->GetSafeHwnd();

                                        // set the various flags...
    if ( bSilent )                  fFlags |= FOF_SILENT;
    if ( bAllowUndo )               fFlags |= FOF_ALLOWUNDO;
    if ( bWildcardFilesOnly )       fFlags |= FOF_FILESONLY;
    if ( bNoConfirmation )          fFlags |= FOF_NOCONFIRMATION;
    if ( bNoConfirmationToMakeDir ) fFlags |= FOF_NOCONFIRMMKDIR;
    if ( bRenameOnCollision )       fFlags |= FOF_RENAMEONCOLLISION;
    if ( bSimpleProgressDlg )       fFlags |= FOF_SIMPLEPROGRESS;

    m_rFOS.fFlags = fFlags;

    m_bFlagsSet = TRUE;
}


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::SetOperationFlags
//
// Description:
//  Sets various parameters for the next file operation.
//
// Input:
//  uOpType: [in] FO_COPY, FO_DELETE, FO_MOVE, or FO_RENAME.
//  pWnd: [in] Pointer to the CWnd which will the parent window for any UI that
//             the shell displays during the operation.
//  fFlags: [in] Any legal combination of the FOF_* flags.  See the docs for
//               SHFileOperation() for info on the flags.
//
// Returns:
//  Nothing.
//
/////////////////////////////////////////////////////////////////////////////

void CShellFileOp::SetOperationFlags ( UINT uOpType, CWnd* pWnd,
                                       FILEOP_FLAGS fFlags )
{
    // Validate the op type.  If this assert fires, check the operation
    // type param that you're passing in.
    ASSERT ( uOpType == FO_COPY  ||  uOpType == FO_DELETE  ||
             uOpType == FO_MOVE  ||  uOpType == FO_RENAME );
    ASSERT_VALID ( pWnd );

                                        // store the op type
    m_rFOS.wFunc = uOpType;

                                        // store the parent window
    m_rFOS.hwnd = pWnd->GetSafeHwnd();

                                        // store the op flags
    m_rFOS.fFlags = fFlags;

    m_bFlagsSet = TRUE;
}


/////////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::SetProgressDlgTitle
//
// Description:
//  Sets the string to be used if the simple progress dialog is used for
//  the next file operation.
//
// Input:
//  szTitle: [in] The string to use.
//
// Returns:
//  Nothing.
//
// Note:
//  The object maintains its own copy of the string in a CString, so the 
//  caller can destroy or reuse its string once this function returns.
//
/////////////////////////////////////////////////////////////////////////////

void CShellFileOp::SetProgressDlgTitle ( LPCTSTR szTitle )
{
    ASSERT ( AfxIsValidString ( szTitle ) );

    try
        {
        m_cstrProgressDlgTitle = szTitle;
        }
    catch ( CMemoryException )
        {
        TRACE0("Memory exception in CShellFileOp::SetProgressDlgTitle()!\n");
        throw;
        }
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::AnyOperationsAborted
//
// Description:
//  Returns a flag indicating whether the user canceled the last file operation.
//
// Input:
//  Nothing.
//
// Returns:
//  TRUE if the user canceled the last file op, or FALSE if not.
//
//////////////////////////////////////////////////////////////////////////

BOOL CShellFileOp::AnyOperationsAborted()
{
    // If this assert fires, it means you called this member function
    // before calling Go(), or you did call Go() but Go() couldn't call
    // the SHFileOperation() API due to incomplete or invalid infomation.
    // You should have gotten an assert in Go() in the latter case - that's
    // where you should be looking for the source of the trouble. :)

    ASSERT ( m_bGoCalledAPI );

    return m_rFOS.fAnyOperationsAborted;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:    CShellFileOp::Reset
//
// Description:
//  Resets internal data used for file operations.
//
// Input:
//  Nothing.
//
// Returns:
//  Nothing.
//
// Note:
//  If you are using a CShellFileOp object to do multiple operations, call
//  this function after one operation to clear out all data from the

⌨️ 快捷键说明

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