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

📄 filedlg.cpp

📁 选择两个文件夹
💻 CPP
字号:
// FileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "File.h"
#include "FileDlg.h"
#include <vector>
using namespace std;
#include "ThreadMD5.h"
CThreadMD5 ThMD5;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



typedef struct  
{
	char szFileName[200];
	BOOL bName;
	BOOL bContent;
}FILECOMP,*PFILECOMP;

std::vector<FILECOMP> vFC;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CFileDlg dialog

CFileDlg::CFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileDlg)
	DDX_Control(pDX, IDC_LIST2, m_list2);
	DDX_Control(pDX, IDC_LIST1, m_list1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileDlg, CDialog)
	//{{AFX_MSG_MAP(CFileDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNDIR1, OnBtndir1)
	ON_BN_CLICKED(IDC_BTNDIR2, OnBtndir2)
	ON_BN_CLICKED(IDC_BTNCMP, OnBtncmp)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileDlg message handlers

BOOL CFileDlg::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
	GetDlgItem(IDC_EDITDIR1)->SetWindowText(_T("d:\\Backup\\桌面\\新建文件夹"));
	GetDlgItem(IDC_EDITDIR2)->SetWindowText(_T("d:\\Backup\\桌面\\一级"));
	
	// TODO: Add extra initialization here
	InitListCtrl();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CFileDlg::OnBtndir1() 
{
	// TODO: Add your control notification handler code here
	BROWSEINFO bsi={0};
	LPITEMIDLIST lpList=NULL;
	if ((lpList=SHBrowseForFolder(&bsi))!=NULL)
	{
		char szPath[300]={0};
		SHGetPathFromIDList(lpList,szPath);
		GetDlgItem(IDC_EDITDIR1)->SetWindowText(szPath);
		GetDlgItem(IDC_EDITDIR1)->GetWindowText(strPath1);
		Show1();
	}
}

void CFileDlg::OnBtndir2() 
{
	// TODO: Add your control notification handler code here
	BROWSEINFO bsi={0};
	//	bsi.lpszTitle="choose a file directory";
	LPITEMIDLIST lpList=NULL;
	if ((lpList=SHBrowseForFolder(&bsi))!=NULL)
	{
		char szPath[300]={0};
		SHGetPathFromIDList(lpList,szPath);
		GetDlgItem(IDC_EDITDIR2)->SetWindowText(szPath);
		GetDlgItem(IDC_EDITDIR2)->GetWindowText(strPath2);
		Show2();
	}	
}

void CFileDlg::OnBtncmp() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_EDITDIR1)->GetWindowText(strPath1);
	GetDlgItem(IDC_EDITDIR2)->GetWindowText(strPath2);
	if (strPath1.Right(1)=="\\")
	{
		strPath1.Delete(strPath1.GetLength()-1);
	}
	if (strPath2.Right(1)=="\\")
	{
		strPath2.Delete(strPath2.GetLength()-1);
	}
	Show1();
	Show2();
	if (strPath1==""||strPath2=="")
	{
		MessageBox("the path is illegal");
		return;
	}
	ThMD5.m_strPath1=strPath1;
	ThMD5.m_strPath2=strPath2;
	SetTimer(1,1000,0);
	if (ThMD5.ThreadBegin()!=TRUE)
	{
		MessageBox("Can not Begin thread");
	}	
}

