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

📄 mysplitterdlg.cpp

📁 《Windows应用程序捆绑核心编程》配套源码
💻 CPP
字号:
// MySplitterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MySplitter.h"
#include "MySplitterDlg.h"

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

CMySplitterDlg::CMySplitterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMySplitterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMySplitterDlg)
	m_strCutNum = _T("");
	m_strCutUnit = _T("");
	m_strFileName = _T("");
	m_strSavePath = _T("");
	m_strFileSize = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bEqVolume=TRUE;
}

void CMySplitterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMySplitterDlg)
	DDX_Control(pDX, IDC_EDCUTNUM, m_edCutNum);
	DDX_Control(pDX, IDC_EDCUTUNIT, m_edCutUnit);
	DDX_Text(pDX, IDC_EDCUTNUM, m_strCutNum);
	DDX_Text(pDX, IDC_EDCUTUNIT, m_strCutUnit);
	DDX_Text(pDX, IDC_EDFILENAME, m_strFileName);
	DDX_Text(pDX, IDC_EDFILESAVE, m_strSavePath);
	DDX_Text(pDX, IDC_FILESIZE, m_strFileSize);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMySplitterDlg, CDialog)
	//{{AFX_MSG_MAP(CMySplitterDlg)
	ON_BN_CLICKED(IDC_RAD_EQVOL, OnRadEqvol)
	ON_BN_CLICKED(IDC_RAD_UNIT, OnRadUnit)
	ON_BN_CLICKED(IDC_BROWSE_FILE, OnBrowseFile)
	ON_BN_CLICKED(IDC_BROWSE_SAVE, OnBrowseSave)
	ON_EN_CHANGE(IDC_EDCUTUNIT, OnChangeEdcutunit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMySplitterDlg message handlers

BOOL CMySplitterDlg::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
	
    m_edCutNum.EnableWindow(m_bEqVolume);
	m_edCutUnit.EnableWindow(!m_bEqVolume);

	CButton *RadEqVol=(CButton *)GetDlgItem(IDC_RAD_EQVOL);
	CButton *RadEqUnit=(CButton *)GetDlgItem(IDC_RAD_UNIT);
	RadEqVol->SetCheck(m_bEqVolume);
    RadEqUnit->SetCheck(!m_bEqVolume);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMySplitterDlg::OnRadEqvol() 
{
	m_bEqVolume=TRUE;
    m_edCutNum.EnableWindow(m_bEqVolume);
	m_edCutUnit.EnableWindow(!m_bEqVolume);
}

void CMySplitterDlg::OnRadUnit() 
{
	m_bEqVolume=FALSE;
    m_edCutNum.EnableWindow(m_bEqVolume);
	m_edCutUnit.EnableWindow(!m_bEqVolume);
}

void CMySplitterDlg::OnBrowseFile() 
{
	struct _stat ST; 

    CFileDialog fileDialog(TRUE,NULL,NULL,NULL,
		        "所有文件(*.*)|*.*||");
	
	if (fileDialog.DoModal() == IDOK) 
	{
		m_strFileName = fileDialog.GetPathName(); 
		
 	    // 如果输入的文件名为空,返回.
	    if(m_strFileName.IsEmpty()) return;

        // 获取文件的长度.
	    _stat(m_strFileName, &ST);

		m_strFileSize.Format("%d",ST.st_size);
		UpdateData(FALSE);
	}			
}

void CMySplitterDlg::OnBrowseSave() 
{
	CString strResult =""; 
    LPMALLOC lpMalloc;  // pointer to IMalloc 
    if (::SHGetMalloc(&lpMalloc) != NOERROR) 
	{ 
		AfxMessageBox("Path operation error!"); 
		return ; 
	} 
    char szDisplayName[_MAX_PATH]; 
    char szBuffer[_MAX_PATH]; 
    BROWSEINFO browseInfo; 
    browseInfo.hwndOwner = this->m_hWnd; 
    // set root at Desktop 
    browseInfo.pidlRoot = NULL; 
    browseInfo.pszDisplayName = szDisplayName; 
    browseInfo.lpszTitle = "请选择分类数据路径";  // Dialog title 
    browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS; 
    browseInfo.lpfn = NULL;     // not used 
    browseInfo.lParam = 0;      // not used 
    LPITEMIDLIST lpItemIDList; 
    if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) 
        != NULL) 
    { 
        // Get the path of the selected folder from the    item ID list. 
        if (::SHGetPathFromIDList(lpItemIDList, szBuffer)) 
        { 
            // At this point, szBuffer contains the path the user chose. 
            if (szBuffer[0] == '\0') 
            { 
                // SHGetPathFromIDList failed, or SHBrowseForFolder failed. 
                AfxMessageBox("Fail to get directory!", 
                    MB_ICONSTOP|MB_OK); 
                return ; 
            } 
            // We have a path in szBuffer! Return it. 
            strResult = szBuffer; 
        } 
        else 
        { 
            // The thing referred to by lpItemIDList 
            // might not have been a file system object. 
            // For whatever reason, SHGetPathFromIDList didn't work! 
            AfxMessageBox("Fail to get directory!", 
                MB_ICONSTOP|MB_OK); 
            return ; 
        } 
        lpMalloc->Free(lpItemIDList); 
        lpMalloc->Release(); 
    } 

    m_strSavePath=strResult;
	UpdateData(FALSE); 	
}

