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

📄 haffumandlg.cpp

📁 哈夫曼编码在文件压缩中有其独特一点
💻 CPP
字号:
// haffumanDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CHaffumanDlg dialog

CHaffumanDlg::CHaffumanDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHaffumanDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHaffumanDlg)
	m_sCodeAdd = _T("");
	m_sDecodeAdd = _T("");
	m_sMessage = _T("准备就绪");
	m_iFileFlag = 0;
	m_sCompress = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);



}

void CHaffumanDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHaffumanDlg)
	DDX_Text(pDX, IDC_EDIT_CODEADD, m_sCodeAdd);
	DDX_Text(pDX, IDC_EDIT_DECODEADD, m_sDecodeAdd);
	DDX_Text(pDX, IDC_MESSAGE, m_sMessage);
	DDX_Text(pDX, IDC_STATIC_COMPRSS, m_sCompress);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHaffumanDlg, CDialog)
	//{{AFX_MSG_MAP(CHaffumanDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INFO, OnInfo)
	ON_BN_CLICKED(IDC_BUT_CODEOPEN, OnButCodeopen)
	ON_BN_CLICKED(IDC_BUT_CODE, OnButCode)
	ON_BN_CLICKED(IDC_BUT_DECODEOPEN, OnButDecodeopen)
	ON_BN_CLICKED(IDC_BUT_DECODE, OnButDecode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHaffumanDlg message handlers

BOOL CHaffumanDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CHaffumanDlg::OnInfo() 
{
	// TODO: Add your control notification handler code here
	MessageBox("数据结构设计性实验案例:哈夫曼编译码器");
	
}

void CHaffumanDlg::OnButCodeopen() 
{
	// TODO: Add your control notification handler code here
	CFileDialog CodeOpen(TRUE);
	CodeOpen.m_ofn.lpstrTitle = "待压缩文件地址";
    CodeOpen.m_ofn.lpstrFilter = "All Files(*.*)\0*.*\0\0";
	CFileDialog CodeSave(FALSE);
	CodeSave.m_ofn.lpstrTitle = "压缩文件另存为";
	CodeSave.m_ofn.lpstrDefExt = "haf";
	
	if(IDOK == CodeOpen.DoModal())
	{
		int i = 0;
		int j = 0;
		int isize;
        m_sCodeAdd = CodeOpen.GetPathName();
        isize=m_sCodeAdd.GetLength();
		UpdateData(FALSE);
		for (; i < 150 && j < isize; ++i,++j)
		{
			m_cCodeOpenAdd[i] = m_sCodeAdd[j];
			if ( '\\' == m_cCodeOpenAdd[i] )
			{
				i++;
				m_cCodeOpenAdd[i] = '\\';
			}
			
		}
         m_cCodeOpenAdd[i] = '\0';

		m_iFileFlag = 0;
		if(IDOK == CodeSave.DoModal())
		{	
			m_sCodeSave = CodeSave.GetPathName();
			 isize = m_sCodeSave.GetLength();
			 i = 0;
			 j = 0;
			for (; i < 150 && j < isize; ++i,++j)
			{
				m_cCodeSaveAdd[i] = m_sCodeSave[j];
				if ( '\\' == m_cCodeSaveAdd[i] )
				{
				   i++;
				   m_cCodeSaveAdd[i] = '\\';
				}
				
			}
			m_cCodeSaveAdd[i] = '\0';
			m_iFileFlag = 1;			
		}
		
	}
}



void CHaffumanDlg::OnButCode() 
{
	// TODO: Add your control notification handler code here
	CHaffmanCode m_hCode;
	float Compre;
	if (0 == m_iFileFlag)
	{
		MessageBox("文件压缩时地址操作错误!","错误",MB_OK | MB_ICONERROR);
	
	}
	else if(2 == m_iFileFlag)
	{
	   MessageBox("文件已压缩!","错误",MB_OK | MB_ICONERROR);
	}
	else if(1 == m_iFileFlag)
	{
	   m_sMessage = "正在压缩文件,请稍候…";
	   UpdateData(FALSE);
       Compre = m_hCode.OpenandCloseFile(m_cCodeOpenAdd,m_cCodeSaveAdd);
       m_sCompress.Format("%f",Compre);
	   m_sCompress += "%";
	   if ("-1.#INF00%" == m_sCompress)
	   {
		   m_sCompress = "原文件为空,无法计算!";
	   }
	   m_sMessage = "文件压缩完毕!";
	   UpdateData(FALSE);
	   m_sCompress = "";
	   m_iFileFlag = 2;
	     
	}
}

void CHaffumanDlg::OnButDecodeopen() 
{
	// TODO: Add your control notification handler code here
	int isize;
	int i;
	int j;
	CFileDialog DecodeOpen(TRUE);
	DecodeOpen.m_ofn.lpstrTitle = "待解压文件";
	DecodeOpen.m_ofn.lpstrFilter = "haf Files(*.haf)\0*.haf\0\0";
	CFileDialog DecodeSave(FALSE);
	DecodeSave.m_ofn.lpstrTitle = "解压文件另存为";
	DecodeSave.m_ofn.lpstrFilter = "All Files(*.*)\0*.*\0\0";
	if (IDOK == DecodeOpen.DoModal())
	{
        m_sDecodeAdd = DecodeOpen.GetPathName();
		UpdateData(FALSE);
		isize = m_sDecodeAdd.GetLength();
		i = 0;
		j = 0;
		for (; i < 150 && j < isize; ++i,++j)
		{
			m_cDecodeOpenAdd[i] = m_sDecodeAdd[j];
			if ( '\\' == m_cDecodeOpenAdd[i] )
			{
				i++;
				m_cDecodeOpenAdd[i] = '\\';
			}
			
		}
		m_cDecodeOpenAdd[i] = '\0';
		m_iFileFlag = 10;
		if (IDOK == DecodeSave.DoModal())
		{	
			m_sDecodeSave = DecodeSave.GetPathName();
			isize = m_sDecodeSave.GetLength();
			i = 0;
			j = 0;
		
			for (; i < 150 && j < isize; ++i,++j)
			{
				m_cDecodeSaveAdd[i] = m_sDecodeSave[j];
				if ( '\\' == m_cDecodeSaveAdd[i] )
				{
					i++;
					m_cDecodeSaveAdd[i] = '\\';
				}
				
			}
	    m_cDecodeSaveAdd[i] = '\0';
		m_iFileFlag = 11;
		}
	}
}

void CHaffumanDlg::OnButDecode() 
{
	// TODO: Add your control notification handler code here
	if (11 == m_iFileFlag)
	{
	   m_sMessage = "正在解压文件,请稍候…";
	   UpdateData(FALSE);
       CHaffmanCode m_hDecode;
	   m_hDecode.TranslateCodes(m_cDecodeOpenAdd,m_cDecodeSaveAdd);
	   m_sMessage = "文件解压完毕!";
	   UpdateData(FALSE);
	   m_iFileFlag = 12;
	}
	else if (12 == m_iFileFlag)
	{
		MessageBox("文件已解压!","错误",MB_OK | MB_ICONERROR);
	}
	else
	{
    	MessageBox("文件解压时地址操作错误!","错误",MB_OK | MB_ICONERROR);
	}
	
}

⌨️ 快捷键说明

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