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

📄 timingcopydlg.cpp

📁 简单的实现定时复制功能
💻 CPP
字号:
// TimingCopyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TimingCopy.h"
#include "TimingCopyDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimingCopyDlg dialog

CTimingCopyDlg::CTimingCopyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTimingCopyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimingCopyDlg)
	m_iHour = 0;
	m_iMinute = 0;
	m_strSource = _T("");
	m_strDestination = _T("");
	m_iSleepTime = 1000;
	m_day = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTimingCopyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimingCopyDlg)
	DDX_Control(pDX, IDC_LIST_RESULT, m_list);
	DDX_Text(pDX, IDC_EDIT_HOUR, m_iHour);
	DDV_MinMaxInt(pDX, m_iHour, 0, 23);
	DDX_Text(pDX, IDC_EDIT_MINUTE, m_iMinute);
	DDV_MinMaxInt(pDX, m_iMinute, 0, 59);
	DDX_Text(pDX, IDC_EDIT_SOURCE, m_strSource);
	DDV_MaxChars(pDX, m_strSource, 200);
	DDX_Text(pDX, IDC_EDIT_DESTINATION, m_strDestination);
	DDV_MaxChars(pDX, m_strDestination, 200);
	DDX_Text(pDX, IDC_EDIT_SLEEPTIME, m_iSleepTime);
	DDV_MinMaxInt(pDX, m_iSleepTime, 1, 5000);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_day);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimingCopyDlg, CDialog)
	//{{AFX_MSG_MAP(CTimingCopyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SOURCE, OnButtonSource)
	ON_BN_CLICKED(IDC_BUTTON_DESTINATION, OnButtonDestination)
	ON_BN_CLICKED(IDC_BUTTON_SET, OnButtonSet)
	ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimingCopyDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

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

	// 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
	m_bIsStartDone=true;

	m_list.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
	DWORD style=m_list.GetExtendedStyle();
	m_list.SetExtendedStyle(style|LVS_EX_FULLROWSELECT|LVS_EX_FLATSB);
	m_list.InsertColumn( 0 , " " , LVCFMT_LEFT , 455 );
	//////////
	void CALLBACK OnTimer1(HWND h, UINT u1, UINT u2, DWORD d);
	int iInstallResult;
	iInstallResult = SetTimer(1, 1000, OnTimer1);
	if(iInstallResult == 0)
	{
		AfxMessageBox("Can not install timer!");
		exit(0);
	}

	CTime t;
	t=t.GetCurrentTime();
	m_iHour=t.GetHour();
	m_iMinute=t.GetMinute();
	m_day=m_day.GetCurrentTime();
	UpdateData(FALSE);
	//////////
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// 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 CTimingCopyDlg::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 CTimingCopyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTimingCopyDlg::OnButtonSource() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
	char   path[500];   
     BROWSEINFO   bi;   
     ITEMIDLIST   *   pidl;   
     bi.hwndOwner   =this->GetSafeHwnd();
     bi.iImage   =0;   
     bi.lParam   =0;   
     bi.lpfn   =0;   
     bi.lpszTitle   ="请选择文件夹:";
     bi.pidlRoot   =0;   
     bi.pszDisplayName   =0;   
     bi.ulFlags   =BIF_RETURNONLYFSDIRS;   
     pidl=SHBrowseForFolder(&bi);   
     if(SHGetPathFromIDList(pidl,path)==TRUE)
     {   
		strcat(path,"\\");
		m_strSource.Format("%s",path);
		UpdateData(FALSE);
     } 
}

void CTimingCopyDlg::OnButtonDestination() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	char   path[500];   
     BROWSEINFO   bi;   
     ITEMIDLIST   *   pidl;   
     bi.hwndOwner   =this->GetSafeHwnd();
     bi.iImage   =0;   
     bi.lParam   =0;   
     bi.lpfn   =0;   
     bi.lpszTitle   ="请选择文件夹:";
     bi.pidlRoot   =0;   
     bi.pszDisplayName   =0;   
     bi.ulFlags   =BIF_RETURNONLYFSDIRS;   
     pidl=SHBrowseForFolder(&bi);   
     if(SHGetPathFromIDList(pidl,path)==TRUE)
     {   
		strcat(path,"\\");
		m_strDestination.Format("%s",path);
		UpdateData(FALSE);
     } 
}

