📄 ftpexp~1.cpp
字号:
// FtpExpDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "FtpExp.h"
#include "FtpExpDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CFtpExpDlg 对话框
CFtpExpDlg::CFtpExpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFtpExpDlg::IDD, pParent)
, m_strServer(_T(""))
, m_strName(_T(""))
, m_strPassWord(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFtpExpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_FILE, m_listFile);
DDX_Control(pDX, IDC_ANONYMOUS, m_btnAnonymous);
DDX_Text(pDX, IDC_EDIT_SERVER, m_strServer);
DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strPassWord);
}
BEGIN_MESSAGE_MAP(CFtpExpDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_SHOW, OnBnClickedShow)
ON_BN_CLICKED(IDC_ANONYMOUS, OnBnClickedAnonymous)
END_MESSAGE_MAP()
// CFtpExpDlg 消息处理程序
BOOL CFtpExpDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO:在此添加额外的初始化代码
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
void CFtpExpDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CFtpExpDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CFtpExpDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CFtpExpDlg::OnBnClickedShow()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
CStringArray localName;
int nFileNumber=0;
//初始化一个Internet连接
InitConnection();
//建立到FTP服务器的连接
if(OpenConnection(m_strServer))
{
//获取FTP服务器上的文件目录
nFileNumber=GetFileName(&localName);
}
else
{
MessageBox("不能连接服务器!");
return;
}
//用FTP服务器上的文件填充列表框
for(int test=0;test<nFileNumber;test++)
{
m_listFile.AddString((LPCTSTR)(localName.GetAt(test)));
}
//关闭与FTP服务器的连接
CloseConnection();
}
void CFtpExpDlg::OnBnClickedAnonymous()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
int check = m_btnAnonymous.GetCheck();
if(check == 1)
{
m_strName="anonymous";
m_strPassWord="yours@email.com";
UpdateData(FALSE);
}
}
BOOL CFtpExpDlg::OpenConnection(CString strServer)
{
//设置等待光标
CWaitCursor cursor;
//若FTP服务器的URL为空返回0
if(strServer == "")
return 0;
//若用户名或密码为空则返回0
if( (m_strPassWord == "") || (m_strName == ""))
return 0;
CString strTemp;
strTemp = "ftp://";
m_strFullURL=strServer;
//解析URL
if (!AfxParseURL(m_strFullURL, dwServiceType, m_strServer, strAppName, nPort))
{
m_strFullURL=strTemp+ m_strFullURL;
if (!AfxParseURL(m_strFullURL, dwServiceType, m_strServer, strAppName, nPort))
{
AfxMessageBox("无法解析服务器地址");
return 0;
}
}
//获取FTP服务器路径名
int nIndex=strTemp.GetLength()+m_strServer.GetLength();
m_strFullPath=m_strFullURL.Right(m_strFullURL.GetLength()-nIndex);
try {
//连接FTP服务器
pFtpConnection = pInternetSession->GetFtpConnection(m_strServer,
m_strName,
m_strPassWord);
}
catch (CInternetException* pEx)
{
//如果出错,返回错误信息
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
TRACE(szErr);
AfxMessageBox(szErr);
pEx->Delete();
return 0;
}
return 1;
}
int CFtpExpDlg::GetFileName(CStringArray *localNameArray)
{
//设置等待光标
CWaitCursor cursor;
BOOL goodfile;
int x=0;
int nFileNumber=0;
//设置当前路径
pFtpConnection->SetCurrentDirectory(m_strFullPath);
CFtpFileFind fFiles(pFtpConnection);
//查找第一个FTP文件
goodfile=fFiles.FindFile(m_strFullPath+_T("/*"));
if(goodfile==FALSE)
{
AfxMessageBox("目录为空。");
return 0;
}
CString str;
while(goodfile)
{
//查找下一个FTP文件
goodfile=fFiles.FindNextFile();
//获取文件名称
str=fFiles.GetFileName();
//判断文件是否是目录
if(fFiles.IsDirectory())
{
str+=" <DIR>";
}
//将文件或目录存入CStringArray结构中
localNameArray->InsertAt(x,str);
nFileNumber++;
x++;
}
//返回文件和目录的总数
return nFileNumber;
}
void CFtpExpDlg::InitConnection()
{
//获取应用程序名称
//strAppName.LoadString(AFX_IDS_APP_TITLE);
strAppName=AfxGetAppName();
// 创建一个Internet会话
pInternetSession = new CInternetSession(strAppName,
INTERNET_OPEN_TYPE_PRECONFIG);
// 如果创建失败,则返回并给出错误信息
if(!pInternetSession)
{
AfxMessageBox("初始化会话失败!");
return;
}
}
BOOL CFtpExpDlg::CloseConnection()
{
if(pFtpConnection == NULL)
return 0;
try
{
//关闭与FTP服务器的连接
pFtpConnection->Close();
}
catch(...)
{
return 0;
}
//释放资源
if(pFtpConnection != NULL)
delete pFtpConnection;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -