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

📄 shelloperation.cpp

📁 第14章 MFC文件操作.rar 第14章 MFC文件操作.rar 第14章 MFC文件操作.rar
💻 CPP
字号:
// ShellOperation.cpp : implementation file
//

#include "stdafx.h"
#include "Chapter14.h"
#include "ShellOperation.h"

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

/////////////////////////////////////////////////////////////////////////////
// CShellOperation dialog


CShellOperation::CShellOperation(CWnd* pParent /*=NULL*/)
	: CDialog(CShellOperation::IDD, pParent)
{
	//{{AFX_DATA_INIT(CShellOperation)
	m_desPath = _T("");
	m_sourcePath = _T("");
	//}}AFX_DATA_INIT
}


void CShellOperation::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShellOperation)
	DDX_Text(pDX, IDC_des, m_desPath);
	DDX_Text(pDX, IDC_source, m_sourcePath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CShellOperation, CDialog)
	//{{AFX_MSG_MAP(CShellOperation)
	ON_BN_CLICKED(IDC_shell_des, Onshelldes)
	ON_BN_CLICKED(IDC_shell_source, Onshellsource)
	ON_CBN_SELCHANGE(IDC_function, OnSelchangefunction)
	ON_BN_CLICKED(IDC_execute, Onexecute)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShellOperation message handlers

BOOL CShellOperation::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//初始化操作类别索引
	iIndex=0;
	CComboBox* pCobo=(CComboBox*)GetDlgItem(IDC_function);
	//设置默认的操作类别索引
	pCobo->SetCurSel(iIndex);
	//其他初始化
	fileOpStruct.hwnd=GetSafeHwnd();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CShellOperation::Onshelldes() 
{
	// TODO: Add your control notification handler code here
	//声明一个文件夹浏览结构
	LPBROWSEINFO lpbi=new BROWSEINFO;	
	lpbi->hwndOwner=GetSafeHwnd();//NULL;
	lpbi->pidlRoot=NULL;
	lpbi->pszDisplayName=NULL;
	lpbi->lpszTitle="请选择要移动或者复制的目的地:";
	lpbi->ulFlags=BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
	lpbi->lpfn=NULL;
	//显示外壳文件夹以便用户选择
    LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi); 	
	if(lpitemidlist==NULL)
	{
		delete lpbi;
		lpbi = NULL;
		return;
	}
	char path[MAX_PATH];
	//转换项目标志符列表为一个系统文件路径
	SHGetPathFromIDList(lpitemidlist,path);
	delete lpbi;
	m_desPath = path;
    UpdateData(FALSE);
}

void CShellOperation::Onshellsource() 
{
	// TODO: Add your control notification handler code here
	//此函数用来选择源文件
	CString strFilter;
	strFilter="All Files(*.*)|*.*||";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|
		OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
	
	dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
	if(dlg.DoModal() == IDOK )
	{
		m_sourcePath=dlg.GetPathName();
	}
    UpdateData(FALSE);
}


void CShellOperation::OnSelchangefunction() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pCobo=(CComboBox*)GetDlgItem(IDC_function);
	//获取当前准备进行操作的类别
	iIndex=pCobo->GetCurSel();
	switch(iIndex)
	{
	case 1:
		{
			//删除操作时,将目的文件相关的控件隐藏,有三个基本控件
			CEdit* pWnd=(CEdit*)GetDlgItem(IDC_des);
			pWnd->ShowWindow(SW_HIDE);
			pWnd=(CEdit*)GetDlgItem(IDC_shell_des);
			pWnd->ShowWindow(SW_HIDE);//
			pWnd=(CEdit*)GetDlgItem(IDC_STATIC_des);
			pWnd->ShowWindow(SW_HIDE);//
			break;
		}
	case 0:
	case 2:
		{
			//其他操作时,将目的文件相关的控件正常显示
			CEdit* pWnd=(CEdit*)GetDlgItem(IDC_des);
			pWnd->ShowWindow(SW_SHOW);
			pWnd=(CEdit*)GetDlgItem(IDC_shell_des);
			pWnd->ShowWindow(SW_SHOW);//
			pWnd=(CEdit*)GetDlgItem(IDC_STATIC_des);
			pWnd->ShowWindow(SW_SHOW);//
			break;
		}		
	}

}

void CShellOperation::Onexecute() 
{
	// TODO: Add your control notification handler code here
	UpdateData(FALSE);
	DWORD errorCode;
	//错误提示消息
	CString strMsg,strHint;
	CString strPre,strNxt;
	//源文件目的文件路径信息
	CString strTem1,strTem2;
	strTem1=m_sourcePath;
	strTem2=m_desPath;
	//由于fileOpStruct.pFrom的特殊要求,其必须以null结尾,否则不会成功
	//故这里需要在尾部加上null字符
	m_sourcePath.Insert(m_sourcePath.GetLength()+1,'\0');
	m_desPath.Insert(m_desPath.GetLength()+1,'\0');
	fileOpStruct.pFrom=m_sourcePath;
	fileOpStruct.pTo=m_desPath;
	int k=m_desPath.GetLength();
	switch(iIndex)
	{
		case 0://复制
			{
				//准备提示信息
				strPre="将文件";
				strNxt="中!";
				strPre=strPre+strTem1+"复制到"+strTem2+strNxt;
				strHint="复制被中断";
				strMsg="复制出错,其代码为:";
				//填充结构的成员
				fileOpStruct.lpszProgressTitle=strPre;
				fileOpStruct.wFunc=FO_COPY;
				break;
			}
		case 1://删除
			{
				//准备提示信息
				strPre="将文件";
				strNxt="删除!";
				strPre=strPre+strTem1+strNxt;
				strHint="删除被中断";
				strMsg="删除出错,其代码为:";
				//填充结构的成员
				fileOpStruct.lpszProgressTitle=strPre;
				fileOpStruct.wFunc=FO_DELETE;
				break;
			}
		case 2://移动
			{
				//准备提示信息
				strPre="将文件";
				strNxt="中!";
				strPre=strPre+strTem1+"移动到"+strTem2+strNxt;
				strHint="移动被中断";
				strMsg="移动出错,其代码为:";
				//填充结构的成员
				fileOpStruct.lpszProgressTitle=strPre;
				fileOpStruct.wFunc=FO_MOVE;
				break;
			}
		default:
			break;
	}

	//填充结构成员
	fileOpStruct.fFlags=FOF_SIMPLEPROGRESS;	
	int res=SHFileOperation(&fileOpStruct);
	errorCode=GetLastError();
	if(fileOpStruct.fAnyOperationsAborted==TRUE)
	{
		//提示中断信息
		AfxMessageBox(strHint);
	}
	else
	{
		//如果操作出错
		if(res!=0)
		{
			CString strTem;
			strTem.Format("%s%d",strMsg,errorCode);
			AfxMessageBox(strMsg);
		}
	}
	//返回
	return;
}

⌨️ 快捷键说明

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