📄 filedemoview.cpp
字号:
// FileDemoView.cpp : implementation of
// the CFileDemoView class
//
#include "stdafx.h"
#include "FileDemo.h"
#include "FileDemoDoc.h"
#include "FileDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFileDemoView
IMPLEMENT_DYNCREATE(CFileDemoView, CView)
BEGIN_MESSAGE_MAP(CFileDemoView, CView)
//{{AFX_MSG_MAP(CFileDemoView)
ON_COMMAND(ID_FILE_ADDTOLIST, OnFileAddtolist)
ON_COMMAND(ID_FILE_SAVEFILESINLIST, OnFileSavefilesinlist)
ON_COMMAND(ID_FILE_EMPTYLIST, OnFileEmptylist)
ON_COMMAND(ID_FILE_COPYFIRSTFILE, OnFileCopyfirstfile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileDemoView construction/destruction
CFileDemoView::CFileDemoView()
{
}
CFileDemoView::~CFileDemoView()
{
}
BOOL CFileDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CFileDemoView drawing
void CFileDemoView::OnDraw(CDC* pDC)
{
CFileDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Draw the list of files in the list.
int y = 0;
for( int i=0; i<m_Filename.GetSize(); i++ ){
// Get the string at this index.
CString strText = m_Filename.GetAt( i );
// Draw it to the DC.
pDC->TextOut( 0, y, strText );
// Get the size so we can increment
// the y coordinate.
CSize size =
pDC->GetTextExtent( strText, 1 );
// Increment the y coordinate by
// the text height.
y += size.cy;
}
}
/////////////////////////////////////////////////////////////////////////////
// CFileDemoView diagnostics
#ifdef _DEBUG
void CFileDemoView::AssertValid() const
{
CView::AssertValid();
}
void CFileDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CFileDemoDoc* CFileDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFileDemoDoc)));
return (CFileDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFileDemoView message handlers
char szFilter[] = "All Files(*.*)|*.*||";
void CFileDemoView::OnFileAddtolist()
{
// Common dialog box.
CFileDialog FileDlg( TRUE, NULL, NULL,
OFN_HIDEREADONLY, szFilter );
if( FileDlg.DoModal() == IDOK ){
// Add the full path and file name
// to the m_Pathname list.
m_Pathname.Add( FileDlg.GetPathName() );
// Add the file name to the m_Filename
// list.
m_Filename.Add( FileDlg.GetFileName() );
// Cause a window redraw.
InvalidateRect( NULL, TRUE );
UpdateWindow();
}
}
void CFileDemoView::OnFileSavefilesinlist()
{
// Check to make sure we have some
// files in the list.
if( m_Pathname.GetSize() == 0 ){
AfxMessageBox( "There are no files in the list." );
return;
}
// Common dialog box.
CFileDialog FileDlg( FALSE, NULL, NULL,
OFN_HIDEREADONLY, szFilter );
if( FileDlg.DoModal() == IDOK ){
// Attempt to open the output file.
CFile Out;
if( Out.Open( FileDlg.GetPathName(),
CFile::modeWrite | CFile::modeCreate ) ){
// Loop through each file.
for( int i=0; i<m_Pathname.GetSize(); i++ ){
// Attempt to create the input
// file.
CFile In;
if( In.Open( m_Pathname.GetAt( i ),
CFile::modeRead ) ){
// Temporary buffer.
char cbBuffer[4000];
// Get the input file size so we
// know when we're done writing.
int nFilesize = In.GetLength();
while( nFilesize > 0 ){
// Start off by assuming
// we'll read in enough bytes
// to fill the buffer.
int nSize = sizeof( cbBuffer );
// If the number of bytes remaining
// isn't enough to fill the
// buffer, adjust the size.
if( nSize > nFilesize )
nSize = nFilesize;
// Read in the bytes and make
// sure we catch any exceptions
// that are thrown.
try{
In.Read( cbBuffer, nSize );
}
catch( CFileException *e ){
// Format a message from
// the system.
char *lpMsgBuf;
if( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, e->m_lOsError,
MAKELANGID( LANG_NEUTRAL,
SUBLANG_DEFAULT ),
(LPSTR) &lpMsgBuf, 0, NULL ) > 0 ){
AfxMessageBox( lpMsgBuf );
LocalFree( lpMsgBuf );
}
// Free the exception and
// return.
e->Delete();
return;
}
// Write out the bytes and make
// sure we catch any exceptions
// that are thrown.
try{
Out.Write( cbBuffer, nSize );
}
catch( CFileException *e ){
// Format a message from
// the system.
char *lpMsgBuf;
if( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, e->m_lOsError,
MAKELANGID( LANG_NEUTRAL,
SUBLANG_DEFAULT ),
(LPSTR) &lpMsgBuf, 0, NULL ) > 0 ){
AfxMessageBox( lpMsgBuf );
LocalFree( lpMsgBuf );
}
// Free the exception and
// return.
e->Delete();
return;
}
nFilesize -= nSize;
}
}
// Alert user to problem.
else
AfxMessageBox( "Could not open "
+ m_Pathname.GetAt( i ) );
}
}
// Alert user to problem.
else
AfxMessageBox( "Could not create the output file." );
}
}
void CFileDemoView::EmptyStringArrays( void )
{
// Empty the arrays which contain
// the full path and file name,
// and just the file name.
m_Pathname.RemoveAll();
m_Filename.RemoveAll();
}
void CFileDemoView::OnFileEmptylist()
{
// Empty everything and then
// cause a screen redraw.
EmptyStringArrays();
InvalidateRect( NULL, TRUE );
UpdateWindow();
}
BOOL CFileDemoView::CopyFile( const char *lpSrcFilename,
const char *lpDestFilename, int nBuffersize )
{
CFile Out, In;
int nFilesize;
char *lpBuffer;
// Attempt to open the input file.
if( !In.Open( lpSrcFilename, CFile::modeRead ) ){
AfxMessageBox( "Could not open the input file." );
return( FALSE );
}
// Attempt to create the output file.
if( !Out.Open( lpDestFilename,
CFile::modeWrite | CFile::modeCreate ) ){
AfxMessageBox( "Could not open the output file." );
return( FALSE );
}
// Create the copy buffer.
lpBuffer = new char [nBuffersize];
if( lpBuffer == NULL ){
AfxMessageBox( "Could not allocate the copy buffer." );
return( FALSE );
}
// Get the input file status so that
// we can set the output file's status
// to the same. This will preserve
// things such as time and date.
CFileStatus rStatus;
In.GetStatus( lpSrcFilename, rStatus );
// Get file file size so that we know
// when we're done copying.
nFilesize = In.GetLength();
while( nFilesize > 0 ){
// Start off by assuming
// we'll read in enough bytes
// to fill the buffer.
int nSize = nBuffersize;
// If the number of bytes remaining
// isn't enough to fill the
// buffer, adjust the size.
if( nSize > nFilesize )
nSize = nFilesize;
// Read in the bytes and make
// sure we catch any exceptions
// that are thrown.
try{
In.Read( lpBuffer, nSize );
}
catch( CFileException *e ){
// Format a message from
// the system.
char *lpMsgBuf;
if( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, e->m_lOsError,
MAKELANGID( LANG_NEUTRAL,
SUBLANG_DEFAULT ),
(LPSTR) &lpMsgBuf, 0, NULL ) > 0 ){
AfxMessageBox( lpMsgBuf );
LocalFree( lpMsgBuf );
}
// Free the exception and
// return.
e->Delete();
return( FALSE );
}
// Write out the bytes and make
// sure we catch any exceptions
// that are thrown.
try{
Out.Write( lpBuffer, nSize );
}
catch( CFileException *e ){
// Format a message from
// the system.
char *lpMsgBuf;
if( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, e->m_lOsError,
MAKELANGID( LANG_NEUTRAL,
SUBLANG_DEFAULT ),
(LPSTR) &lpMsgBuf, 0, NULL ) > 0 ){
AfxMessageBox( lpMsgBuf );
LocalFree( lpMsgBuf );
}
// Free the exception and
// return.
e->Delete();
return( FALSE );
}
nFilesize -= nSize;
}
// Close the output file so that the
// SetStatus() function won't fail.
Out.Close();
CFile::SetStatus( lpDestFilename, rStatus );
// Delete the buffer.
delete [] lpBuffer;
return( TRUE );
}
void CFileDemoView::OnFileCopyfirstfile()
{
// Check to make sure we have some
// files in the list.
if( m_Pathname.GetSize() == 0 ){
AfxMessageBox( "There are no files in the list." );
return;
}
// Common dialog box.
CFileDialog FileDlg( FALSE, NULL, NULL,
OFN_HIDEREADONLY, szFilter );
// If the selected OK, do the copy.
if( FileDlg.DoModal() == IDOK )
CopyFile( m_Pathname.GetAt( 0 ),
FileDlg.GetPathName() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -