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

📄 filesplitterdlg.cpp

📁 vc++高级编程源代码
💻 CPP
字号:
// FileSplitterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileSplitter.h"
#include "FileSplitterDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CFileSplitterDlg dialog

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

void CFileSplitterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileSplitterDlg)
	DDX_Check(pDX, IDC_CHECK_ADD, m_bCom);
	DDX_Check(pDX, IDC_CHECK_REPORT, m_bReport);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileSplitterDlg, CDialog)
	//{{AFX_MSG_MAP(CFileSplitterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELECT, OnSelect)
	ON_BN_CLICKED(IDC_SPLITTER, OnSplitter)
	ON_BN_CLICKED(IDC_CLOSE, OnCloseFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileSplitterDlg message handlers

BOOL CFileSplitterDlg::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
	SetDlgItemInt(IDC_EDIT_LENGTH,0);
	SetDlgItemInt(IDC_EDIT_SIZE,0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CFileSplitterDlg::OnSelect() 
{
	// TODO: Add your control notification handler code here
	// 选择要打开的文件
	CFileDialog dlg(TRUE);
	if (dlg.DoModal() == IDCANCEL)
		return;

	// 若已经打开了一个文件,则关闭之
	if (m_file.m_hFile != CFile::hFileNull)
		OnCloseFile();

	// 获得文件名并打开之
	m_strName = dlg.GetFileName();
	if (!m_file.Open(m_strName,CFile::modeRead | CFile::shareDenyWrite))
	{
		AfxMessageBox(_T("无法打开文件!"));
		m_strName.Empty();
		return;
	}
	// 获得文件标题、路径并更新用户界面
	m_strPath = dlg.GetPathName();
	m_strTitle = dlg.GetFileTitle();
	SetDlgItemText(IDC_EDIT_PATH,m_strPath);
	GetDlgItem(IDC_SPLITTER)->EnableWindow(TRUE);
	GetDlgItem(IDC_CLOSE)->EnableWindow(TRUE);
	SetDlgItemInt(IDC_EDIT_LENGTH,m_file.GetLength() / 1024);
}

void CFileSplitterDlg::OnSplitter() 
{
	// TODO: Add your control notification handler code here
	ASSERT(m_file.m_hFile != CFile::hFileNull);

	// 获得文件长度以及用户指定的子文件大小
	DWORD dwLength = m_file.GetLength();
	DWORD dwPart = (DWORD)1024 * GetDlgItemInt(IDC_EDIT_SIZE);
	if (dwPart == 0)
	{
		AfxMessageBox(_T("切割文件大小不能为0!"));
		return;
	}
	else if (dwPart > dwLength)
	{
		AfxMessageBox(_T("切割文件比原文件大,将不作切割!"));
		return;
	}
	else if (dwPart < dwLength / 1000)
	{
		// 子文件数不应大于1000个,否则也太多了
		AfxMessageBox(_T("切割文件太小了,总数应该少于1000!"));
		return;
	}

	// 获得必要的内存空间
	char *pBuf = new char[dwPart];
	if (pBuf == NULL)
	{
		AfxMessageBox(_T("没有足够的内存来进行分割操作!"));
		return;
	}

	// 开始分割
	// 获得用户的输入值
	UpdateData();
	int nCnt = 1;
	CString strName;
	DWORD dwRead;
	CFile wrFile;
	while (1)
	{
		// 从原文件中读取指定长度的部分
		dwRead = m_file.Read(pBuf,dwPart);
		// 将子文件命名为“文件标题.子文件序号”的方式
		strName.Format("%s.%d",m_strTitle,nCnt);
		wrFile.Open(strName,CFile::modeCreate | CFile::modeWrite);
		// 将读得的部分写入新的文件
		wrFile.Write(pBuf,dwRead);
		wrFile.Close();
		// 若读得的长度比要求的少则跳出
		if (dwRead < dwPart)
			break;
		nCnt++;
	}
	
	// 建立的子文件数是否合乎要求
	BOOL bSuccess = (dwRead + (nCnt - 1) * dwPart == dwLength);

	// 创建自合并文件
	CString strReport = _T("");
	if (bSuccess && m_bCom)
	{
		//按照“copy filename1/b+filename2/b+…+filenameN/b filename”的顺序写入批处理
		strReport += _T("copy ");
		for (int i = 1;i < nCnt;i++)
		{
			strName.Format("%s.%d/b + ",m_strTitle,i);
			strReport += strName;
		}
		strName.Format("%s.%d/b ",m_strTitle,i);
		strReport += strName;
		strReport += m_strName;
		strName = m_strTitle + ".bat";
		wrFile.Open(strName,CFile::modeCreate | CFile::modeWrite);
		wrFile.Write((LPCTSTR)strReport,strReport.GetLength());
	}
	if (m_bReport)
	{
		strReport = m_strPath + "\r\n";
		strReport += _T("分割为下列文件:\r\n");
		for (int i = 1;i <= nCnt;i++)
		{
			strName.Format("%s.%d\r\n",m_strTitle,i);
			strReport += strName;
		}
		if (bSuccess)
		{
			strReport += _T("分割成功!");
			strName.Format(_T("\r\n生成自合并文件\r\n%s.bat"),m_strTitle);
			if (m_bCom)
				strReport += strName;
		}
		else
			strReport += _T("分割未成功!");
	}
	else 
		strReport = bSuccess ? _T("分割成功!") : _T("分割未成功!");
	AfxMessageBox(strReport);
}

void CFileSplitterDlg::OnCloseFile() 
{
	// TODO: Add your control notification handler code here
	ASSERT(m_file.m_hFile != CFile::hFileNull);
	m_file.Close();
	GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_SPLITTER)->EnableWindow(FALSE);
	SetDlgItemText(IDC_EDIT_PATH,_T(""));
	SetDlgItemInt(IDC_EDIT_LENGTH,0);
}

⌨️ 快捷键说明

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