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

📄 myftpdlg.cpp

📁 本压缩软件为《Visual C++6.0基础教程》(黑魔方系列)一书的源代码
💻 CPP
字号:
// MyFtpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyFtp.h"
#include "MyFtpDlg.h"
#include "Wininet.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMyFtpDlg dialog

CMyFtpDlg::CMyFtpDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyFtpDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyFtpDlg)
	m_addr_down = _T("");
	m_addr_up = _T("");
	m_user_down = _T("");
	m_user_up = _T("");
	m_pass_down = _T("");
	m_pass_up = _T("");
	m_path_down = _T("");
	m_path_up = _T("");
	m_local_name = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyFtpDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyFtpDlg)
	DDX_Text(pDX, IDC_FTP_ADDR_DOWN, m_addr_down);
	DDX_Text(pDX, IDC_FTP_ADDR_UP, m_addr_up);
	DDX_Text(pDX, IDC_USER_DOWN, m_user_down);
	DDX_Text(pDX, IDC_USER_UP, m_user_up);
	DDX_Text(pDX, IDC_PASS_DOWN, m_pass_down);
	DDX_Text(pDX, IDC_PASS_UP, m_pass_up);
	DDX_Text(pDX, IDC_FTP_PATH_DOWN, m_path_down);
	DDX_Text(pDX, IDC_FTP_PATH_UP, m_path_up);
	DDX_Text(pDX, IDC_LOCAL_NAME, m_local_name);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyFtpDlg, CDialog)
	//{{AFX_MSG_MAP(CMyFtpDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE_DOWN, OnBrowseDown)
	ON_BN_CLICKED(IDC_BROWSE_UP, OnBrowseUp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyFtpDlg message handlers

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

void CMyFtpDlg::OnBrowseDown() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	HINTERNET hInetSession,hFtpConn;
	char path[MAX_PATH],dispname[MAX_PATH];
	CString strPath;
	BROWSEINFO bi;      //Get Download To Directory	  
	ITEMIDLIST * pi;
	bi.hwndOwner=m_hWnd;
	bi.pidlRoot=0;
	bi.pszDisplayName=dispname;
	bi.lpszTitle="选择下载目录";
	bi.ulFlags=BIF_RETURNONLYFSDIRS;
	bi.lpfn=0;
	bi.lParam=0;
	bi.iImage=0;
	if(pi=SHBrowseForFolder(&bi))
		SHGetPathFromIDList(pi,path);
	else
	{
		AfxMessageBox("Failed!");
		return;
	}

	strPath.Format("%s",path);

	hInetSession=InternetOpen("MyFtp",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	if(!hInetSession)
	{
		AfxMessageBox("不能打开网络!");
		return;
	}
	if(!(hFtpConn=InternetConnect(hInetSession,m_addr_down,21,m_user_down,m_pass_down,INTERNET_SERVICE_FTP,0,1)))
	{
		AfxMessageBox("不能连接网络!");
		return;
	}
	
	if(!FtpGetFile(hFtpConn,m_path_down,strPath+"\\"+m_local_name,TRUE,FILE_ATTRIBUTE_NORMAL,INTERNET_FLAG_NO_CACHE_WRITE,2))
	{
		CString str;
		str.Format("%d",GetLastError());
		AfxMessageBox("不能下载文件!Error Code:"+str);
		
	}
	else
	{
		AfxMessageBox("下载成功");
	}


	if(hFtpConn)
		InternetCloseHandle(hFtpConn);
	if (hInetSession)
		InternetCloseHandle(hInetSession);
	
}

void CMyFtpDlg::OnBrowseUp() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString PathName;
	char szFilter[] = "选择上传文件 (*.*)|*.*";//用于设置FileDialog的文件类型
	HINTERNET hInetSession,hFtpConn;

	CFileDialog FileDlg( TRUE, NULL, NULL,OFN_HIDEREADONLY, szFilter );
	if( FileDlg.DoModal() == IDOK )
	{
		PathName = FileDlg.GetPathName();
    }
	
	hInetSession=InternetOpen("MyFtp",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	if(!hInetSession)
	{
		AfxMessageBox("不能打开网络!");
		return;
	}
	if(!(hFtpConn=InternetConnect(hInetSession,m_addr_up,21,m_user_up,m_pass_up,INTERNET_SERVICE_FTP,0,1)))
	{
		AfxMessageBox("不能连接网络!");
		return;
	}
		
	if(!FtpPutFile(hFtpConn,PathName,m_path_up+"/"+FileDlg.GetFileName(),INTERNET_FLAG_NO_CACHE_WRITE,2))
	{
		CString str;
		str.Format("%d",GetLastError());
		AfxMessageBox("不能上传文件!Error Code:"+str);
	}
	else
		AfxMessageBox("上传成功!");

	if(hFtpConn)
		InternetCloseHandle(hFtpConn);
	if(hInetSession)
		InternetCloseHandle(hInetSession);


}

⌨️ 快捷键说明

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