void CFileDlg::Show1()
{
	
	UpdateData(TRUE);
	GetDlgItem(IDC_EDITDIR1)->GetWindowText(strPath1);
	m_list1.DeleteAllItems();
	CString szPath1=strPath1;
	if (szPath1.GetLength()!=0)
	{
		if (szPath1.Right(1)=="\\")
		{
			szPath1.Delete(szPath1.GetLength()-1);
		}
		szPath1+="\\*";
		CFileFind finder;
		BOOL bFinding=finder.FindFile(szPath1);
		if (!bFinding)
		{
			if (szPath1.GetLength()==3)
			{
				MessageBox("请在盘符后面加“:”");
			}
			else
			{
				MessageBox("the directory is illegal");
			}
			
		}
		LPITEMIDLIST pIdl=NULL;
		int iRet=SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&pIdl);
		SHFILEINFO sfi={0};
		HIMAGELIST hImageList=(HIMAGELIST)SHGetFileInfo((LPCTSTR)pIdl,0,&sfi,
			sizeof(SHFILEINFO),SHGFI_PIDL|SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
		ListView_SetImageList(m_list1.GetSafeHwnd(),hImageList,LVSIL_SMALL);
		while (bFinding)
		{
			CString strPathAndName;
			CString strFileName;
			CString strFileLength;
			DWORD dwFileLength=0;
			CTime ctCreate;
			CString strCreatetime;
			bFinding=finder.FindNextFile();
			strFileName=finder.GetFileName();
			dwFileLength=finder.GetLength();
			strFileLength.Format("%ld",dwFileLength);
			finder.GetCreationTime(ctCreate);
			strPathAndName=strPath1;
			if (strPathAndName.Right(1)=='\\')
			{
				strPathAndName+=strFileName;
			}
			else
			{
				strPathAndName=strPathAndName+'\\'+strFileName;
			}
			//获取该文件的图标在系统图标索引中的索引号,保存在sfi.iIcon
			SHGetFileInfo(strPathAndName,FILE_ATTRIBUTE_NORMAL,&sfi,sizeof(SHFILEINFO),
				SHGFI_ICON | SHGFI_OPENICON | SHGFI_DISPLAYNAME | SHGFI_EXETYPE | 
				SHGFI_TYPENAME | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
			strCreatetime.Format("%04d.%02d.%02d. %02d:%02d:%02d",ctCreate.GetYear(),
				ctCreate.GetMonth(),ctCreate.GetDay(),ctCreate.GetHour(),ctCreate.GetMinute(),
				ctCreate.GetSecond());
			//判断是不是“.”和“..”
			if (!finder.IsDots())
			{
				int nIndex=m_list1.InsertItem(m_list1.GetItemCount(),strFileName,sfi.iIcon);
				if (!finder.IsDirectory())
				{
					m_list1.SetItemText(nIndex,1,strFileLength);
				}
				m_list1.SetItemText(nIndex,2,strCreatetime);
			}
		}
	}
}

void CFileDlg::Show2()
{
	UpdateData(TRUE);
	GetDlgItem(IDC_EDITDIR2)->GetWindowText(strPath2);
	m_list2.DeleteAllItems();
	CString szPath2=strPath2;
	if (szPath2.GetLength()!=0)
	{
		if (szPath2.Right(1)=="\\")
		{
			szPath2.Delete(szPath2.GetLength()-1);
		}
		szPath2+="\\*";
		CFileFind finder;
		BOOL bFinding=finder.FindFile(szPath2);
		if (!bFinding)
		{
			if (szPath2.GetLength()==3)
			{
				MessageBox("请在盘符后面加“:”");
			}
			else
			{
				MessageBox("the directory is illegal");
			}
			
		}
		LPITEMIDLIST pIdl=NULL;
		int iRet=SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&pIdl);
		SHFILEINFO sfi={0};
		HIMAGELIST hImageList=(HIMAGELIST)SHGetFileInfo((LPCTSTR)pIdl,0,&sfi,
			sizeof(SHFILEINFO),SHGFI_PIDL|SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
		ListView_SetImageList(m_list2.GetSafeHwnd(),hImageList,LVSIL_SMALL);
		while (bFinding)
		{
			CString strPathAndName;
			CString strFileName;
			CString strFileLength;
			DWORD dwFileLength=0;
			CTime ctCreate;
			CString strCreatetime;
			bFinding=finder.FindNextFile();
			strFileName=finder.GetFileName();
			dwFileLength=finder.GetLength();
			strFileLength.Format("%ld",dwFileLength);
			finder.GetCreationTime(ctCreate);
			strPathAndName=strPath2;
			if (strPathAndName.Right(1)=='\\')
			{
				strPathAndName+=strFileName;
			}
			else
			{
				strPathAndName=strPathAndName+'\\'+strFileName;
			}
			//获取该文件的图标在系统图标索引中的索引号,保存在sfi.iIcon
			SHGetFileInfo(strPathAndName,FILE_ATTRIBUTE_NORMAL,&sfi,sizeof(SHFILEINFO),
				SHGFI_ICON | SHGFI_OPENICON | SHGFI_DISPLAYNAME | SHGFI_EXETYPE | 
				SHGFI_TYPENAME | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
			strCreatetime.Format("%04d.%02d.%02d. %02d:%02d:%02d",ctCreate.GetYear(),
				ctCreate.GetMonth(),ctCreate.GetDay(),ctCreate.GetHour(),ctCreate.GetMinute(),
				ctCreate.GetSecond());
			//判断是不是“.”和“..”
			if (!finder.IsDots())
			{
				int nIndex=m_list2.InsertItem(m_list2.GetItemCount(),strFileName,sfi.iIcon);
				if (!finder.IsDirectory())
				{
					m_list2.SetItemText(nIndex,1,strFileLength);
				}
				m_list2.SetItemText(nIndex,2,strCreatetime);
			}
			
		}
	}
}

void CFileDlg::InitListCtrl()
{
	m_list1.InsertColumn(0,"file1",LVCFMT_LEFT,100);
	m_list1.InsertColumn(1,"size1(byte)",LVCFMT_LEFT,80);
	m_list1.InsertColumn(2,"time1",LVCFMT_LEFT,100);
	m_list1.InsertColumn(3,"same",LVCFMT_LEFT,60);
	m_list2.InsertColumn(0,"file2",LVCFMT_LEFT,100);
	m_list2.InsertColumn(1,"size2(byte)",LVCFMT_LEFT,80);
	m_list2.InsertColumn(2,"time2",LVCFMT_LEFT,100);
	m_list2.InsertColumn(3,"same",LVCFMT_LEFT,60);
}

void CFileDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (ThMD5.bSuccess==TRUE)
	{
		KillTimer(1);
		std::vector<FILECOMP>::iterator p=vFC.begin();
		int i=0;
		CString strName;
		while (p!=vFC.end())
		{
			for (i=0;i<m_list1.GetItemCount();i++)
			{
				strName=m_list1.GetItemText(i,0);
				if (strcmp(strName.GetBuffer(strName.GetLength()),(*p).szFileName)==0)
				{
					if (((*p).bName==1) && ((*p).bContent==1))
					{
						m_list1.SetItemText(i,3,"N&Cont");
					}
					else
					{
						m_list1.SetItemText(i,3,"Name");
					}
				}
			}
			for (i=0;i<m_list2.GetItemCount();i++)
			{
				strName=m_list2.GetItemText(i,0);
				if (strcmp(strName.GetBuffer(strName.GetLength()),(*p).szFileName)==0)
				{
					if (((*p).bName==1) && ((*p).bContent==1))
					{
						m_list2.SetItemText(i,3,"N&Cont");
					}
					else
					{
						m_list2.SetItemText(i,3,"Name");
					}
				}
			}
			p=vFC.erase(p);
		}
		MessageBox("Compare over");
	}
	CDialog::OnTimer(nIDEvent);
}

void CFileDlg::SetDisplay(LPCSTR FileName, BOOL bName, BOOL bCont)
{
	FILECOMP FC={0};
	strncpy(FC.szFileName,FileName,sizeof(FC.szFileName)-1);
	FC.bName=bName;
	FC.bContent=bCont;
	vFC.push_back(FC);
}

⌨️ 快捷键说明

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