MFC 多线程拷贝文件开始暂停停止和进度条

如图建一个基于对话框

MFC 多线程拷贝文件开始暂停停止和进度条_第1张图片

 

代码如下 一个头文件一个cpp

// CopyFileDlg.h : 头文件
//

#pragma once
#include "afxcmn.h"
#include "afxwin.h"
UINT MyThreadFun(LPVOID pParam);

// CCopyFileDlg 对话框
class CCopyFileDlg : public CDialog
{
// 构造
public:
	CCopyFileDlg(CWnd* pParent = NULL);	// 标准构造函数

// 对话框数据
	enum { IDD = IDD_COPYFILE_DIALOG };

	HANDLE hStop;
	HANDLE hExit;

   CWinThread * pThreadpause;
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持

	

// 实现
protected:
	HICON m_hIcon;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
	
public:
	
	CString m_srcPath;
	CString m_desPath;
	afx_msg void OnBnClickedButtonsrcpath();
	afx_msg void OnBnClickedButtondespath();
	afx_msg void OnBnClickedButtonstat();
	afx_msg void OnBnClickedButtonstop();
	afx_msg void OnBnClickedButtonpause();
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	CProgressCtrl m_ProgressCopy;
	afx_msg void OnDestroy();
    afx_msg LRESULT Do_process(WPARAM wParam,LPARAM lParam); 
	afx_msg LRESULT CCopyFileDlg::ShowFile(WPARAM wParam, LPARAM lParam); 
	

	CEdit m_EditShowName;
	int m_iStatus;
};

 

 

cpp

 

#include "stdafx.h"
#include "CopyFile.h"
#include "CopyFileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
static int countset = 0;
#define WM_USER_PROCESS (WM_USER+100)
#define WM_USER_SHOW (WM_USER+101)
void myCopyDirectory(CString source, CString target,CWnd * pWndMsg);
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
void myCopyDirectory(CString source, CString target);  
VOID GetInfo(CString csPath, long& iDirCnt, long& iFileCnt);
//INT  myCount(CString source);
static  int nlpos = 0;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CCopyFileDlg 对话框




CCopyFileDlg::CCopyFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCopyFileDlg::IDD, pParent)
	, m_srcPath(_T(""))
	, m_desPath(_T(""))
	
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCopyFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_SRC_PATH, m_srcPath);
	DDX_Text(pDX, IDC_EDIT_DES_PATH, m_desPath);
	DDX_Control(pDX, IDC_PROGRESSCOPY, m_ProgressCopy);
	

	DDX_Control(pDX, IDC_EDITSHOWFILENAME, m_EditShowName);
}

BEGIN_MESSAGE_MAP(CCopyFileDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTONSRCPATH, &CCopyFileDlg::OnBnClickedButtonsrcpath)
	ON_BN_CLICKED(IDC_BUTTONDESPATH, &CCopyFileDlg::OnBnClickedButtondespath)
	ON_BN_CLICKED(IDC_BUTTONSTAT, &CCopyFileDlg::OnBnClickedButtonstat)
	ON_BN_CLICKED(IDC_BUTTONSTOP, &CCopyFileDlg::OnBnClickedButtonstop)
	ON_BN_CLICKED(IDC_BUTTONPAUSE, &CCopyFileDlg::OnBnClickedButtonpause)
	ON_WM_TIMER()
	ON_WM_DESTROY()  
	ON_MESSAGE(WM_USER_PROCESS, Do_process)
	ON_MESSAGE(WM_USER_SHOW, ShowFile)

END_MESSAGE_MAP()


// CCopyFileDlg 消息处理程序

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

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	hStop = CreateEvent(NULL, TRUE, FALSE, NULL);
	hExit = CreateEvent(NULL, TRUE, TRUE, NULL);
	
    m_iStatus = -1;	
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CCopyFileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CCopyFileDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

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

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCopyFileDlg::OnQueryDragIcon()
{
	return static_cast(m_hIcon);
}


void CCopyFileDlg::OnBnClickedButtonsrcpath()
{

	// TODO: Add your control notification handler code here
	CString		strEditSrc;
	BROWSEINFO  bi;
	wchar_t     name[MAX_PATH];
	ZeroMemory(&bi,sizeof(BROWSEINFO));
	bi.hwndOwner			= GetSafeHwnd();			//获取父窗口句柄
	bi.pszDisplayName		= name;
	bi.ulFlags				= BIF_RETURNFSANCESTORS;
	LPITEMIDLIST idl		= SHBrowseForFolder(&bi);
	if (idl	== NULL)
	{
		return;
	}
	SHGetPathFromIDList(idl,strEditSrc.GetBuffer(MAX_PATH));
	strEditSrc.ReleaseBuffer();
	SetDlgItemText(IDC_EDIT_SRC_PATH,strEditSrc);
	
	
}

void CCopyFileDlg::OnBnClickedButtondespath()
{
	// TODO: Add your control notification handler code here
	CString		strEditDes;
	BROWSEINFO  bi;
	wchar_t		name[MAX_PATH];
	ZeroMemory(&bi,sizeof(BROWSEINFO));
	bi.hwndOwner			= GetSafeHwnd();
	bi.pszDisplayName		= name;
	bi.ulFlags				= BIF_RETURNFSANCESTORS;
	LPITEMIDLIST idl		= SHBrowseForFolder(&bi);
	if(idl	== NULL)
		return;
	SHGetPathFromIDList(idl, strEditDes.GetBuffer(MAX_PATH));
	strEditDes.ReleaseBuffer();
	SetDlgItemText(IDC_EDIT_DES_PATH,strEditDes);

    
}

