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

📄 ftp5view.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
// ftp5View.cpp : implementation of the CFtp5View class
//

#include "stdafx.h"
#include "ftp5.h"

#include "ftp5Doc.h"
#include "ftp5View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFtp5View

IMPLEMENT_DYNCREATE(CFtp5View, CView)

BEGIN_MESSAGE_MAP(CFtp5View, CView)
	//{{AFX_MSG_MAP(CFtp5View)
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFtp5View construction/destruction

CFtp5View::CFtp5View()
{

	m_nFileStatus = -3;
	m_bConnectAttempted = FALSE;

}

CFtp5View::~CFtp5View()
{
}

BOOL CFtp5View::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CFtp5View drawing

void CFtp5View::OnDraw(CDC* pDC)
{
	CFtp5Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

if( !m_bConnectAttempted ){
  m_bConnectAttempted = TRUE;
  FTP_DIRECTORY *pFtpDir = new FTP_DIRECTORY;
  pFtpDir->pnFileStatus = &m_nFileStatus;
  pFtpDir->pStringArray = &m_StringArray;
  AfxBeginThread( (AFX_THREADPROC) CFtpThread::GetDirectory,
   pFtpDir );
  }
for( int i=0; i<m_StringArray.GetSize(); i++ ){
 CString strDirEntry;
 strDirEntry = m_StringArray.GetAt( i );
 pDC->TextOut( 0, i * 20, strDirEntry,
  strDirEntry.GetLength() );
 }

}

/////////////////////////////////////////////////////////////////////////////
// CFtp5View diagnostics

#ifdef _DEBUG
void CFtp5View::AssertValid() const
{
	CView::AssertValid();
}

void CFtp5View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CFtp5Doc* CFtp5View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFtp5Doc)));
	return (CFtp5Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFtp5View message handlers
/////////////////////////////////////////////////////////////////////////////
// CFtpThread

IMPLEMENT_DYNCREATE(CFtpThread, CWinThread)

CFtpThread::CFtpThread()
{
}

CFtpThread::~CFtpThread()
{
}

BOOL CFtpThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

UINT CFtpThread::GetDirectory( LPVOID Info )
{
 FTP_DIRECTORY *pFtpDir;
 CInternetSession *pInetSession;
 CFtpConnection *pFtpConnection = NULL;
 CString strFileName;
 BOOL bContinue;
 CFtpFileFind *pFtpFind = NULL;

 pFtpDir = (FTP_DIRECTORY *) Info;
 *pFtpDir->pnFileStatus = 0;

 pInetSession =
  new CInternetSession( AfxGetAppName(),
  1, PRE_CONFIG_INTERNET_ACCESS );

  try{
   pFtpConnection =
    pInetSession->GetFtpConnection( "ftp.microsoft.com");
   }
  catch( CInternetException *pEx ){
   pEx->Delete();
   pFtpConnection = NULL;
   *pFtpDir->pnFileStatus = -1;
   goto BailOut;
   }

  *pFtpDir->pnFileStatus = 1;

  pFtpFind = new CFtpFileFind( pFtpConnection );

  bContinue = pFtpFind->FindFile( "*" );
  if( !bContinue ){
   pFtpFind->Close();
   goto BailOut;
   }

  while( bContinue ){
   bContinue = pFtpFind->FindNextFile();
   strFileName = pFtpFind->GetFileName();
   if( pFtpFind->IsDirectory() ){
    strFileName = " [" + strFileName;
    strFileName += "]";
    }
   pFtpDir->pStringArray->Add( strFileName );

   } 

  pFtpFind->Close();
  *pFtpDir->pnFileStatus = 2;

BailOut:
 if( pFtpFind != NULL )
  delete pFtpFind;

 if( pFtpConnection != NULL ){
  pFtpConnection->Close();
  delete pFtpConnection;
  }

 delete pInetSession;

 AfxEndThread( 0 );
 return( FALSE );

}

int CFtpThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CFtpThread, CWinThread)
	//{{AFX_MSG_MAP(CFtpThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFtpThread message handlers

int CFtp5View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	SetTimer( 1, 1000, NULL );
	
	return 0;
}

void CFtp5View::OnTimer(UINT nIDEvent) 
{

 if( m_nFileStatus == 2 ){
  SortDirectory();
  m_nFileStatus = 3;
  InvalidateRect( NULL, FALSE );
  UpdateWindow();
  }
	
	CView::OnTimer(nIDEvent);
}

void CFtp5View::SortDirectory( void )
{

 int nCount = m_StringArray.GetSize();
 for( int i=1; i<nCount; i++ ){
  CString String1 = m_StringArray.GetAt( i );
  for( int j=0; j<i; j++ ){
   CString String2 = m_StringArray.GetAt( j );
   if( String1.CompareNoCase( String2 ) < 0 ){
    for( int k=i; k>j; k-- )
     m_StringArray.SetAt( k,
      m_StringArray.GetAt( k - 1 ) );
    m_StringArray.SetAt( j, String1 );
    j = i;
    }
   }
 }

}

⌨️ 快捷键说明

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