📄 myftpdlg.cpp
字号:
// MyFtpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyFtp.h"
#include "MyFtpDlg.h"
#include "TipDlg.h"
#include "UpDlg.h"
#include "CrtDirDlg.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_HostEdit = _T("202.117.1.24");
m_PasswordEdit = _T("");
m_UserEdit = _T("");
m_port = 21;
m_DirName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pFtpConnection = NULL;
m_pInetSession = NULL;
iItemCount=0;
}
CMyFtpDlg::~CMyFtpDlg()
{
if(m_pFtpConnection != NULL)
{
m_pFtpConnection->Close();
delete m_pFtpConnection;
}
if(m_pInetSession != NULL)
{
m_pInetSession->Close();
delete m_pInetSession;
}
}
void CMyFtpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyFtpDlg)
DDX_Control(pDX, IDC_LIST, m_FileList);
DDX_Text(pDX, IDC_IP, m_HostEdit);
DDX_Text(pDX, IDC_PASSWORD, m_PasswordEdit);
DDX_Text(pDX, IDC_USER, m_UserEdit);
DDX_Text(pDX, IDC_PORT, m_port);
DDX_Text(pDX, IDC_DIRNAME, m_DirName);
//}}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_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
ON_BN_CLICKED(IDC_CREATEDIR, OnCreatedir)
ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList)
ON_BN_CLICKED(IDC_FATHERDIR, OnFatherdir)
ON_BN_CLICKED(IDC_DEL, OnDel)
//}}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
// 插入列表控件纵列
m_FileList.InsertColumn(0, "名称", LVCFMT_LEFT, 230);
m_FileList.InsertColumn(1, "大小", LVCFMT_LEFT, 70);
m_FileList.InsertColumn(2, "类型", LVCFMT_LEFT, 70);
m_FileList.InsertColumn(3, "更改时间", LVCFMT_LEFT,130);
m_FileList.InsertColumn(4, "属性", LVCFMT_LEFT, 70);
m_FileList.InsertColumn(5, "描述", LVCFMT_LEFT, 70);
m_FileList.InsertColumn(6, "所有权", LVCFMT_LEFT, 70);
// 设置列表控件扩展样式为整行高亮
m_FileList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
bConnect=FALSE;
bFirstConnect=TRUE;
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::OnConnect()
{
UpdateData();
GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE);
m_HostEdit.TrimLeft();
m_HostEdit.TrimRight();
m_UserEdit.TrimLeft();
m_UserEdit.TrimRight();
m_PasswordEdit.TrimLeft();
m_PasswordEdit.TrimRight();
if (m_UserEdit=="") m_UserEdit="anonymous";//缺省用户名为anonymous
if (m_PasswordEdit=="") m_PasswordEdit="anonymous";//缺省密码为anonymous
if (m_port==0) m_port=21;//缺省端口为21
TRACE(":%s:%s:%s:%s\n", m_HostEdit, m_UserEdit, m_PasswordEdit);
try
{
m_pInetSession = new CInternetSession();//建立Internet连接
m_pFtpConnection = m_pInetSession->GetFtpConnection(m_HostEdit, m_UserEdit, m_PasswordEdit,m_port);
//建立FTP连接
if (bFirstConnect) List();//第一次连接时显示服务器目录文件及文件夹
bConnect=TRUE;
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);//连接已经建立,允许点击断开按钮
}
catch(CInternetException *pEx)//错误处理
{
TCHAR szError[1024];
if(pEx->GetErrorMessage(szError,1024))
AfxMessageBox(szError);
else AfxMessageBox("发生错误.");
pEx->Delete();
m_pFtpConnection = NULL;
m_pInetSession = NULL;
GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
}
}
void CMyFtpDlg::OnSend()
{
CString strSrc,strDst;
CUpDlg dlg;
if (!m_pFtpConnection)
{
MessageBox("还没有连接到FTP服务器。");
return;
}
dlg.DoModal();
strSrc=dlg.GetSrcFile();
strDst=dlg.GetDstFile();
if (!m_pFtpConnection->PutFile(strSrc,strDst))
{
CString errStr;
DWORD dw = GetLastError();
errStr.Format("上传文件失败.Error Code: %u",dw);
MessageBox(errStr);
}
else
{
MessageBox("上传文件成功!");
}
return;
}
void CMyFtpDlg::OnDisconnect()
{
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
if (m_pFtpConnection!=NULL)
{m_pFtpConnection->Close();
delete m_pFtpConnection;
m_pFtpConnection=NULL;
}
if (m_pInetSession!=NULL)
{m_pInetSession->Close();
delete m_pInetSession;
m_pInetSession=NULL;
}
bConnect=FALSE;
GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
}
void CMyFtpDlg::OnCreatedir()
{
CString steDir;
CCrtDirDlg dlg;
if (!m_pFtpConnection)
{
MessageBox("还没有连接到FTP服务器。");
return;
}
dlg.DoModal();
steDir=dlg.GetDirName();
if (!m_pFtpConnection->CreateDirectory(m_DirName))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -