拷贝指定文件到新的目录
// copyFilePathDlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "copyFilePath.h"
#include "copyFilePathDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CcopyFilePathDlg 对话框
CcopyFilePathDlg::CcopyFilePathDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_COPYFILEPATH_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CcopyFilePathDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CcopyFilePathDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONFIRM, &CcopyFilePathDlg::OnBnClickedButtonConfirm)
ON_BN_CLICKED(IDC_BUTTON_CONFIRM2, &CcopyFilePathDlg::OnBnClickedButtonConfirm2)
END_MESSAGE_MAP()
// CcopyFilePathDlg 消息处理程序
BOOL CcopyFilePathDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
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: 在此添加额外的初始化代码
Init();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CcopyFilePathDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CcopyFilePathDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(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
{
CDialogEx::OnPaint();
}
CPoint p1(300, 50), p2(300, 95);
vDrawArrow(p1, p2, 30, 12);
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CcopyFilePathDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//递归创建目录
void RecursiveDirectory(CString cstrDir) // 递归创建目录
{
if (cstrDir.GetLength() <= 3)//是根目录,无需创建目录
{
return;
}
if (cstrDir[cstrDir.GetLength() - 1] == '\\') // 将路径改为目录
{
cstrDir.Delete(cstrDir.GetLength() - 1, 1);
}
// 修改文件属性
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(cstrDir, &wfd); // 查找
if (hFind != INVALID_HANDLE_VALUE)
{
FindClose(hFind);
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return;
}
// 创建当前目录的地目录失败
if (CreateDirectory(cstrDir, NULL) == false)
{// 退到上一级目录
CString wstrNewDir = cstrDir;
int n = wstrNewDir.ReverseFind('\\');
wstrNewDir = cstrDir.Left(n);
// 递归进入
RecursiveDirectory(wstrNewDir); // 递归本函数,再创建目录
// 递归退出后创建之前失败的目录
CreateDirectory(cstrDir, NULL); // 递归返回,在存在的目录上再建目录
}// 多级目录创建成功
}
void CcopyFilePathDlg::Init()
{
wchar_t pFileName[MAX_PATH];
GetModuleFileName(NULL, pFileName, MAX_PATH);
wchar_t pFileName2[MAX_PATH];
int nPos2 = GetCurrentDirectory(MAX_PATH, pFileName2);
CString csFullPath(pFileName);
CString csFullPath2;
int nPos = csFullPath.ReverseFind(_T('\\') );
if (nPos < 0)
csFullPath2= CString("");
else
csFullPath2 = csFullPath.Left(nPos);
GetDlgItem(IDC_EDIT_CURRENTDIR)->SetWindowTextW(csFullPath2);
}
void CcopyFilePathDlg::vDrawArrow(CPoint p1, CPoint p2, double theta, int lenght)
{
theta = 3.1415925 * theta / 180;
double Px, Py, p1x, p1y, p2x, p2y;
Px = p1.x - p2.x;
Py = p1.y - p2.y;
p1x = Px * cos(theta) - Py * sin(theta);
p1y = Px * sin(theta) + Py * cos(theta);
p2x = Px * cos(-theta) - Py * sin(-theta);
p2y = Px * sin(-theta) + Py * cos(-theta);
double x1, x2;
x1 = sqrt(p1x * p1x + p1y * p1y);
p1x = p1x * lenght / x1;
p1y = p1y * lenght / x1;
x2 = sqrt(p2x*p2x + p2y * p2y);
p2x = p2x * lenght / x2;
p2y = p2y * lenght / x2;
p1x = p1x + p2.x;
p1y = p1y + p2.y;
p2x = p2x + p2.x;
p2y = p2y + p2.y;
CClientDC dc(this);
CPen pen, pen1, *pOldpen;
pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
pen1.CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
pOldpen = dc.SelectObject(&pen);
dc.MoveTo(p1.x, p1.y);
dc.LineTo(p2.x, p2.y);
dc.SelectObject(&pen1);
dc.MoveTo(p2.x, p2.y);
dc.LineTo(p1x, p1y);
dc.MoveTo(p2.x, p2.y);
dc.LineTo(p2x, p2y);
dc.SelectObject(pOldpen);
}
void CcopyFilePathDlg::OnBnClickedButtonConfirm()
{
// TODO: 在此添加控件通知处理程序代码
CString targetDir, targetFile,desDir,currentDir;
GetDlgItem(IDC_EDIT_CURRENTDIR)->GetWindowTextW(currentDir);
GetDlgItem(IDC_EDIT_TARGETDIR)->GetWindowTextW(targetDir);//\Source\View\kxGIS\dllmain.cpp
GetDlgItem(IDC_EDIT_TARGETFILE)->GetWindowTextW(targetFile);//G:\KS_TASK\JHC00231205_KSP增加打开文件夹函数需求\KS37.00.00000.10000\Source\View\kxGIS\dllmain.cpp
desDir.Format(_T("%s%s"), currentDir, targetDir);
if (targetFile.Compare(_T(""))==0) {
MessageBox(_T("源文件不能为空"));
return;
}
CString m_strSrcPath = targetFile;//要拷贝的源文件 _T("G:\\TestMyself\\FileOperation\\Debug\\hello.txt");
CString m_strDestPath = desDir;//目标目录 _T("G:\\TestMyself\\FileOperation2\\VISTA\\FileOperation\\Debug");
CopyDirectory(m_strSrcPath, m_strDestPath);
MessageBox(_T("over"));
}
BOOL CcopyFilePathDlg::CopyDirectory(CString strSrcPath, CString strDestPath)
{
CFileFind FileFind;
if (strSrcPath.IsEmpty())
{
MessageBox(_T("源文件名为空,无法进行拷贝!"));
return FALSE;
}
if (!FileFind.FindFile(strDestPath))
{
RecursiveDirectory(strDestPath);
//CreateDirectory(strDestPath, NULL);//创建目标文件夹
}
CFileFind finder;
CString path;
path = strSrcPath;
//path.Format(_T("%s/*.*"), strSrcPath);//拷贝目录下的所有文件
BOOL bWorking = finder.FindFile(path);
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDirectory() && !finder.IsDots())//是文件夹 而且 名称不含 . 或 ..
{
CopyDirectory(finder.GetFilePath(), strDestPath + "/" + finder.GetFileName());//递归创建文件夹+"/"+finder.GetFileName()
}
else
{//是文件,则直接复制
//AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()
CopyFile(finder.GetFilePath(), strDestPath + "/" + finder.GetFileName(), FALSE);
}
}
return TRUE;
}
CString CcopyFilePathDlg::GetFilePath()
{
CString FilePath;
GetModuleFileName(NULL, FilePath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
FilePath.ReleaseBuffer();
int iPosIndex;
iPosIndex = FilePath.ReverseFind('\\');
FilePath = FilePath.Left(iPosIndex + 1);
return FilePath;
}
CString CcopyFilePathDlg::GetFileName()
{
CString sFileName;
sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log");
return sFileName;
}
void CcopyFilePathDlg::OnBnClickedButtonConfirm2()
{
// TODO: 在此添加控件通知处理程序代码
//选择文件
static TCHAR BASED_CODE szFilter[] = _T("");
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
CString fileName;
if (dlg.DoModal() == IDOK)
{
UpdateData(FALSE);
}
GetDlgItem(IDC_EDIT_TARGETFILE)->SetWindowText(dlg.GetPathName());
//excel_path = dlg.GetPathName();
}