?? 數(shù)據(jù)壓縮dlg.cpp
字號:
// 數(shù)據(jù)壓縮Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "數(shù)據(jù)壓縮.h"
#include "數(shù)據(jù)壓縮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;//設(shè)置初始狀態(tài)
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);//結(jié)束計時器
}
else
if(workflag)
{
m_ProgBar.SetPos(int(jc * 100 / length));//設(shè)置進度條狀態(tài)
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;//設(shè)置工作狀態(tài),1為壓縮
unsigned long idThread; //多線程
m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PROCESS,(void*)this,0,&idThread);
}
void CMyDlg::OnBtnuncompress()
{//解壓按鈕
flag = 0;//設(shè)置工作狀態(tài),0為解壓
unsigned long idThread;//多線程
m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PROCESS,(void*)this,0,&idThread);
}
void CMyDlg::Encode()
{
workflag = true;//設(shè)置工作狀態(tài)
timenum = 0;//計時
SetTimer(1,100,NULL);//設(shè)置計時器
CurrentFileName = FileList[0];//獲得壓縮文件名
//讀文件,開辟內(nèi)存
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);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -