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

📄 packdlg.cpp

📁 VC++例子:文件打包
💻 CPP
字号:
// PackDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Pack.h"
#include "PackDlg.h"
#include "Kyio.h"
#include "direct.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CString explain(CString cmd0,CString file) 
{
	CString Cmd=cmd0;
	if(Cmd.Find("%FILE")>=0)
	{
		Cmd.Replace("%FILE","%s");
		const char *s=Cmd.GetBuffer(Cmd.GetLength());
		char ss[512];
		sprintf(ss,s,file);
		Cmd=ss;
	}
	return Cmd;
}
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()

/////////////////////////////////////////////////////////////////////////////
// CPackDlg dialog

CPackDlg::CPackDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPackDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPackDlg)
	m_Path = _T("");
	m_Ext = _T("");
	m_FileName = _T("");
	m_Style = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPackDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPackDlg)
	DDX_Text(pDX, IDC_PATH, m_Path);
	DDX_CBString(pDX, IDC_EXT, m_Ext);
	DDX_CBString(pDX, IDC_FILENAME, m_FileName);
	DDX_CBString(pDX, IDC_STYLE, m_Style);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPackDlg, CDialog)
	//{{AFX_MSG_MAP(CPackDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDPACK, OnPack)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPackDlg message handlers

BOOL CPackDlg::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

	CComboBox *Com=(CComboBox *)GetDlgItem(IDC_EXT);
	Com->AddString("*.*");
	Com->AddString("*.mat");
	SetDlgItemText(IDC_EXT,"*.mat");
	
	Com=(CComboBox *)GetDlgItem(IDC_FILENAME);
	Com->AddString("%FILE");
	Com->AddString("%FILE+");
	Com->AddString("%FILE%EXT+");
	SetDlgItemText(IDC_FILENAME,"%FILE");

	Com=(CComboBox *)GetDlgItem(IDC_STYLE);
	Com->AddString("%FILE*.*");
	Com->AddString("%FILE%EXT*.*");
	Com->AddString("%PATH%FILE%EXT*.*");
	SetDlgItemText(IDC_STYLE,"%FILE*.*");
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CPackDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	KyPFE pfe;
	CFileDialog *fd;
	CString fname,path;
	static char BASED_CODE szFilter[] = "Mat Files (*.mat)|*.mat|All Files (*.*)|*.*||";
	fd=new CFileDialog(TRUE, "*", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT ,szFilter, NULL);
	fd->DoModal();
	fname=fd->GetPathName();
	getFileName(pfe,fname.GetBuffer(fname.GetLength()));
	SetDlgItemText(IDC_PATH,pfe.mpath);	
	//SetDlgItemText(IDC_FILENAME,pfe.mname);	
	char ext[64],ext1[64];
	sprintf(ext,"*.%s",pfe.mext);
	SetDlgItemText(IDC_EXT,ext);	
////////////
	strupr(ext);
	CComboBox *Com=(CComboBox *)GetDlgItem(IDC_EXT);
	if(Com->FindString(0,ext)<0)
		Com->InsertString(1,ext);
	
}

void CPackDlg::OnPack() 
{
	// TODO: Add your control notification handler code here
	CString WJ,WJJ,File;
	//UpdateData(FALSE);
	UpdateData();

	CString p;
	bool fd=true;
	WIN32_FIND_DATA find_data;
	HANDLE          find_handle;
	char *pname=new char[256];
	sprintf(pname,"%s%s",m_Path,m_Ext);
	find_handle = FindFirstFile(pname,&find_data);
	//find_handle = FindFirstFile(p,&find_data);

	while( find_handle !=  INVALID_HANDLE_VALUE && fd) 
	{
		DWORD a=find_data.dwFileAttributes;
		if(!strcmp(find_data.cFileName,".") || !strcmp(find_data.cFileName,".."))
			a=0;
		if(a)
		{
			//sprintf(pname,"%s%s",path,find_data.cFileName);
			CString Dir,Npath;
			Dir.Format("%s%s",m_Path,find_data.cFileName);
			KyPFE pfe;
			getFileName(pfe,Dir.GetBuffer(MAX_PATH));
			WJJ=explain(m_FileName,pfe.mname);
			mkdir(WJJ);
			Npath.Format("%s%s\\",m_Path,WJJ);
			WJJ=explain(m_Style,pfe.mname);
			if(WJJ.GetLength()>1)
			{
				WIN32_FIND_DATA find_data;
				HANDLE          find_handle;
				char *pname=new char[256];
				sprintf(pname,"%s%s",m_Path,WJJ);
				find_handle = FindFirstFile(pname,&find_data);
				//find_handle = FindFirstFile(p,&find_data);
				while( find_handle !=  INVALID_HANDLE_VALUE && fd) 
				{
					
					fd=FindNextFile(find_handle,&find_data);
					CString Efile,Nfile;
					Efile.Format("%s%s",m_Path,find_data.cFileName);
					Nfile.Format("%s%s",Npath,find_data.cFileName);
					MoveFile(Efile,Nfile);
				}
				delete pname;
			}
		}
		fd=FindNextFile(find_handle,&find_data );
	}
	delete pname;

	

}

⌨️ 快捷键说明

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