void CTimingCopyDlg::OnButtonSet() 
{
	// TODO: Add your control notification handler code here
	//
	CWnd *pWnd=NULL;
	pWnd=GetDlgItem(IDC_EDIT_HOUR);
	pWnd->EnableWindow(FALSE);
	pWnd=GetDlgItem(IDC_EDIT_MINUTE);
	pWnd->EnableWindow(FALSE);
	pWnd=GetDlgItem(IDC_EDIT_SOURCE);
	pWnd->EnableWindow(FALSE);
	pWnd=GetDlgItem(IDC_EDIT_DESTINATION);
	pWnd->EnableWindow(FALSE);
	pWnd=GetDlgItem(IDC_BUTTON_SOURCE);
	pWnd->EnableWindow(FALSE);
	pWnd=GetDlgItem(IDC_BUTTON_DESTINATION);
	pWnd->EnableWindow(FALSE);
	m_bIsStartDone=false;
	m_list.DeleteAllItems();
}

void CTimingCopyDlg::OnButtonReset() 
{
	// TODO: Add your control notification handler code here
	CWnd *pWnd=NULL;
	pWnd=GetDlgItem(IDC_EDIT_HOUR);
	pWnd->EnableWindow(TRUE);
	pWnd=GetDlgItem(IDC_EDIT_MINUTE);
	pWnd->EnableWindow(TRUE);
	pWnd=GetDlgItem(IDC_EDIT_SOURCE);
	pWnd->EnableWindow(TRUE);
	pWnd=GetDlgItem(IDC_EDIT_DESTINATION);
	pWnd->EnableWindow(TRUE);
	pWnd=GetDlgItem(IDC_BUTTON_SOURCE);
	pWnd->EnableWindow(TRUE);
	pWnd=GetDlgItem(IDC_BUTTON_DESTINATION);
	pWnd->EnableWindow(TRUE);
	m_bIsStartDone=true;
}

int g_iSleepTime=1000;

void CALLBACK OnTimer1(HWND h, UINT u1, UINT u2, DWORD d)
{
	CTimingCopyDlg *pMainDlg= ((CTimingCopyDlg *)(AfxGetApp()->m_pMainWnd));
	
	if(pMainDlg->m_bIsStartDone==true)
	{
		return;
	}
	
	CTime t;
	t=t.GetCurrentTime();
	pMainDlg->UpdateData(TRUE);
	//////
	if(t.GetDay()!=pMainDlg->m_day.GetDay() || t.GetMonth()!=pMainDlg->m_day.GetMonth() || t.GetYear()!=pMainDlg->m_day.GetYear())
	{
		TRACE("not today!");
		return;
	}
	//////
	if(t.GetHour()==pMainDlg->m_iHour && t.GetMinute() == pMainDlg->m_iMinute)
	{
		pMainDlg->m_bIsStartDone=true;
		
		CString strTemp;
		pMainDlg->m_iCount=0;
		pMainDlg->m_iCountDir=0;
		CString strSrc;
		CString strDes;
		pMainDlg->UpdateData(TRUE);
		strSrc=pMainDlg->m_strSource;
		strDes=pMainDlg->m_strDestination;
		void CopyDir(CString strSrcDir,CString strDesDir);
		//CopyDir(strSrc,strDes);
		///////
		DWORD WINAPI CopyThread(LPARAM lParam);
		//TwoString twoString;
		pMainDlg->twoString.str1=strSrc;
		pMainDlg->twoString.str2=strDes;
		g_iSleepTime=pMainDlg->m_iSleepTime;
		CreateThread(NULL,0,LPTHREAD_START_ROUTINE(CopyThread),LPVOID(&(pMainDlg->twoString)),0,NULL);
		///////
		//strTemp.Format("成功复制了%d个文件,创建了%d个目录",pMainDlg->m_iCount,pMainDlg->m_iCountDir);
		//pMainDlg->m_list.InsertItem(0,strTemp);
		//AfxMessageBox(strTemp);
	}
}

