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

📄 copydlg.cpp

📁 一款商业软件的配置文件备份/恢复工具,可以将备份自动恢复到软件中
💻 CPP
字号:
// copyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "copy.h"
#include "copyDlg.h"
#include "MyFileDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCopyDlg dialog

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

void CCopyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCopyDlg)
	DDX_Text(pDX, IDC_EDIT1, m_file);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCopyDlg, CDialog)
	//{{AFX_MSG_MAP(CCopyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BT_W, OnBtW)
	ON_BN_CLICKED(IDC_BT_R, OnBtR)
	ON_MESSAGE(MYWM_VALBUE, DeleteDirectory)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCopyDlg message handlers

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

void CCopyDlg::OnOK() 
{
	// TODO: Add extra validation here

	CDialog::OnOK();
}

void CCopyDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CMyFileDialog dlg(true, "tiger.ini", NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST, 
     "配置文件 *.ini|tiger.ini||"); 
	dlg.DoModal();
	CString fileName;//定义一个Cstring类型的字符串来存储文件的路径及文件名
    fileName=dlg.GetPathName();
	m_file=fileName;
	UpdateData(false);
	
}

void CCopyDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	DeleteDirectory("d:\\tiger");
	MessageBox("备份文件删除成功!!!");
}

void CCopyDlg::OnBtW() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_file.TrimLeft();
	m_file.TrimRight();

	if (m_file.IsEmpty()==1) 
	{
		MessageBox("请选择程序路径");
		return;
	}
	int x=CopyFile("d:\\tiger\\tiger.dat",m_file,false);
	if (x=1)
	{
	MessageBox("配置文件恢复成功!!\n请重启软件试一下!!");
	}
	else
	{
		MessageBox("文件恢复失败!!\n请检查路径!!");
	}
}

void CCopyDlg::OnBtR() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_file.TrimLeft();
	m_file.TrimRight();

	if (m_file.IsEmpty()==1) 
	{
		MessageBox("请选择程序路径");
		return;
	}
	CreateDirectory("d:\\tiger",NULL);
	int x=CopyFile(m_file,"d:\\tiger\\tiger.dat",false);
	if (x=1)
	{
	MessageBox("配置文件备份成功!!\n请勿格式化或删除D盘tiger文件");
	}
	else
	{
       MessageBox("配置文件备份失败!!");
	}
}

void  CCopyDlg::DeleteDirectory(CString strDir)   
{   
	if(strDir.IsEmpty())   
		return;   
    
	//   首先删除文件及子文件夹   
	CFileFind   ff;   
	BOOL   bFound   =   ff.FindFile(strDir+"\\*",   0);   
	while(bFound)   
	{   
		bFound   =   ff.FindNextFile();   
		if(ff.GetFileName()=="."||ff.GetFileName()=="..")   
		continue;   
		//   去掉文件(夹)只读等属性   
		SetFileAttributes(ff.GetFilePath(),   FILE_ATTRIBUTE_NORMAL);   
		if(ff.IsDirectory())   
		{   
			//   递归删除子文件夹   
			DeleteDirectory(ff.GetFilePath());   
			RemoveDirectory(ff.GetFilePath());   
		}   
		else   
		{   
			//   删除文件   
			DeleteFile(ff.GetFilePath());   
		}   
	}   
	ff.Close();     
	//   然后删除该文件夹   
	RemoveDirectory(strDir);   
}

⌨️ 快捷键说明

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