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

📄 ftpexdlg.cpp

📁 网络编程相关代码 FTP的程序
💻 CPP
字号:
// FtpExDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FtpEx.h"
#include "FtpExDlg.h"

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


#include "FtpGet.h"
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CFtpExDlg dialog

CFtpExDlg::CFtpExDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFtpExDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFtpExDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFtpExDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFtpExDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
   DDX_Control(pDX, IDC_ANONYMOUS, m_btnAnonymous);
	DDX_Control(pDX, IDC_EDIT_NAME, m_editUser);
	DDX_Control(pDX, IDC_EDIT_SERVER, m_editServer);
	DDX_Control(pDX, IDC_EDIT_PASSWORD, m_editPass);
	DDX_Control(pDX, IDC_EDIT_FOLDER, m_editFolder);
	DDX_Control(pDX, IDC_FIL_LIST, m_listFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFtpExDlg, CDialog)
	//{{AFX_MSG_MAP(CFtpExDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GETFILES, OnGetFiles)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnDel)
	ON_BN_CLICKED(IDC_ANONYMOUS, OnAnonymous)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFtpExDlg message handlers

BOOL CFtpExDlg::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
	m_editFolder.SetWindowText("c:\\download\\");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CFtpExDlg::OnGetFiles()      //Get These files button
{

	/*int iRadio=GetCheckedRadioButton(IDC_RADIO_BIN,IDC_RADIO_ASC);
	if(iRadio==IDC_RADIO_BIN)
		radioFlag=FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
	else
		radioFlag=FTP_TRANSFER_TYPE_ASCII | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
	*/
	CString tempRemote;
	CString tempLocal;

  // get every file name in the box
	// make 2 CStringArray, one for remote, the other for local

	CStringArray remote;
	CStringArray local;

	for(int x=0;x<m_listFile.GetCount();x++)    //m_file ------the list box
	{
		m_listFile.GetText(x,tempRemote);
		remote.Add(tempRemote);
	
		tempLocal = m_strFolder + tempRemote;
		local.Add(tempLocal);
	}	

	// open CFtpGet class
	CFtpGet ftpget;
	// SetRight
	ftpget.SetAccessRight(m_strUser,m_strPass);
	// open server
	bool conectOK;
	
	conectOK = ftpget.OpenConnection(m_strServer);
	// transfer multiple file
	if(conectOK)
	{
		int succ;
		succ=ftpget.GetMultipleFile(&remote,&local,m_listFile.GetCount());
		if(succ)
		    MessageBox("下载完毕!");
		else
		{
			MessageBox("下载未完成!");
			return;
		}
	}
	else
	{
		MessageBox("不能连接服务器!");
		return;
	}
	// verify which file, if any, have not been transfered
	
	// close connection
	ftpget.CloseConnection();
	// quit this function
}


void CFtpExDlg::OnDel() // delete button
{
	// TODO: Add your control notification handler code here
	int selection=0;

	selection = m_listFile.GetCurSel();
	// now i get the good one
	if(selection <0)
	{
		AfxMessageBox("请在列表中选择文件!");
		return;
	}

	m_listFile.DeleteString(selection);
	// no more in the dialog
	//m_listFile.SetFocus();
	m_listFile.SetCurSel(0);
}

void CFtpExDlg::OnAnonymous() // anonymous check button
{
	// TODO: Add your control notification handler code here
	int check = m_btnAnonymous.GetCheck();

	if(check == 1)
	{
		
		// set new value
		m_editUser.SetWindowText("anonymous");
		m_editPass.SetWindowText("your@email.com");
		
		// you can't edit anonymous
		m_editUser.SetReadOnly(TRUE);
	}
}

void CFtpExDlg::OnOK() 
{
	// TODO: Add extra validation here
   m_editFolder.GetWindowText(m_strFolder);
	// Get username, password, server
	m_editUser.GetWindowText(m_strUser);
	m_editPass.GetWindowText(m_strPass);
	m_editServer.GetWindowText(m_strServer);

	// get every file name in the box
	// make 2 CStringArray, one for remote, the other for local
	//CStringArray remote;
	CStringArray localName;	

   
    int nFileNumber=0;

    CFtpGet ftpget;
	// SetRight
	ftpget.SetAccessRight(m_strUser,m_strPass);
	// open server
	bool conectOK;
	conectOK = ftpget.OpenConnection(m_strServer);
	// transfer multiple file
	if(conectOK)
	{
		
		nFileNumber=ftpget.GetMultipleFileName(&localName);
	}
	else
	{
		MessageBox("不能连接服务器!");
		return;
	}
	// verify which file, if any, have not been transfered
	
	
	for(int test=0;test<nFileNumber;test++)
	{
		{
			m_listFile.AddString((LPCTSTR)(localName.GetAt(test)));
		}
	}
	// close connection
	ftpget.CloseConnection();
}

⌨️ 快捷键说明

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