void CTimingCopyDlg::OnOK() 
{
	// TODO: Add extra validation here
	//CString strTemp="E:\\temp2\\abc";
	//if(CreateDirectory(strTemp,NULL))
	//{}
	//void CopyDir(CString strSrcDir,CString strDesDir);
	//CString strTemp;
	//strTemp.Format("%d",abs(-1));
	//AfxMessageBox(strTemp);
	//CopyDir("E:\\temp\\","E:\\temp2\\");
	CDialog::OnOK();
}

void CopyDir(CString strSrcDir,CString strDesDir)
{
	CTimingCopyDlg *pMainDlg= ((CTimingCopyDlg *)(AfxGetApp()->m_pMainWnd));

	CString strTemp,strTemp2;
	bool bRet = true;
	HANDLE hFile;
	WIN32_FIND_DATA fd;
	//
	strTemp.Format("%s*.*",strSrcDir);
	hFile = FindFirstFile(strTemp, &fd);
	while (hFile != INVALID_HANDLE_VALUE && bRet)
	{
		strTemp.Format("%s",fd.cFileName);
		if(strTemp=="." || strTemp=="..")
		{
			bRet = FindNextFile(hFile, &fd);
			continue;
		}
		
		if(fd.dwFileAttributes==32)
		{
			if(CopyFile(strSrcDir+strTemp,strDesDir+strTemp,1))
			{
				pMainDlg->m_iCount++;
				strTemp2.Format("复制文件成功:  %s--->%s",strSrcDir+strTemp,strDesDir+strTemp);
				pMainDlg->m_list.InsertItem(0,strTemp2);
				Sleep(g_iSleepTime);
			}
		}
		else if(fd.dwFileAttributes==16)
		{
			if(CreateDirectory(strDesDir+strTemp,NULL))
			{
				pMainDlg->m_iCountDir++;
				strTemp2.Format("创建文件夹成功:  %s",strDesDir+strTemp);
				pMainDlg->m_list.InsertItem(0,strTemp2);
				Sleep(g_iSleepTime);
				CopyDir(strSrcDir+strTemp+"\\",strDesDir+strTemp+"\\");
			}
		}
		else
		{
			AfxMessageBox("不可能!");
		}
		
		bRet = FindNextFile(hFile, &fd);
	}
}

DWORD WINAPI CopyThread(LPARAM lParam)
{
	CString strTemp;
	CTimingCopyDlg *pMainDlg= ((CTimingCopyDlg *)(AfxGetApp()->m_pMainWnd));
	CWnd *pWnd=NULL;
	pWnd=pMainDlg->GetDlgItem(IDC_BUTTON_SET);
	pWnd->EnableWindow(FALSE);

	pWnd=pMainDlg->GetDlgItem(IDC_BUTTON_RESET);
	pWnd->EnableWindow(FALSE);

	TwoString *pTwoString=(TwoString*)(lParam);
	CopyDir(pTwoString->str1,pTwoString->str2);
	strTemp.Format("成功复制了%d个文件,创建了%d个目录",pMainDlg->m_iCount,pMainDlg->m_iCountDir);
	pMainDlg->m_list.InsertItem(0,strTemp);
	AfxMessageBox(strTemp);

	pWnd=pMainDlg->GetDlgItem(IDC_BUTTON_SET);
	pWnd->EnableWindow(TRUE);

	pWnd=pMainDlg->GetDlgItem(IDC_BUTTON_RESET);
	pWnd->EnableWindow(TRUE);
	return 0;
}

⌨️ 快捷键说明

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