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

📄 数据压缩dlg.cpp

📁 自己做的作业(自适应霍夫曼解压缩) 完成功能解压缩文件!方法:自适应霍夫曼编码(8bit编码)(有GUI界面)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 数据压缩Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "数据压缩.h"
#include "数据压缩Dlg.h"
#include "Help.h"

#include"direct.h"
#include"time.h"
#include"math.h"
#include"Winbase.h"
#include"io.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int flag;//工作模式-》压缩、解压
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_FilePath = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	workflag = false;//设置初始状态
	jc = 0;
	iFile = 0;
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_T1, m_T1);
	DDX_Control(pDX, IDC_R1, m_R1);
	DDX_Control(pDX, IDC_L2, m_L2);
	DDX_Control(pDX, IDC_L1, m_L1);
	DDX_Control(pDX, IDC_FILELENGTH2, m_FileLength2);
	DDX_Control(pDX, IDC_RATIO, m_Ratio);
	DDX_Control(pDX, IDC_TIME, m_Time);
	DDX_Control(pDX, IDC_MSG, m_Msg);
	DDX_Control(pDX, IDC_PROGRESS1, m_ProgBar);
	DDX_Text(pDX, IDC_FILEPATH, m_FilePath);
	DDX_Control(pDX, IDC_FILELENGTH, m_FileLength);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNDIR, OnBtndir)
	ON_BN_CLICKED(IDC_BTNCOMPRESS, OnBtncompress)
	ON_BN_CLICKED(IDC_BTNUNCOMPRESS, OnBtnuncompress)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTNHELP, OnBtnhelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

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


void CMyDlg::OnBtndir() 
{//选择多份文件
	POSITION  pos;
	iFileNum = 0;
	TCHAR* m_pszFile = new TCHAR[4096000]; //set a 2K buffer to hold selected files
	m_pszFile[0] = '\0'; //initialize pointer;
	char filters[] = "All Files (*.*)|*.*|";
	CFileDialog m_filedlg(TRUE, NULL, "*.*",NULL, filters, NULL);
	m_filedlg.m_ofn.Flags = m_filedlg.m_ofn.Flags|OFN_ALLOWMULTISELECT;
	m_filedlg.m_ofn.lpstrFile = m_pszFile; //initialize the OPENFILENAME structure
	m_filedlg.m_ofn.nMaxFile = 4096000;
	if(m_filedlg.DoModal() == IDOK)
	{
		pos=m_filedlg.GetStartPosition();
        while(pos!=NULL)
		{
			FileList[iFileNum] = m_filedlg.GetNextPathName(pos);
			    FileTitle = m_filedlg.GetFileTitle();
				FileExt = m_filedlg.GetFileExt();
				FileName = m_filedlg.GetFileName(); 
			iFileNum++;
		}
		if(iFileNum == 1)
		{
			m_FilePath = FileList[0];
		}
		else{
            m_FilePath.Format("处理%2d份文件",iFileNum);
		}
		UpdateData(false);
	}
	delete m_pszFile;
	
}
long WINAPI PROCESS(CMyDlg *frame)
{//多线程
	if(flag)//压缩
		frame->Encode();
	else//解压
		frame->Decode();
	return 0;
}
void CMyDlg::OnTimer(UINT nIDEvent) 
{//计时器
	char gmsg[300];
	if(!workflag )
	{
		if(flag)
	    	sprintf(gmsg,"压缩完毕!生成文件:%s",CurrentFileName);
		else
			sprintf(gmsg,"解压完毕!生成文件:%s",CurrentFileName);
		m_Msg.SetWindowText(gmsg);
		KillTimer(1);//结束计时器
	}
	else
	if(workflag)
	{
		m_ProgBar.SetPos(int(jc * 100 / length));//设置进度条状态
		timenum++;//计时,每次100毫秒
		if(flag)
		    sprintf(gmsg,"正在压缩文件......%d%%",jc * 100 / length);
		else
			sprintf(gmsg,"正在解压文件......%d%%",jc * 100 / length);
		m_Msg.SetWindowText(gmsg);//处理信息
        sprintf(gmsg,"%02d时%02d分%02d秒",timenum/36000,timenum/600,timenum/10);
		m_Time.SetWindowText(gmsg);//显示处理时间
	}
	CDialog::OnTimer(nIDEvent);
}
void CMyDlg::OnBtncompress() 
{//压缩按钮
	flag = 1;//设置工作状态,1为压缩
	unsigned long idThread;	//多线程
	m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PROCESS,(void*)this,0,&idThread);

}
void CMyDlg::OnBtnuncompress() 
{//解压按钮
	flag = 0;//设置工作状态,0为解压
	unsigned long idThread;//多线程
	m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PROCESS,(void*)this,0,&idThread);
}
void CMyDlg::Encode()
{
	workflag = true;//设置工作状态
	timenum = 0;//计时
	SetTimer(1,100,NULL);//设置计时器
	CurrentFileName = FileList[0];//获得压缩文件名
	
	//读文件,开辟内存
	CFile openfile;
	openfile.Open(CurrentFileName,CFile::modeRead | CFile::shareDenyNone);
	length = openfile.GetLength();
	BYTE *filebuf,*filememp;
	filememp = (BYTE *)GlobalAlloc(GMEM_MOVEABLE,length);
    filebuf = (BYTE *)GlobalLock(filememp);
	BYTE *buf = filebuf;
	length = openfile.ReadHuge(buf,length);
	openfile.Close();

    //对话框面板显示
	char gmsg[300];
	sprintf(gmsg,"压缩前长度:");
	m_L1.SetWindowText(gmsg);
	sprintf(gmsg,"压缩后长度:");
	m_L2.SetWindowText(gmsg);
	sprintf(gmsg,"压缩比:");
	m_R1.SetWindowText(gmsg);
	sprintf(gmsg,"压缩时间:");
	m_T1.SetWindowText(gmsg);
	sprintf(gmsg,"%d",length/1024);
	m_FileLength.SetWindowText(gmsg);
	sprintf(gmsg,"");
	m_FileLength2.SetWindowText(gmsg);
	sprintf(gmsg,"%02d时%02d分%02d秒",timenum/36000,timenum/600,timenum/10);

⌨️ 快捷键说明

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