📄 dlgftp.cpp
字号:
// DlgFtp.cpp : 实现文件
//
#include "stdafx.h"
#include "Test.h"
#include "DlgFtp.h"
#include ".\dlgftp.h"
// CDlgFtp 对话框
IMPLEMENT_DYNAMIC(CDlgFtp, CDialog)
CDlgFtp::CDlgFtp(CWnd* pParent /*=NULL*/)
: CDialog(CDlgFtp::IDD, pParent)
{
}
CDlgFtp::~CDlgFtp()
{
}
void CDlgFtp::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_IP_FTPSRV, m_ctlIP);
DDX_Control(pDX, IDC_RDO_PORT, m_ctlPort);
DDX_Control(pDX, IDC_RDO_PASSIVE, m_ctlPassive);
DDX_Control(pDX, IDC_EDT_RECVCMD, m_ctlOutput);
}
BEGIN_MESSAGE_MAP(CDlgFtp, CDialog)
ON_MESSAGE(WM_DATA, OnFtpData)
ON_BN_CLICKED(IDC_BTN_EXIT, OnBnClickedBtnExit)
ON_BN_CLICKED(IDC_BTN_CNN, OnBnClickedBtnCnn)
ON_BN_CLICKED(IDC_BTN_CLOSE, OnBnClickedBtnClose)
ON_BN_CLICKED(IDC_BTN_LIST, OnBnClickedBtnList)
ON_BN_CLICKED(IDC_BTN_PWD, OnBnClickedBtnPwd)
ON_BN_CLICKED(IDC_BTN_FATDIR, OnBnClickedBtnFatdir)
ON_BN_CLICKED(IDC_BTN_SUBDIR, OnBnClickedBtnSubdir)
ON_BN_CLICKED(IDC_BTN_DOWN, OnBnClickedBtnDown)
ON_BN_CLICKED(IDC_BTN_UP, OnBnClickedBtnUp)
END_MESSAGE_MAP()
// CDlgFtp 消息处理程序
BOOL CDlgFtp::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
m_ctlIP.SetAddress( 127, 0, 0, 1 );
SetDlgItemInt(IDC_EDT_SRVPORT, 21);
m_ftp.SetSocketNotify(m_hWnd, WM_DATA);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CDlgFtp::OnOK()
{
OnBnClickedBtnExit();
}
void CDlgFtp::OnCancel()
{
OnBnClickedBtnExit();
}
void CDlgFtp::OnBnClickedBtnExit()
{
OnBnClickedBtnClose();
CDialog::OnOK();
}
LRESULT CDlgFtp::OnFtpData( WPARAM wParam, LPARAM lParam )
{
switch(wParam)
{
case NET_FTPCOMMAND:
AddOutputCommand((const FTP_LPARAM*)lParam);
break;
case NET_FTPREPLY:
AddOutputReply((const FTP_LPARAM*)lParam);
break;
case NET_FTPLOGIN:
TRACE("\nConnect FTP Server Success.");
break;
case NET_FTPLIST:
AddOutputReply((const FTP_LPARAM*)lParam);
break;
default:
break;
}
return NULL;
}
void CDlgFtp::OnBnClickedBtnCnn()
{
int nPort;
BYTE pucIP[4];
CString strUser;
CString strPass;
if (m_ctlPort.GetCheck() == BST_CHECKED)
{
m_ftp.SetMode(CFtp::PORT);
}
else if(m_ctlPassive.GetCheck() == BST_CHECKED)
{
m_ftp.SetMode(CFtp::PASSIVE);
}
m_ctlIP.GetAddress(pucIP[0], pucIP[1], pucIP[2], pucIP[3]);
nPort = GetDlgItemInt(IDC_EDT_SRVPORT);
GetDlgItem(IDC_EDT_USER)->GetWindowText(strUser);
GetDlgItem(IDC_EDT_PASS)->GetWindowText(strPass);
if (m_ftp.Connect(pucIP, nPort, strUser, strPass))
{
MessageBox("连接FTP服务器失败。");
return;
}
}
void CDlgFtp::OnBnClickedBtnClose()
{
m_ftp.Close();
}
void CDlgFtp::OnBnClickedBtnPwd()
{
m_ftp.Pwd();
}
void CDlgFtp::OnBnClickedBtnList()
{
m_ftp.List();
}
void CDlgFtp::OnBnClickedBtnFatdir()
{
m_ftp.CDFather();
}
void CDlgFtp::OnBnClickedBtnSubdir()
{
CString strDir;
GetDlgItem(IDC_EDT_SUBDIR)->GetWindowText(strDir);
m_ftp.CDSub(strDir);
}
void CDlgFtp::OnBnClickedBtnDown()
{
CString strFile;
CString strDes;
CString strFile1;
CString strFile2;
int nPos1;
int nPos2;
GetDlgItem(IDC_EDT_FILE)->GetWindowText(strFile);
nPos1 = strFile.ReverseFind('\\');
if (nPos1 >= 0 && nPos1 < strFile.GetLength() - 1)
{
strFile1 = strFile.Right(strFile.GetLength() - nPos1 - 1);
}
nPos2 = strFile.ReverseFind('/');
if (nPos2 >= 0 && nPos2 < strFile.GetLength() - 1)
{
strFile2 = strFile.Right(strFile.GetLength() - nPos1 - 1);
}
if (!strFile1.IsEmpty() && !strFile2.IsEmpty())
{
if (strFile1.GetLength() < strFile2.GetLength())
{
strDes = strFile1;
}
else
{
strDes = strFile2;
}
}
else if (!strFile1.IsEmpty())
{
strDes = strFile1;
}
else if (!strFile2.IsEmpty())
{
strDes = strFile2;
}
else
{
strDes = strFile;
}
m_ftp.DownLoad(strFile, strDes);
}
void CDlgFtp::AddOutputReply(const FTP_LPARAM *pucText)
{
CString strText;
CString strRecv;
strRecv.Format("Reply:%c%c%s%c%c", 13, 10,
(const BYTE*)pucText->m_pRecvData, 13, 10);
m_ctlOutput.GetWindowText(strText);
strText += strRecv;
m_ctlOutput.SetWindowText(strText);
m_ctlOutput.LineScroll(m_ctlOutput.GetLineCount());
}
void CDlgFtp::AddOutputCommand(const FTP_LPARAM *pucText)
{
CString strText;
CString strRecv;
strRecv.Format("Command:%c%c%s%c%c", 13, 10,
(const BYTE*)pucText->m_pRecvData, 13, 10);
m_ctlOutput.GetWindowText(strText);
strText += strRecv;
m_ctlOutput.SetWindowText(strText);
m_ctlOutput.LineScroll(m_ctlOutput.GetLineCount());
}
void CDlgFtp::OnBnClickedBtnUp()
{
MessageBox("该功能未完成");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -