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

📄 foldercopydlg.cpp

📁 Visual.C++程序设计技巧与实例--配套光盘 第6章 文件和系统操作 本章共11个实例: 1. FolderCopy文件夹的选择和拷贝 2. DeleteCertainFile删除指定路
💻 CPP
字号:
// FolderCopyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FolderCopy.h"
#include "FolderCopyDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFolderCopyDlg dialog

CFolderCopyDlg::CFolderCopyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFolderCopyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFolderCopyDlg)
	m_StrDestEdit = _T("");
	m_StrSourceEdit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFolderCopyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFolderCopyDlg)
	DDX_Text(pDX, IDC_DEST_EDIT, m_StrDestEdit);
	DDX_Text(pDX, IDC_SOURCE_EDIT, m_StrSourceEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFolderCopyDlg, CDialog)
	//{{AFX_MSG_MAP(CFolderCopyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SOUCEFOLDER, OnSoucefolder)
	ON_BN_CLICKED(IDC_DESTFOLDER, OnDestfolder)
	ON_BN_CLICKED(IDC_BEGINCOPY, OnBegincopy)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFolderCopyDlg message handlers

BOOL CFolderCopyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFolderCopyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFolderCopyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFolderCopyDlg::OnSoucefolder() 
{
	m_sourcedir=GetDirectoryPath();
	m_StrSourceEdit=m_sourcedir;
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CFolderCopyDlg::OnDestfolder() 
{
	m_sourcedir=GetDirectoryPath();
	m_StrDestEdit=m_sourcedir;
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}
//此函数实现文件夹拷贝
BOOL CFolderCopyDlg::CopyFolder(CString strsource,CString strdest)
{
   SHFILEOPSTRUCT  sfo;
   
   char szSource[MAX_PATH];
   char szDest[MAX_PATH];

   lstrcpy(szSource,strsource);
   lstrcpy(szDest,strdest);
   szSource[lstrlen(szSource)+1] = 0;
   szDest[lstrlen(szDest)+1] = 0;

   sfo.hwnd = NULL;
   sfo.wFunc = FO_COPY;
   sfo.pFrom = (LPCSTR)szSource;
   sfo.pTo = (LPCSTR)szDest;
 
   sfo.fFlags =   FOF_NOCONFIRMATION |
                  FOF_SIMPLEPROGRESS |
                  FOF_NOCONFIRMMKDIR |
                  FOF_MULTIDESTFILES;
   sfo.hNameMappings = 0;
   sfo.lpszProgressTitle = "文件夹拷贝";

   int res = SHFileOperation(&sfo);
   return res;
}

//此函数得到所选文件的路径
CString CFolderCopyDlg::GetDirectoryPath()
{
	//定义文件夹的路径
    CString strPath ="";
	//定义指向IMalloc的指针
    LPMALLOC  pMalloc;  // pointer to IMalloc
    if (::SHGetMalloc(&pMalloc) != NOERROR)
        return strPath; // failed to get allocator
    char szDisplayName[_MAX_PATH];
    char szBuffer[_MAX_PATH];
    BROWSEINFO browseInfo;
    browseInfo.hwndOwner = this->m_hWnd;
    // set root at Desktop
    browseInfo.pidlRoot = NULL;
    browseInfo.pszDisplayName = szDisplayName;
    browseInfo.lpszTitle = "Select a path";  // Dialog title
    browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
    browseInfo.lpfn = NULL;      // not used
    browseInfo.lParam = 0;      // not used
    LPITEMIDLIST lpItemIDList;
    if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo))
        != NULL)
    {
		//得到
        // Get the path of the selected folder from the    item ID list.
        if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
        {
            // At this point, szBuffer contains the path the user chose.
            if (szBuffer[0] == '\0')
            {
                // SHGetPathFromIDList failed, or SHBrowseForFolder failed.
                AfxMessageBox("IDP_FAILED_GET_DIRECTORY",
                    MB_ICONSTOP|MB_OK);
                return strPath;
            }
			//返回所选的文件架
            // We have a path in szBuffer! Return it.
            strPath = szBuffer;
            return strPath;
        }
        else
        {
            // The thing referred to by lpItemIDList
            // might not have been a file system object.
            // For whatever reason, SHGetPathFromIDList didn't work!
            //AfxMessageBox("IDP_FAILED_GET_DIRECTORY",
            //    MB_ICONSTOP|MB_OK);
			AfxMessageBox("不能得到所选的文件夹!",
                MB_ICONSTOP|MB_OK);
			//返回的路径为空
            return strPath; // strPath is empty
        }
        pMalloc->Free(lpItemIDList);
        pMalloc->Release();
    }
    // If we made it this far, SHBrowseForFolder failed.
    return strPath;
} 

void CFolderCopyDlg::OnBegincopy() 
{
	if(0==CopyFolder(m_sourcedir,m_destdir) )
		AfxMessageBox("拷贝出错!");
	else
		AfxMessageBox("拷贝完成!");
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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