📄 compressdemoview.cpp
字号:
// CompressDemoView.cpp : implementation of the CCompressDemoView class
//
#include "stdafx.h"
#include "CompressDemo.h"
#include "CompressDemoDoc.h"
#include "CompressDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCompressDemoView
IMPLEMENT_DYNCREATE(CCompressDemoView, CView)
BEGIN_MESSAGE_MAP(CCompressDemoView, CView)
//{{AFX_MSG_MAP(CCompressDemoView)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCompressDemoView construction/destruction
CCompressDemoView::CCompressDemoView()
{
m_bLoaded = FALSE;
}
CCompressDemoView::~CCompressDemoView()
{
}
BOOL CCompressDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCompressDemoView drawing
static char *szComps[] = {
"No Compression ",
"Huffman Compression ",
"LZSS Compression ",
"LZW Compression "
};
void CCompressDemoView::OnDraw(CDC* pDC)
{
CCompressDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if( !m_bLoaded ){
CString PathName = pDoc->GetPathName();
strcpy( m_szFilename, PathName.GetBuffer( 3 ) );
strcpy( m_szSaveLoad, "File loaded unsuccessfully " );
m_pFile = CCompressedFile::MakeNewFile( m_szFilename );
if( !m_pFile ) AfxMessageBox( "Could not create File class!" );
else{
m_dwUncompressedSize = m_dwCompressedSize = 0L;
if( m_pFile != NULL ){
if( m_pFile->Open( m_szFilename, CFile::modeRead ) ){
strcpy( m_szCompression, szComps[m_pFile->m_nCompressionType+1] );
hGlobal = ::GlobalAlloc( GMEM_FIXED, m_pFile->m_dwUncompressedSize + 100L );
if( hGlobal ){
m_cbDataBuffer = (unsigned char *) ::GlobalLock( hGlobal );
BeginWaitCursor();
m_pFile->Read( m_cbDataBuffer, m_pFile->m_dwUncompressedSize );
EndWaitCursor();
m_bSuccess = TRUE;
m_dwUncompressedSize = m_pFile->m_dwUncompressedSize;
m_dwCompressedSize = m_pFile->m_dwCompressedSize;
strcpy( m_szSaveLoad, "File loaded successfully " );
}
else AfxMessageBox( "Couldn't create!" );
m_pFile->Close();
}
else AfxMessageBox( "Couldn't open file!" );
delete m_pFile;
m_pFile = NULL;
}
}
m_bLoaded = TRUE;
}
char junk[200];
wsprintf( junk, "File:%s ", m_szFilename );
pDC->TextOut( 10, 10, junk, strlen( junk ) );
wsprintf( junk, "Uncompressed Size:%ld, Compressed Size:%ld, Ratio:%ld%% ", m_dwUncompressedSize, m_dwCompressedSize, ( m_dwCompressedSize * 100 ) / m_dwUncompressedSize );
pDC->TextOut( 10, 30, junk, strlen( junk ) );
pDC->TextOut( 10, 50, m_szSaveLoad, strlen( m_szSaveLoad ) );
pDC->TextOut( 10, 70, m_szCompression, strlen( m_szCompression ) );
}
/////////////////////////////////////////////////////////////////////////////
// CCompressDemoView diagnostics
#ifdef _DEBUG
void CCompressDemoView::AssertValid() const
{
CView::AssertValid();
}
void CCompressDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCompressDemoDoc* CCompressDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCompressDemoDoc)));
return (CCompressDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCompressDemoView message handlers
void CCompressDemoView::OnFileSaveAs( const char *Filename )
{
CCompressDemoDoc *pDoc = GetDocument();
if( !hGlobal ){
AfxMessageBox( "No buffer to save!" );
return;
}
if( !m_dwUncompressedSize ){
AfxMessageBox( "The size of the data is zero!" );
return;
}
m_pFile = CCompressedFile::MakeNewFile( NULL, pDoc->m_nCompressionSelected );
if( !m_pFile ) AfxMessageBox( "Couldn't create!" );
else{
strcpy( m_szSaveLoad, "File saved unsuccessfully " );
if( !m_pFile->Open( Filename, CFile::modeWrite | CFile::modeCreate ) ) AfxMessageBox( "Couldn't create!" );
else{
BeginWaitCursor();
m_pFile->Write( m_cbDataBuffer, m_dwUncompressedSize );
m_pFile->Close();
EndWaitCursor();
m_dwCompressedSize = m_pFile->m_dwCompressedSize;
strcpy( m_szFilename, Filename );
strcpy( m_szSaveLoad, "File saved successfully " );
strcpy( m_szCompression, szComps[m_pFile->m_nCompressionType+1] );
InvalidateRect( NULL );
UpdateWindow();
}
delete m_pFile;
m_pFile = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -