📄 filespltdlg.cpp
字号:
#include "stdafx.h"
#include "FileSplt.h"
#include "FileSpltDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
/////////////////////////////////////////////////////////////////////////////
// CFileSpltDlg dialog
CFileSpltDlg::CFileSpltDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileSpltDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileSpltDlg)
m_parts = _T("");
m_path = _T("");
m_targetpath = _T("");
m_split = TRUE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFileSpltDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileSpltDlg)
DDX_Control(pDX, IDC_WEB, m_web);
DDX_Control(pDX, IDC_COMBOX_FILESIZE, m_FileSize);
DDX_Control(pDX, IDC_PROGRESS, pProgress);
DDX_Control(pDX, IDC_RADIO3, m_RadioSplit);
DDX_Control(pDX, IDC_PATH, m_edit);
DDX_Text(pDX, IDC_PARTS, m_parts);
DDX_Text(pDX, IDC_PATH, m_path);
DDX_Text(pDX, IDC_PATH2, m_targetpath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileSpltDlg, CDialog)
//{{AFX_MSG_MAP(CFileSpltDlg)
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_BN_CLICKED(IDOK, OnOk)
ON_BN_CLICKED(IDC_BUTTON_MERGE, OnButtonMerge)
ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
ON_BN_CLICKED(IDC_BROWSE2, OnBrowse2)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_BN_CLICKED(IDC_WEB, OnWeb)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileSpltDlg message handlers
BOOL CFileSpltDlg::OnInitDialog()
{
CDialog::OnInitDialog();
if (m_split)
m_RadioSplit.SetCheck(1);
CButton *pButtonMerge = (CButton*)(GetDlgItem(IDC_BUTTON_MERGE));
pButtonMerge->EnableWindow(FALSE);
m_path =_T("");
m_filename =_T("");
// 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
//设置位图按钮
//在此初始化渐变色进度条
pProgress.SetRange32(1,500);
pProgress.SetPos(500);
//设置选择文件大小下拉框默认值为第1个
m_FileSize.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
HCURSOR CFileSpltDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
long CFileSpltDlg::GetSplitFileSize() //获取用户选择文件分割后的大小
{
char buf[100];
m_FileSize.GetWindowText(buf,100);
for(int i=0; i<(int)strlen(buf); i++)
{
if(!::isdigit(buf[i]))
{
MessageBox("请输入数字!","文件分割",MB_ICONEXCLAMATION);
return 0;
}
}
return atoi(buf)*1000000;
}
int CFileSpltDlg::MergeMe() // 文件合并涵数
{
CWaitCursor wait;
CFile destFile;
CFileException ex;
BYTE buffer[140000];
DWORD dwRead;
UINT nCount = 140000;
UINT newlen = 1400000;
char buff [20];
long l = 1;
CString name;
CString pref;
CString newpath;
UpdateData(TRUE);
if (!m_path.IsEmpty())
{
if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
m_edit.SetFocus();
m_edit.SetSel(0, -1);
return 1;
}
m_filename = m_path.Right((m_path.GetLength() - m_path.ReverseFind('\\')) - 1);
m_SourceFile.Close();
}
newpath = m_path.Left(m_path.GetLength() - m_filename.GetLength());
if (!m_targetpath.IsEmpty())
{
if (!m_filename.IsEmpty() && m_filename.Left(2) != _T("1_")) {
::AfxMessageBox(_T("待合并的源文件名不对..."), MB_ICONERROR);
return 1;
}
else if(m_filename.IsEmpty()) {
MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
m_filename = m_filename.Right(m_filename.GetLength() - 2);
if(m_targetpath.Right(1)=='\\')
m_path = m_targetpath + m_filename;
else
m_path = m_targetpath + _T("\\") + m_filename;
if (!destFile.Open(m_path,
CFile::modeWrite |
CFile::shareExclusive |
CFile::typeBinary |
CFile::modeCreate, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
return 0;
}
}
else if(m_path.IsEmpty())
{
MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
if (m_targetpath.IsEmpty()) {//target is not there
MessageBox(_T("请选择合并后要保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
do {
pref = _ltoa(l, buff, 10);
pref += _T("_");
if (!m_SourceFile.Open(newpath + pref + m_filename,
CFile::modeRead |
CFile::shareExclusive |
CFile::typeBinary, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
destFile.Close();
m_path = _T("");
m_filename = _T("");
newpath = _T("");
UpdateData(FALSE);
return 0;
}
else
name = _T(newpath + pref + m_filename);
do
{
dwRead = m_SourceFile.Read(buffer, nCount);
destFile.Write(buffer, dwRead);
}while (dwRead > 0);
m_SourceFile.Close();
pProgress.SetRange(0, 500);
for (int i = 0; i < 500; i++)
pProgress.SetPos(i);
m_parts = _ltoa(l, buff, 10);
m_parts += _T("个文件已合并");
UpdateData(FALSE);
l++;
UpdateWindow();
} while (l < 500);
return 0;
}
int CFileSpltDlg::SplitMe() //文件分割涵数
{
CWaitCursor wait;
CFile destFile;
CFileException ex;
DWORD dwRead;
UINT newlen;
char buff [20];
char b [20];
long l = 1;
CString name;
UINT len = 0;
UpdateData(TRUE);
newlen=GetSplitFileSize(); //获取文件分割后的大小,定义相对应变量数值
UINT nCount = newlen/10;
BYTE buffer[140000];
if (!m_path.IsEmpty())
{
if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
m_edit.SetFocus();
m_edit.SetSel(0, -1);
return 1;
}
len = m_SourceFile.GetLength();
}
else
{
MessageBox(_T("请选择待分割的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
if (m_targetpath.IsEmpty())
{
MessageBox(_T("请选择分割后保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION);
return 1;
}
if (len < newlen)
{
CString length = _itoa(len, b, 10);
MessageBox(_T("文件长度为 " + length + " 字节,不够指定的分割大小, 没有必要再进行分割."), _T("文件分割器"), MB_ICONEXCLAMATION);
m_SourceFile.Close();
m_path = _T("");
m_filename = _T("");
UpdateData(FALSE);
return 1;
}
do {
name = _ltoa(l, buff, 10);
name += _T("_");
CString newpath;
//判断选择目录未尾是否已有"\"符
if(m_targetpath.Right(1)=='\\')
newpath = m_targetpath;
else
newpath = m_targetpath + _T("\\");
if (!destFile.Open(newpath + name + m_SourceFile.GetFileName(),
CFile::modeWrite |
CFile::shareExclusive |
CFile::typeBinary |
CFile::modeCreate, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
m_SourceFile.Close();
return 1;
}
do
{
dwRead = m_SourceFile.Read(buffer, nCount);
destFile.Write(buffer, dwRead);
}while (dwRead > 0 && destFile.GetLength() < newlen);
destFile.Close();
pProgress.SetRange(0, len /newlen*10);
pProgress.SetPos(l);
m_parts = _ltoa(l , buff, 10);
m_parts += _T("个文件生成");
UpdateData(FALSE);
l++;
UpdateWindow();
}while (dwRead > 0);
m_SourceFile.Close();
m_path = _T("");
m_filename = _T("");
UpdateData(FALSE);
return 0;
}
void CFileSpltDlg::OnBrowse()
{
static char BASED_CODE szSplitFilter[] = _T("待分割文件(*.*)|*.*||");
static char BASED_CODE szMergeFilter[] = _T("待合并文件(1_*.*)|1_*.*||");
CString filter;
if (!m_split)
filter = szMergeFilter;
else
filter = szSplitFilter;
CFileDialog dlg(TRUE,
NULL,
NULL,
OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT |
OFN_FILEMUSTEXIST,
filter,
0);
if (dlg.DoModal() == IDOK)
{
m_path = dlg.GetPathName();
m_filename = dlg.GetFileName();
UpdateData(FALSE);
}
}
void CFileSpltDlg::OnOk()
{
if (!SplitMe())
MessageBox(_T("文件已被成功分割!"), _T("文件分割"), MB_ICONEXCLAMATION);
}
void CFileSpltDlg::OnButtonMerge()
{
if (! MergeMe())
MessageBox(_T("文件合并成功!"), _T("文件合并"), MB_ICONEXCLAMATION);
}
void CFileSpltDlg::OnRadio4()
{
CButton *pButtonMerge = (CButton*)(GetDlgItem(IDC_BUTTON_MERGE));
CButton *pButtonSplit = (CButton*)(GetDlgItem(IDOK));
m_FileSize.EnableWindow(false);
pButtonSplit->EnableWindow(FALSE);
pButtonMerge->EnableWindow(TRUE);
m_split = FALSE;
m_path = _T("");
UpdateData(FALSE);
}
void CFileSpltDlg::OnRadio3()
{
CButton *pButtonMerge = (CButton*)(GetDlgItem(IDC_BUTTON_MERGE));
CButton *pButtonSplit = (CButton*)(GetDlgItem(IDOK));
m_FileSize.EnableWindow(true);
pButtonSplit->EnableWindow(TRUE);
pButtonMerge->EnableWindow(FALSE);
m_split = TRUE;
m_path = _T("");
UpdateData(FALSE);
}
void CFileSpltDlg::OnBrowse2()
{
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));
bi.hwndOwner = GetSafeHwnd();
bi.lpszTitle = "请选择文件或文件夹";
LPITEMIDLIST pidl= ::SHBrowseForFolder(&bi); //浏览文件夹
if(pidl !=NULL)
{
::SHGetPathFromIDList(pidl,(LPTSTR)(LPCTSTR)this->m_targetpath);// 把PIDL转换为路径名
UpdateData(false);
}
}
void CFileSpltDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
SendMessage(WM_NCLBUTTONDOWN,HTCAPTION,0);
CDialog::OnLButtonDown(nFlags, point);
}
BOOL CFileSpltDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
return true;
//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
void CFileSpltDlg::OnWeb()
{
::ShellExecute(NULL,"open","http://www.59hao.com/chx",NULL,NULL,SW_SHOWNORMAL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -