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

📄 compdlg.cpp

📁 一个小老乡写的压缩、解压软件
💻 CPP
字号:
// CompDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CCompDlg dialog

CCompDlg::CCompDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCompDlg)
	m_comSource = _T("");
	m_comTarget = _T("");
	m_decomSource = _T("");
	m_decomTarget = _T("");
	m_comboPath = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	main = new Cmain();
	//pathDlg = new CPathDialog();
	::GetCurrentDirectory (MAX_PATH, m_comSource.GetBuffer(MAX_PATH));
}

void CCompDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompDlg)
	DDX_Control(pDX, IDC_COM_SOUCRE, m_comSPathName);
	DDX_Text(pDX, IDC_COM_SOUCRE, m_comSource);
	DDX_Text(pDX, IDC_COM_TARGET, m_comTarget);
	DDX_Text(pDX, IDC_DECOM_SOURCE, m_decomSource);
	DDX_Text(pDX, IDC_DECOM_TARGET, m_decomTarget);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCompDlg, CDialog)
	//{{AFX_MSG_MAP(CCompDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_COM_OK, OnComOk)
	ON_BN_CLICKED(IDC_DECOM_OK, OnDecomOk)
	ON_COMMAND(ID_FILE_EXIT, OnFileExit)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_WM_KEYDOWN()
	ON_BN_CLICKED(IDC_COM_SOURCEVIEW, OnComSourceview)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompDlg message handlers

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

//	m_comboPathCon.AddString("fafa");
//	m_comboPathCon.SetCurSel(1);
	//::SendMessage(
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CCompDlg::OnComOk() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if ( m_comSource!= "" && m_comTarget != "")
	{
		int c = 0;
		FILE *fin;//,*fout;
		if (fin = fopen ((char *)(LPCTSTR)(m_comSource), "rb"))
		{
			fseek (fin, 0L, 2);
			main->file_size = (unsigned long) ftell (fin);
			if (main->file_size != 0)
			{

				fseek (fin, 0L, 0);
				c = getc(fin);
				unsigned long  loop;
				for(loop = 0 ; loop < main->file_size; loop++)
				{
					main->countf[c]++;           //统计频率函数
					c = getc(fin);	          
				}
				fclose(fin);
				
				//堆操作
				main->heap_length = 0;

				for(int m0 =  0; m0 < 256; m0++)
				{
					if(main->countf[m0])
						main->heap[++(main->heap_length)] = (unsigned long) m0;
				}

				for(int m1 = main->heap_length; m1 > 0; m1--)
				{
					main->heap_function(m1);          //建立最小堆
				}
				//堆操作结束
				//建哈夫曼树
					main->build_tree();
					
				//建哈夫曼树结束
				//建编码表
					main->build_table();
				//建编码表结束
					main->compress_function((char *)(LPCTSTR)(m_comSource),(char *)(LPCTSTR)(m_comTarget));
					MessageBox(TEXT("压缩成功!"),TEXT("COM_OK"),MB_OK);

			}
			else
			{
				MessageBox(TEXT("文件为空,不需要压缩!"),TEXT("COM_ERROR"),MB_OK);
				fclose(fin);
			}
		}
		else
		{
			MessageBox(TEXT("没找到需要压缩的文件,请检查路径!"),TEXT("COM_ERROR"),MB_OK);
		}
	}
	else
	{
		MessageBox(TEXT("必须完整地填写压缩文件路径和压缩后文件存放路径"),TEXT("COM_ERROR"),MB_OK);
	}
	
}

void CCompDlg::OnDecomOk() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if ( m_decomSource!= "" && m_decomTarget != "")
	{
		main->Decompression_function((char *)(LPCTSTR)(m_decomSource), (char *)(LPCTSTR)(m_decomTarget));
		MessageBox(TEXT("解压缩成功!"),TEXT("DECOM_OK"),MB_OK);
	}
	else
	{
		MessageBox(TEXT("必须完整地填写解压缩文件路径和解压缩后文件存放路径"),TEXT("DECOM_ERROR"),MB_OK);
	}
}

void CCompDlg::OnFileExit() 
{
	// TODO: Add your command handler code here
	PostQuitMessage(0);
	
}

void CCompDlg::OnHelpAbout() 
{
	// TODO: Add your command handler code here
	CAboutDlg about;
	about.DoModal();
	
}

void CCompDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
		if( nChar == VK_ESCAPE)
	{
		MessageBox(TEXT("JK"),TEXT("KFL"), MB_OK);
	}
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
	


}

void CCompDlg::OnOK()
{

}

void CCompDlg::OnComSourceview() 
{
	// TODO: Add your control notification handler code here
	CString strPath;
	CString strYourCaption(_T("选择文件:"));
	CString strYourTitle(_T("请选择:"));
	m_comSPathName.GetWindowText (strPath);

	CPathDialog dlg(strYourCaption, strYourTitle, strPath);

	if (dlg.DoModal() == IDOK)
	{
		m_comSPathName.SetWindowText(dlg.GetPathName());
	}	
}

⌨️ 快捷键说明

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