void CCopyFileDlg::OnBnClickedButtonstat()
{

	// TODO: Add your control notification handler code here
    
	UpdateData(true);    
   pThreadpause = AfxBeginThread(MyThreadFun, this);
	
/*

	 long iDirCnt  = 0;                //文件夹数量
	 long iFileCnt = 0;                //文件的数量
	
	GetInfo(m_srcPath,iDirCnt,iFileCnt);
	m_ProgressCopy.SetRange32(0,iFileCnt);
	m_ProgressCopy.SetStep(1);
    
	
	CString strAll;
		//strAll.Format(_T("%d"),iDirCnt);
		strAll.Format(_T("%d"),iFileCnt);
		MessageBox(strAll);
	    */

	
	

}

void CCopyFileDlg::OnBnClickedButtonstop()
{
	// TODO: Add your control notification handler code here
	    CDialog::OnCancel();	//关闭对话框

}

void CCopyFileDlg::OnBnClickedButtonpause()
{
	// TODO: Add your control notification handler code here
   
      m_iStatus++;
	   
	if (m_iStatus%2 == 0)
	{  
		pThreadpause->SuspendThread();
	} 
	else
	{
		pThreadpause->ResumeThread();
	}




}

void CCopyFileDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnTimer(nIDEvent);
}

//文件夹拷贝
void myCopyDirectory(CString source, CString target, CCopyFileDlg * pDlgMsg)  
{  
	CreateDirectory(target,NULL); 

	CFileFind finder;  
	CString path;  
	path.Format(_T("%s/*.*"),source);  

	BOOL bWorking = finder.FindFile(path);  
	while(bWorking)
	{ 
		if (WAIT_OBJECT_0 == WaitForSingleObject(pDlgMsg->hStop, 0))
		   break;

		bWorking = finder.FindNextFile(); 
		 
		if(finder.IsDirectory() && !finder.IsDots())
		{ 
			myCopyDirectory(finder.GetFilePath(),target+_T("/")+finder.GetFileName(), pDlgMsg); 
		}  
		else
		{    
			CopyFile(finder.GetFilePath(),target+_T("/")+finder.GetFileName(),FALSE); 
			countset++;
			pDlgMsg->PostMessage(WM_USER_PROCESS,countset);
			CString* msg = new CString(target+_T("/")+finder.GetFileName());
			pDlgMsg->PostMessage(WM_USER_SHOW,0,(LPARAM)msg);

			 Sleep(50); 
		} 
	}  
}  

//文件夹中文件个数计数
VOID GetInfo(CString csPath, long& iDirCnt, long& iFileCnt)
{	
	SetCurrentDirectory(csPath);
	CFileFind finder;
	BOOL bWorking = finder.FindFile(_T("*.*"));
	while(bWorking)
	{
		bWorking = finder.FindNextFile();

		if(finder.IsDots()) 
		{
			continue;
		}
		else if(finder.IsDirectory())
		{
			++iDirCnt;
			GetInfo(finder.GetFilePath(), iDirCnt, iFileCnt);
		}
		else
		{
			++iFileCnt;
		}
	}
	
}

//线程函数
UINT MyThreadFun(LPVOID pParam)
{
	CCopyFileDlg* pDlg = (CCopyFileDlg*)pParam;
	if (!pDlg)
		return 0;
	ResetEvent(pDlg->hExit);

	long siDirCnt  = 0;                //文件夹数量
	long siFileCnt = 0;                //文件的数量
	GetInfo(pDlg->m_srcPath,siDirCnt,siFileCnt);
	pDlg->m_ProgressCopy.SetRange32(0,siFileCnt);
	pDlg->m_ProgressCopy.SetStep(1);  
    myCopyDirectory(pDlg->m_srcPath, pDlg->m_desPath, pDlg); 
  
	/*
while (TRUE)
	{
		if (WAIT_OBJECT_0 == WaitForSingleObject(pDlg->hStop, 0)) 
			break;	
	}
*/

	SetEvent(pDlg->hExit);
	

	return 1;
}

void CCopyFileDlg::OnDestroy()
{
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
	SetEvent(hStop);
	WaitForSingleObject(hExit, INFINITE);
}


LRESULT CCopyFileDlg::Do_process(WPARAM wParam, LPARAM lParam) 
{
	//TRACE("WM_USER_PROCESS message /n");
  //if(wParam >0 && wParam <= 100) 
   wParam += 1;
   m_ProgressCopy.SetPos(wParam);  
	return 0;
}       

LRESULT CCopyFileDlg::ShowFile(WPARAM wParam, LPARAM lParam) 
{
     CString * pStrpath = (CString *)lParam;
	 
     GetDlgItem(IDC_EDITSHOWFILENAME)->SetWindowText(*pStrpath);
     UpdateData(FALSE);
	delete pStrpath;
	return 0;

}






 

你可能感兴趣的:(工作笔记,图形相关,MFC)