//---------------------------------------------------------------------------
void CMySplitterDlg::AddDirSplash(CString &strDir) 
{
    CString strTmp=strDir;
	strTmp.TrimRight();
    if((!strTmp.IsEmpty())&&(strTmp.Right(1)!='\\'))
		strTmp +="\\";
	strDir=strTmp;
}

//---------------------------------------------------------------------------
BOOL CMySplitterDlg::DoSplit() 
{
    // 检查被分割的文件名.
    if(m_strFileName.IsEmpty()) return FALSE;

	CString strFilePath,strShortName;
	
	// 控制程序的系统文件名.
    CString strFileDest;

	CThreadFCut sect;

	// 给文件目录路径增加分隔线.
	AddDirSplash(m_strSavePath);

  	// 分离文件路径和短文件名.
	sect.BreakFileName(m_strFileName,strFilePath,strShortName);

	// 如果保存分割后的文件的目录为空,取当前目录保存分割后的文件.
	if(m_strSavePath.IsEmpty())
         m_strSavePath = strFilePath;

	if(m_bEqVolume){
		// 等体积分割.
		if(m_strCutNum.IsEmpty()) return FALSE;
		sect.DoVolumeCut(strFilePath,m_strSavePath,
	    		strShortName,atoi(m_strCutNum));

        // 产生控制程序的目标文件名.
		strFileDest.Format("%s_%sv.exe",m_strSavePath+strShortName,m_strCutNum);
       CopyObjectToDest(strFileDest);
	}
	else{
		// 固定长度单位分割.
		if(m_strCutUnit.IsEmpty()) return FALSE;
		sect.DoUnitCut(strFilePath,m_strSavePath,
      			m_strFileName,atoi(m_strCutUnit));
        
		// 获得控制程序的目标文件名.
		strFileDest.Format("%s_%ss.exe",m_strSavePath+strShortName,m_strCutUnit);
        CopyObjectToDest(strFileDest);
 	}

	return TRUE;
}

//---------------------------------------------------------------------------
void CMySplitterDlg::CopyObjectToDest(CString strFileDest)
{
	struct _stat ST; 
	FILE *fpread,*fpwrite;

	CString strFilePath,strShortName;
   	TCHAR szModule[MAX_PATH];

	// 获得应用程序名.
    GetModuleFileName(0, szModule, sizeof(szModule)); 
	
	// 获取文件长度.
	_stat(szModule, &ST);
	UINT nFilesize=ST.st_size;

	// 打开文件.
	if((fpread=fopen(szModule,"rb"))==NULL)
		return;

	// 打开文件.
	if((fpwrite=fopen(strFileDest,"wb"))==NULL)
		return;
    
	// MySplitter.exe裸体文件长度.
	UINT nPos =126976;
	
	// 设置文件读指针起始位置.
	fseek(fpread,nPos,SEEK_SET);

	int c;
	// 把控制程序数据写入到strFileDest.
	while((c=fgetc(fpread))!=EOF)
	{
		fputc(c,fpwrite);
    }
    
	fclose(fpread);
	fclose(fpwrite);
}

//---------------------------------------------------------------------------
void CMySplitterDlg::OnChangeEdcutunit() 
{
	// 计算分割段的段数.
	UpdateData();
	long nFilesize=atoi(m_strFileSize);
	long nSize=atoi(m_strCutUnit);
    int nSectNum= nFilesize/nSize;

	// 有多余的长度.
    if(nFilesize%nSize!=0) nSectNum ++;

    m_strCutNum.Format("%d",nSectNum);
	UpdateData(FALSE);
}

//---------------------------------------------------------------------------
void CMySplitterDlg::OnOK() 
{
	UpdateData();

	if(DoSplit()){
		AfxMessageBox("文件分割成功!:)");
	}
	else{
		AfxMessageBox("文件分割不成功:(((");
	}

	CDialog::OnOK();
}

⌨️ 快捷键说明

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